Showing posts with label IoT Cloud. Show all posts
Showing posts with label IoT Cloud. Show all posts

ThingSpeak Library with Arduino UNO

Installing the ESP8266 Library for the Arduino IDE

          ESP8266 works with the Arduino Uno by utilizing the ESP8266 library. In this section, we will see how to install the ThingSpeak API for ESP8266 with Arduino Uno. This is a pre-requisite of installing the ThingSpeak library, before we can log data to the cloud. You will need this library in order to include the ThingSpeak.h file in the sketch below.
  • Launch the Arduino IDE.
  • Navigate to Sketch > Include Library > Manage Libraries...
  • The Library Manager window will pop up.
  • In the Library Manager window, search for ThingSpeak and install the required library. The library that I choose to install is shown below.

Installing the ThingSpeak API for the Arduino IDE

          In this section, we will see how to install the ThingSpeak API for ESP8266 with Arduino Uno. This is a pre-requisite of installing the ThingSpeak library, before we can log data to the cloud. You will need this library in order to include the ThingSpeak.h file in the sketch below.
  • Launch the Arduino IDE.
  • Navigate to Sketch > Include Library > Manage Libraries...
  • The Library Manager window will pop up.
  • In the Library Manager window, search for ThingSpeak and install the required library. The library that I choose to install is shown below.
Figure 3 - ThingSpeak API for ESP8266 with Arduino Uno

Post Data to ThingSpeak - Bad Request Problem Solved

In this post we will see the series of AT Commands that you may use from any serial terminal in order to successfully post data to ThingSpeak IoT Cloud.  Many people have reported of getting a Bad Request error returned when they tried to post data to the ThingSpeak server. It was indeed very tricky to figure this out a way to fix the Bad Request problem. Hence I am sharing this information as it was very troublesome to find all the required information in one place on the Internet.

Pre-Requisites

This post assumes that you already have an ESP8266 and a FTDI FT232RL USB to TTL Converter connected properly. You can refer for full connection details here.

The Data Logging Session

For sending data to ThingSpeak use the following series of AT commands from the Arduino Serial Monitor or from any other terminal of your choice. Set the Serial baud rate at 11500 and Both NL & CR. Then execute the following AT Commands.

AT

AT+CWLAP

AT+CWJAP="your-wifi-ssid","your-password"

AT+CIPSTART="TCP","184.106.153.149",80

AT+CIPSEND=60

GET /update?key=<your-channel-api-write-key>&field1=56\r\n        (from my Arduino IDE's Serial Monitor, I had to send this line 2 times...I don't know why yet)

AT+CWQAP

That is all! This should work fine. My Serial window snip is shown below for reference.

Post Data to ThingSpeak - Bad Request Problem Solved


Summary - The Trick

In the series of AT commands above notice the line "AT+CIPSEND=60", the number 60 here means the number of text characters (technically bytes) that will be sent in the request. It is referred to as the Data Length. Read below and see how to manipulate this Data Length setting in order to solve the Bad Request problem.

The request text is in the line "GET /update?key=<your-channel-api-write-key>&field1=56\r\n". In my case, the number of characters in this request text was 46 (including the last \r\n characters, you will have to count them as well). However, I specified 60 as the Data Length. This is the trick. You will have to mention the Data Length a little bit higher than the actual size of the Request Text length.

Cheers!
KBRC


If you liked this post, then your encouragement by performing a G+ and a share is most welcome and highly appreciated!

Fetching Data From ThingSpeak IoT Cloud

In this post we will see how to fetch data from ThingSpeak (an IoT Cloud plarform). The data can be fetched from any client device: A laptop, mobile or a micro-controller.

We will use a JQuery based web page on a laptop, to fetch the data logged into ThingSpeak. You may visit my other post on setting up a device channel in ThingSpeak for a tutorial on the pre-requisites.

The HTML/JQuery Code

The JQuery based web page has been coded in such a way that it will fetch the last logged data row every 10 seconds. The re-usable code has been provided below.



Steps to re-use the above code

Follow these steps to re-use the above code.
  1. Download the JQuery library. I have used "jquery-1.12.1.min.js". You are free to chose, but if you do, then make sure you update the HTML appropriately.
  2. Copy the code
  3. Create an HTML file in the same directory as the JQuery library
  4. Paste the code in the HTML file
  5. Make sure you change the channel ID in the GetData() function to fetch data from your device channel. However, I have left mine "as is" in the above code, so that it works out of the box for you.
  6. Save and close the HTML file
  7. Open the HTML file in a browser
  8. Every 10 seconds it should fetch data from ThingSpeak and display it on the screen

Running the HTML/JQuery App

Follow these steps outlined below.
  1. Just right click and open with a browser.
  2. This is what I see when I open the HTML file.
Fetching Data From ThingSpeak IoT Cloud

What Next?

Well, this is how you can fetch the logged data from your remote sensors or edge devices via an IoT Cloud such as ThingSpeak. Of course you may perform a lot of things hereafter: predictive analytics, machine learning and so on...

Cheers,
KBRC



Setting up a Device Channel in ThingSpeak IoT Cloud

In this post we will see the process of Setting up a device channel in the IoT Cloud platform ThingSpeak.

Step-1: The first step is to create an account. The good part is that ThingSpeak does not require a Credit card (like AWS/Azure) for signing up. You may sign up here.

Step-2: After signing up on ThingSpeak, login here.

Step-3: Create a new ThingSpeak channel by clicking on the "New Channel" button shown below.

Create New ThingSpeak Channel

Step-4: In the new channel creation screen, fill out the details of the device from which data will be received.

Create New ThingSpeak Channel

Step-5: In the new channel creation screen, scroll down further and make this channel public. So that you can post data to it.

Create New ThingSpeak Channel

Step-6: After saving the channel, the channel details screen will get displayed (as shown below). Notice that there will be 5 tabs: Private View, Public View, Channel Settings, API Keys and Data Import/Export. You are all set! Browse through them to get a feeling of the contents. Make note of the Channel ID, as you will need it for posting and reading data to ThingSpeak.

Create New ThingSpeak Channel

Step-7: Now we will see how to log data and read data to and from this channel. Browse to the API Keys tab for your channel. Note down the read and write api_keys (as shown below), as you will need it for posting and reading data to ThingSpeak. I have obscured the api keys for my channel intentionally. Never share your channel api keys with unintented recipients as they might spam the channel inadvertently ;)

Create New ThingSpeak Channel

Step-8: Use the following URL format to log data to ThingSpeak.

https://api.thingspeak.com/update.json?api_key=<your-write-api-key>&field1=58

Upon hitting the above URL, the data '58' will get logged in the ThingSpeak IoT cloud. You will see the below response on your browser.

How to use ThingSpeak for IoT

Correspondingly, you can also see the logged data in the ThingSpeak channel's Private/Public View tabs, as shown below.



Step-9: Use the following URL format to read the logged data from ThingSpeak.

http://api.thingspeak.com/channels/<your-channel-id>/feeds.json?key=<your-read-api-key>&results=1

On hitting the above URL in the browser, you will receive the last posted data row from ThingSpeak, as shown below.




To conclude, I hope you liked this post on creating a device channel in ThingSpeak. I love hearing from you, so don't forget to leave your comments and share your experience using the ThingSpeak IoT cloud.

Cheers!
KBRC