Friday, 31 October 2014

Android App to connect to Thingspeak

Hi, everyone. Few days back we learned how we can upload data to Thingspeak cloud IoT platform. But it is very tedious job to every time open the web browser and insert command or data. So today we will develop an android app that will do it for us. It is recommended that you go through the Thingspeak documentation first. Ok so let's get started.


What you need

  • Laptop or PC with internet connection.
  • Thingspeak account.
  • Android smartphone.
  • Google account.

What is this APP thing?

In this tutorial we will make use of the Thingspeak Apps. You may ask what is this App. It is nothing but services provided by Thingspeak to communicate with your devices. These apps can be accessed by POST, GET, PUT and DELETE HTTP requests. You can check out my previous post on how to make an HTTP request to upload data to your Thingspeak Channel. In this tutorial we will make use of the following Thingspeak Apps.
  • Talkback - This App is used to control your device based on some decision made on the basis of your sensor data in your channel. Using this app you can store commands which your device may fetch at regular intervals or when an event occurs to execute.
  • React - This is a decision making App. We can use this app to trigger an event based on your Thingspeak data channel. The field on which decision is to be made can be String, Location or Numeric data. There are a whole lot of logic that you can execute based on the data. In this project I will use my android app to get my current GPS location and upload it to my channel based on which an event will be triggered. 
  • ThingHTTP - This App is used to do an HTTP request from within Thingspeak. So we can use this App to trigger our Talkback App in turn to update the commands.

Getting the Idea

What I am trying to do here is that I will develop an Android app that will POST my current location to my Thingspeak channel which will trigger some logic in the React App. This App will in turn call the ThingHTTP App to send a PUT request to my Talkback App and update the current command. This command will be fetched by my device on regular interval to perfom some action. The last part will not be covered in this post. I will cover it in my next post.

Setting up Thingspeak

It is assumed that you already have a Thingspeak channel set up. If not follow my previous post.
Before moving on please read the documentation each of this App so that you have a better understanding of how they work.
  • Open your Thingspeak account and click on the Apps tab.
 
  • You will get your Apps menu. Now click on Talkback App.
 
  • Now click on "New Talkback".
  • Now Fill in the Name and select your channel and set a default command by clicking add new command. For now we will add "CLOSE".

  • Now again click on the Apps tab and go to the "ThingHTTP" App.
  • Now click on "New ThingHTTP" and fill  up like this and save your ThingHTTP.


  • Now create another ThingHTTP App for closing. Like this.
 
  • Now click on App and now go to the React App.
  • Click on :"New React" and fill up like this. Save your React App.
 
  • Now create another React App in similar manner for closing like this.
 
  • While doing all the above steps give the parameters carefully like latitude, longitude,api_key etc. specific to your Apps.
  • Now you are ready to go. Lets create the Android App.

Setting up App Inventor 2

Since you are developing an app for android it is already assumed that you have a google account. Now go to the website http://ai2.appinventor.mit.edu. Use your google account to log in and create a new project. App Inventor is an online development platform for creating android apps without any knowledge of programming. It is based on graphical block development. You only need to arranged the required blocks just like a puzzle. Follow the getting started guide to have a clear understanding.
  • After you have opened App Inventor 2 and created a project bring the following objects to the canvas. Check this image to get a list.
 
  • Change the Button1 Text to Turn OFF and the Button2 Text to Turn ON
  • Change the Web1 url to https://api.thingspeak.com/talkbacks/<your Talkback ID>/commands/<your command ID> 
  • Change the Web2 url to https://api.thingspeak.com/update
  • Change the Clock1 Time interval to 10000.
  • Change LocationSensor1 Time interval to 10000.

  • Now click on "Blocks" on the top left corner of your screen.
  • If you have gone through the Getting started portion then you will be familiar with the blocks. So arrange the blocks as shown below.
 
  • Replace Web1.PutText api_key to your Talkback API_Key.
 
  • Replace Web2.PostText api_key to your Channel API_Key.



  • Now click on "Build"->"App(save .apk to my Computer)". It will download the file to your PC. Transfer the file to your android phone and install the app.

Running the Apps

  • Now turn ON you GPS and WiFi/Data connection.
 
  • Now open the Talkback App on your Thingspeak account. You will find the default command as CLOSE.
 
  • Now open the app in your android phone and wait for sometime untill the location is displayed.
 
  • Now refresh the Talkback page. If you are at or near the location you specified in your React App, the command will automatically change to OPEN. You can also manually TURN ON/OFF using the buttons provided.
 

What will you Make?

Isn't it awesome? Now you can automatically pre-cool your room when you are close to your home or you can turn the room lights ON or OFF using the app. There are limitless possibilities of things that you can connect and control through the internet using simple tools like Thingspeak, App Inventor etc. Please keep me posted about your projects and if you are facing any difficulty in understanding the instructions please let me know through the comments. In our next post we will see how we can make our devices fetch these commands at regular intervals and perform certain tasks. Till then keep making and if you like my post please share and +1 me on google.

29 comments:

  1. Hi! I just wanna do autowatering system. How I can start watering with buttons start/stop

    ReplyDelete
  2. In your android app create a button which will send POST request to update the commands in the talkback app. On the controller side use GET requests to execute those commands. One command can be executed only once. Check out this page for more info. https://thingspeak.com/docs/talkback

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. hi, Sounak
    Thank you for showing your project.
    I could learn thingspeak app by you too.
    Now talkback app's version was changed and it did not become to return a response about OPEN & CLOSE as status on its app.

    If possible, Advice me with something.
    I want to get a response and the status on thingspeak's Channel and android app(app inventor).
    Thank you.

    ReplyDelete
  6. i cant acces my thingspeak talkback using this app to add a new command, inorder to add a talkback command. what i have to do?????

    ReplyDelete
  7. is there any video tutorials explaining more about talkback,react and thinghttp ??

    ReplyDelete
  8. Hii thanks for showing that....I actually want to get a graph from the thingspeak to Android app......can you help me with that please!!!!

    ReplyDelete
    Replies
    1. Hi,

      You can do an HTTP request to thingspeak to get your past data. Refer to Thingspeak documentation for that. After that you can use some graph plotting APIs like Plotly to get some graphs.

      Delete
  9. how can i login thingspeak user account in my own android app?

    ReplyDelete
    Replies
    1. Hi Jatin,

      Everything in Thingspeak works through HTTP requests. Every profile will have it's unique API key which allows you to use all the features of thingspeak.

      Delete
  10. Hey! I wanna send a command from my android app to control some ports on my development board, how can I do it?

    ReplyDelete
    Replies
    1. hey bro!
      I also want to do the same but even above project is not working , when I run my app it always pop-up an error "wrong no. of argument for posts" would u please help. any help is appreciable.

      Delete
  11. https://play.google.com/store/apps/details?id=aarsoftronix.thingspeak


    Best app for thingspeak work

    ReplyDelete
    Replies
    1. That is cool. At the time I wrote this blog I think this app was not there.

      Delete
  12. https://play.google.com/store/apps/details?id=comaarsoftronix.facebook.ihome

    Best for esp8266 work

    ReplyDelete
  13. You can still use this applications to track someones location.

    ReplyDelete
  14. Hi,
    Sorry guys I was out of touch from this blog for 2 years. I couldn't reply to most of your comments. But now I am planning to continue the blog with some new projects.

    ReplyDelete
  15. Hi,thanks for this tutorial,can this be done in android studio?

    ReplyDelete
  16. I have ESP8266 connected to DHT22. I see Temperature and Humidity on Thingspeak, both Private or Public. All I want to do now is control a relay from my Android cell phone. It would be great to see a step by step and comments from someone. I really do want to learn, but finding it really tough.

    ReplyDelete
  17. hi,how do i notify my android when some changes occur in the threshold level which is an matlab output

    ReplyDelete
  18. if you try renaming the master file, like the way some people did on gaana app, it won't play.  download

    ReplyDelete
  19. But how can I get data from thingspeak...I am facing problem in that part. Can u guide us?

    ReplyDelete
  20. that's awesome! Am having some difficulties with my project, that is building an app that will be able to track my wearable tracking device.

    ReplyDelete
  21. sir i just need send or post mobile gps data to thingspeak.can you explain me? sir.

    ReplyDelete