In this post we are going to learn how to send data to Thingspeak IoT platform using ESP8266 and Arduino. In the previous tutorial we saw how to send data to thingspeak using GSM modem and Arduino which didn’t need a Wi-Fi hotspot. In this post we will learn how to connect your Arduino project to Wi-Fi and observe data in real time on Thingspeak.
We will see:
- Quick overview: What we are going to do?
- 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?
- How to install ESP8266 board package to Arduino IDE?
- Circuit connection for ESP8266 to Arduino.
- Possible errors while uploading code to ESP8266 and solution.
- Final circuit diagram for sending temperature and humidity data to thingspeak.
- Program codes.
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 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 are uploading two sets of program code: one for Arduino and another for ESP8266 module. We will be utilizing the same Arduino board to upload 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 standalone application or act as slave device that is connected to a microcontroller (master) to enable internet connection wirelessly for the intended project.
Beginners might think that ESP8266 module as a slave device and always need a microcontroller to operate, but that is not true. The ESP8266 can operate as standalone device; it has its own 32-bit microcontroller clocked at 80MHz and flash memory which can be programmed using Arduino IDE.
But this module has only two GPIO pins which will limit our number of sensor and peripherals which we are going to connect. If you are going to use only one sensor (connected to a single pin) then you may not need any microcontroller, you can directly add the library (if necessary) and write code as you do for arduino boards and upload it to ESP8266 module.
As we need more than two pins, we are using ESP8266 as 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 code to ESP8266 using Arduino Uno board in detail later in the article.
Now let’s take a look at its specifications of ESP8266.
- ESP8266 module has its own SoC or system on chip and flash memory etc. 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 up to 72.2 Mbps.
- It supports these network protocols: IPv4, TCP/UDP/HTTP.
Pin Diagram of ESP8266:
The pin diagram is also printed on the back of the ESP8266 module, which makes our tasks much easier.
By now you would have got a small idea on ESP8266 module and you may have understood that we have to upload a program code to ESP8266 to make it operational either as standalone or as slave device.
Now we have to send some data to a server so that we can view the sent data anywhere in the world, which is the objective of this tutorial. We have chosen Thingspeak IoT platform for sending and retrieving the data.
What is Thingspeak platform, how to create an account and get API key?
If you intentionally landed this post, there is a good chance that you may already know what thingspeak platform is and you may already have an account and 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
Please follow channel setting instruction and replicate to your account as we are going to do the same here as in the mentioned tutorial.
How to upload code to ESP8266?
As we mentioned at the beginning of the 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 on Arduino IDE and we are going to use Arduino Uno board’s build-in programmer to upload the code to ESP8266 by doing which the ESP8266 will accept serial data which will be sent to Thingspeak.
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 supply to ESP8266 as it operates on 3.3V and 5V will kill the board.
The ESP8266 will get slightly warm after connecting to the supply which is normal but, if it gets very hot then you probably messed with wire connections/supply voltage and it may already got damaged even before you realize.
Once the hardware is ready we need to install the ESP8266 board package, now let’s see how to do that. We will tell you the function the 2 buttons once the board package is installed to your IDE.
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, which is at top of all options.
- Now a window will popup:
- Type ESP8266 on the box as shown and you will get an installation option, select the latest version and click install.
- Now the IDE will download the necessary package and this will take more than 5 minute to complete.
- Now go to Tools > Board > select “Generic ESP8266 Module” and you are done.
- Now copy the ESP8266 program code given below and paste it on the IDE change the SSID and Password in code with of your home’s Wi-Fi or create a mobile hotspot from your smartphone. Copy the “write API key” from your thingspeak account and paste it on the code.
- Now press compile button (Green tick button). The compilation of code may take more than couple of minutes so be patient. If compilation failed please check that you have selected the “Generic ESP8266 Module” in the board option.
- After the successful compilation of the code, connect the Arduino to your PC and go to “Tools” > “Port” and make sure that Arduino Uno is selected or the correct COM port.
- Now Press and hold the FLASH button and press the RESET button once and leave the FLASH button. By doing this ESP8266 will know that a new program is going to upload to its memory.
- Now click the upload button (Green arrow button), the code starts to compile again (it takes 2 minutes) and uploading to ESP8266 will also take another 2 minutes, again be patient.
- once the code is uploaded successfully it looks something like this:
- The orange coloured text is not error or warning, the uploading progress bar will not be similar to what see while uploading a code to the Arduino Uno board.
- Now the code is successfully uploaded to ESP8266 Module and now will accept the data serially.
Program code for ESP8266:
Possible Errors you will get while uploading to ESP8266:
There is good chance that you will fail to upload the code in the first attempt, here are the possible errors you will 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 order.
- You did not connect the TX and RX terminals properly. Remember the Tx should go to Tx and Rx should go to Rx and not any other way. Also check all the wire connections.
- The board is damaged because you applied 5V to the board mistakenly before, the only possible solution is to replace the ESP8266 module.
- If your code did not compile successfully in the first place, you did not select the correct board. Go to Tools > Board > select “Generic ESP8266 Module”.
Explanation for the ESP8266 Code:
The code will accept data if it finds “*” and “#” in the incoming serial. “*” is the start character and “#” is the end character. The below code accepts 4 integer data after the “*” and stop accepting after the “#”.
The incoming data should be like this *2345# here, 23 is temperature and 45 is humidity. You can open serial monitor with the same 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 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 will kill the module.
Download this DHT library for DHT11 sensor: https://drive.google.com/open?id=1aWuF115pMaZbySQS2CIHBxJiPSM1ezhE
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 in the 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 have any question regarding this project, feel free to ask us. You can anticipate a guaranteed reply from us.