In this post we are going to learn how to send sensor data to Thingspeak IoT platform using ESP8266 and Arduino. In a previous tutorial we saw how to send data to thingspeak using GSM modem and Arduino which didn’t require a Wi-Fi hotspot. In this post we will learn how to connect your Arduino projects to Wi-Fi and observe data in real time on Thingspeak.
We will see:
- What is ESP8266 Wi-Fi Module?
- What is Thingspeak platform, how to create an account and get API key?
- How to upload code to ESP8266 module?
- How to install ESP8266 board package to Arduino IDE?
- Circuit diagram for uploading a code to ESP8266.
- Possible errors while uploading a code to ESP8266 and solutions.
- Final circuit diagram for sending temperature and humidity data to thingspeak.
- Program codes for ESP8266 and Arduino.
UPDATE: An easier method on How to Send Data to Thingspeak (without sensor) using ESP8266 and NodeMCU is explained here.
Quick overview: What we are going to do?
- We are going to send temperature and humidity data to thingspeak using DHT11, ESP8266 module and Arduino board.
- We will learn how to upload a program code to ESP8266 module; the uploaded program code on ESP8266 will accept serial data from Arduino which will be forwarded to Thingspeak platform via Wi-Fi connection.
- The Arduino board will collect temperature and humidity data from DHT11 sensor and will be sent serially to ESP8266 module.
- We will be uploading two sets of program codes: One for Arduino and another for ESP8266 module. We will be utilizing the same Arduino board to upload the program code to ESP8266.
What is ESP8266 Wi-Fi Module?
ESP8266 module is a microcontroller board with Wi-Fi capability which is designed to operate as a standalone device as well as a slave device when it is connected to a microcontroller (master).
Beginners may think ESP8266 module is a slave device and need a microcontroller to make it functional, but this is not always true. The ESP8266 can operate as a standalone device as it sport a 32-bit microcontroller chip, RAM, GPIO pins and a flash memory which can be programmed using Arduino IDE.
But this module have only two GPIO pins and may limit our number of sensors and peripherals which we may connect to it. If you are going to connect one or two (max) sensors then you may not need any additional microcontroller to support the sensors, you can directly add the library files (if necessary) and write a code just as you do for any arduino boards.
As we need more than two pins for the sensors, we are using the ESP8266 as a slave device; this is done by uploading a program code to ESP8266 which will accept serial data from arduino. We will see how to upload the code to ESP8266 using Arduino Uno board in detail later part of this article.
Now let’s take a look at its specifications of ESP8266.
- ESP8266 module has its own SoC or system on chip, it utilizes Tensilica L106 32-bit processor clocked at 80MHz.
- It has 32KB instruction RAM, 32KB instruction cache RAM, 80KB user data RAM and 16 KBETS system data RAM.
- It can operate from 2.5V to 3.6V (nominal 3.3V) and 5V will kill the board. It has average current consumption about 80mA.
- It supports 802.11n (2.4 GHz) and can communicate with a speed of 72.2 Mbps (max).
- It supports these network protocols: IPv4, TCP/UDP/HTTP.
Pin Diagram of ESP8266:
The pin diagram is also printed on the back of ESP8266 module, which makes our tasks much easier.
By now you would have got an idea on ESP8266 module and you might have understood that we have to upload a program code to ESP8266 to make it operational either as a standalone or as a slave device.
What is Thingspeak platform, how to create an account and get API key?
If you are just exploring these things for the first time please click here and read the following topics in the post.
- What is Thingspeak IoT analytics Platform?
- What is API Key?
- How to Sign up for Thingspeak and get API Key
NOTE: Please follow the channel settings in the mentioned tutorial link and replicate the same channel set-up for your thingspeak account for this tutorial.
How to upload code to ESP8266?
As we mentioned at the beginning of this tutorial ESP8266 is a microcontroller board with Wi-Fi capability and we need to upload some program code to make it functional.
So now we are going upload a program code which is written using Arduino IDE and we are going to use Arduino Uno board’s build-in programmer to upload the code to ESP8266.
Circuit Diagram for uploading code to ESP8266:
You need to wire up the circuit as per the above schematic, you will need 2 push buttons and don’t forget to remove the ATmega328P from the board. Double check the voltage supply to ESP8266 as it operates on 3.3V and 5V will kill the board.
The ESP8266 will get slightly warm after you connect it to the power supply which is normal but, if it gets very hot then you might have probably messed with its wire connections/supply voltage.
Once the hardware is ready we need to install the ESP8266 board package, now let’s see how to do that.
How to install ESP8266 board package to Arduino IDE?
This package will make the Arduino IDE compatible with ESP8266 module.
- Copy this link: http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Now open Arduino IDE and click on File > Preferences.
- A window will open like this:
- Paste the URL on the box and click “OK”.
- Now go to Tools > Board > Boards Manager.
- A window will popup:
- Type “ESP8266” on the box as shown and you will get installation option, select the latest version and click install.
- Now the IDE will download the necessary packages and this could take more than 5 minute to complete.
- Now go to Tools > Board > select “Generic ESP8266 Module”.
- Now copy the given below ESP8266 program code and paste it on to Arduino IDE software and insert your SSID and Password of your Wi-Fi hotspot and Copy the “write API key” and “channel ID” from your thingspeak account and insert it to the code.
- Now press compile button (Green tick button). The compilation of code may take more than couple of minutes so be patient. If the compilation failed please check whether have you selected the “Generic ESP8266 Module” in the board option or not.
- After successful compilation of the code, connect the Arduino to your PC and go to Tools > Port and make sure that correct COM port is selected.
- Now Press and hold the FLASH button and press RESET button momentarily once and leave the FLASH button. By doing this ESP8266 will get ready for a new program to be uploaded.
- Now click upload, the code will compile again and begin to upload and uploading could take 1 -2 minutes.
- Once the code is successfully uploaded, it looks something like this:
- The orange colored text is not error or warning, but the uploading progress bar.
- Now the code is successfully uploaded to ESP8266 Module and now it can accept the data serially via its UART pins.
Download this Thingspeak library before you compile the code: Click here
Program code for ESP8266:
Possible Errors you will get while uploading to ESP8266:
There is a good chance that you could fail to upload the code in the first attempt, here is a possible error you may get:
If you see this error message in your IDE, the code did not get uploaded.
- You did not press the flash and reset button in the correct sequence.
- You did not connect TX and RX terminals properly. Please note that Tx should go to Tx and Rx should go to Rx. Also check other the wire connections too.
- The board could have damaged because you applied 5V to the board mistakenly before, the only possible solution for this is to replace the ESP8266 module.
- If your code did not compile successfully in the first place, you did not select the correct board type. Go to Tools > Board > select “Generic ESP8266 Module”.
Explanation for the ESP8266 Code:
The code will accept the data if it only can find “*” and “#” in the data stream. “*” is the start character and “#” is the end character. The below given code accepts 4 integer data after the “*” and stop accepting after the “#”.
The incoming data stream should be like this *2345# here, 23 is temperature and 45 is humidity. You can open serial monitor with the same upload setup and enter *2345#, Thingspeak will plot graph at 23 on temperature graph and 45 at humidity graph.
Please note that only four digits will accepted in between * and # with the above code and no decimal point should be send.
If you want to send 3 digits per graph then please refer this code:
Please note that after “*” there should be precisely 6 digits (no less or no more) otherwise data won’t be sent.
Full circuit diagram for sending data to Thingspeak using Arduino and ESP8266:
The above circuit is self-explanatory, just wire up as per the above circuit diagram. Please double check Vcc to ESP8266 it must be 3.3V and 5V can kill the module.
Download this DHT library for DHT11 sensor: Click here
Program code for Arduino:
Upload the above code to Arduino Uno board after inserting ATmega328P to the board. Don’t forget to select the board back to Arduino Uno, you can do this by going to Tools> boards> Arduino Uno, otherwise you will get compilation error.
Outcome of the above project:
You can open serial monitor and see DHT11’s data:
Graph generated on Thingspeak:
If you can’t see any data on your Thingspeak’s private view, then you should check wheather your ESp8266 is connected to your Wi-Fi hotspot or not. You can do this by checking the connected devices in your router’s admin panel.
Your connected ESP8266 on admin panel will be like ESP, followed by some numbers as shown below:
NOTE 1: Once you complete reading or accomplishing this project, it is equally important to know How to Read Data from Thingspeak
NOTE 2: You can also send data to thingspeak without wifi by using GSM module, click here to get the program code and circuit.
If you have any question regarding this project, feel free to ask us. You can anticipate a guaranteed reply from us.