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.