This is the second part of the series on IoT data logging with the ESP8266 WiFi module. I’ll discuss connecting it to an Arduino, Hardware and Software Serial, debugging with Serial Monitor, and uploading the data to the cloud.
From the previous article, to make my IoT device, I used:
- Arduino UNO board (or any higher version board)
- ESP8266 12E WiFi module
- Any Arduino cellphone charger
- CP2102 USB to Serial converter (for debugging)
- Breadboard (for prototyping)
- Grove VOC sensor (from SeeedStudio)
The TX and RX pins on the ESP8266 are the ones used for Hardware Serial with pins 0 and 1 on the UNO, and pins 10 and 11 are used for Software Serial with the CP2102. The 5V and GND lines are connected to the cellphone charger. The sensor is connected to A0, and to 5V and GND.
The Arduino UNO which I used has only one set of ports for Hardware Serial communication, ie, the digital pins 0 and 1. On the other hand, boards like the Arduino Mega 2560 have four Hardware Serial ports. These ports are also called UARTs (universal asynchronous receiver/transmitter) and the CP2102 is also called a USB to UART converter.
For debugging the communication between the ESP8266 and the Arduino, we can either let them talk over Software Serial and debug directly using Arduino’s Serial Monitor, or use Hardware Serial and use Software Serial to debug indirectly with the CP2102 converter. The speed of Software Serial is limited to 19200 baud. Since the ESP8266 uses 115200 baud, we need to use Hardware Serial for it. For debugging this communication, we can use a Software Serial on any two pins other than 0 and 1. So I used ports 10 and 11, connected them to the TX and RX of the USB to UART converter, and interpreted the output from Arduino Serial Monitor.
The ESP’s TX and RX need to be connected to the Arduino’s RX and TX directly (my module has the level shifter circuit built in, else use MOSFET based level shifting circuits to prevent damage to either component). The other connections include the standard 5V connection from the cellphone charger’s DC input to the Arduino as well as to the ESP. It is important to ensure that they both share the same 5V line and the ground line, or the communication will not work properly.
Disconnect the wires to Arduino’s RX and TX (pins 0 and 1) while uploading the code. If you forget to do this, the Arduino IDE will not be able to upload the code, and you will get an error saying avrdude: stk500_getsync(): not in sync. This is easily fixed by unplugging the Arduino, and the wires to RX and TX, and trying again.
The code works only on ESP8266 boards with the IoT firmware, and will NOT work on boards with the NodeMCU or AI-Thinker firmware. If you would like to flash the IoT firmware on your own board, have a look at the next article.
The code also includes a VOC sensor which I obtained from SeeedStudio. It will work for any generic analog sensor, whose signal pin you can connect to A0 on the Arduino UNO (some code modification may be required). Make sure to modify the API key, the WiFi SSID and WiFi password.
After powering everything up, you can connect the CP2102 to your computer and see the debugging output as the ESP8266 resets, connects to your WiFi network, then enters a loop, which measures the voltage from the sensor and uploads it to ThingSpeak every few seconds. Having convinced yourself that it is working fine, you can turn it off and disconnect the USB to UART converter. Then take the Arduino and ESP8266 to any WiFi connected area of your home, power them up again, and they will upload the sensor data at regular intervals as before.
To remove its dependency on the AC power supply (cellphone charger), I am planning to connect it to a portable battery pack (power bank). Most power banks available today can supply between 1A-2.1A of current at 5V, which is more than enough for the ESP8266.
So there you have it! A neat, portable data logger, which uploads data wirelessly to the cloud, so that you can access it from anywhere in the world.
Well, I could move to a smaller Arduino board and put all the necessary parts onto a PCB to save space. This would also drive down the cost. However, the ESP8266 community has integrated this board into the Arduino IDE, so it can be programmed directly. This means I can upload a program to the micro-controller on the ESP8266 board, and it will run the program I want, and I can use the ESP’s 16 GPIO pins for connecting my sensors. The ESP is more capable than an Arduino, all by itself! So I could just connect the ESP to a power bank and a sensor and set it up somewhere, and it would do everything all by itself!
Programming the ESP8266 directly, coming up in the next article! Hope you liked this one! Don’t forget to subscribe for updates to my blog, and comment if you have any questions.