ESP8266 and ESP32 together on the same IoT Platform ThingSpeak
Table of Contents
ESP8266 And ESP32 with ThingSpeak, Description:
ESP8266 and ESP32 Together on the same IoT Platform ThingSpeak- I have been using Nodemcu ESP8266 and ESP32 modules for quite a long time with different IoT platforms including the Blynk, Ubidots, Google Firebase and Thingspeak.
While using the Blynk application you are limited to use only one type of the module, you can select the ESP8266 or you can select the ESP32 Dev Board from the device list as you can see in the image given above. With Blynk application you can use multiple devices but of the same type.
There is an article, in which I explained how to use multiple Nodemcu modules with the same Blynk application. This project can really help you in designing advanced level projects where you need to monitor multiple devices installed at different locations.
So, if you plan to use the same IoT supported devices then go ahead use blynk. But if you plan to use different modules, then you will need to switch to another IoT Platform Ubidots or ThingSpeak. Before, I am going to explain anything, first, a few words about the Sponsor of this video.
About the Sponsor PCBWay:
High quality & Only 24 Hours Build time
Download PCB Boards Gerber Files:
The Nodemcu ESP8266 and ESP32 power supply PCB boards used in this project are sponsored by the PCBWay Company. PCBWay is quite professional in the field of PCB manufacturing; you can try their services at extremely low prices, Only 5 dollars for 10 PCBs and 30 dollars in total for 20 PCBs assembly, besides this the new members also get a 5 Dollars bonus. The Gerber files of the PCB boards used in this project can be downloaded from the PCBWay official website; the Gerber files download links are given above.
In this tutorial, you will learn how to use the Nodemcu ESP8266 and ESP32 Wifi + Bluetooth module with the ThingSpeak IoT platform. A potentiometer is connected with the Nodemcu ESP8266 WiFi Module.
While the DHT11 Temperature and Humidity Sensor is connected with the ESP32 module.
If you look closely you will find, both the boards are approximately the same. As I am using the same power supply which is based on the LM7805 Voltage Regulator.
As I am using LM7805 Linear Voltage regulators and as you know the LM7805 accepts a wide range of input voltages from 7 to 28 volts. So these circuits can be easily powered up using 12 Volts adaptors.
As soon as you power up the circuits, the Nodemcu ESP8266 and ESP32 connects with the WiFi in no time. Then you can wait for a few seconds, as the values are updated approximately after 20 seconds.
Field1 consists of the Potentiometer value, by rotating the Knob of the Potentiometer different values are sent to the ThingSpeak field1 where these values are displayed on the chart.
Field2 and Field3 consist of the Humidity and Temperature values respectively. It takes approximately 20 seconds to update the values, and this is the reason I prefer Ubidots over the ThingSpeak IoT platform. Because on Ubidots, the values are updated every 1 second. But any how I will continue with the ThingSpeak as I have already used the Ubidots IoT platform.
I think, I have talked enough about this project, and I am sure now you have the complete idea what are we going to make.
Without any further delay, let’s get started!!!
Amazon Purchase Links:
ESP32 WiFi + Bluetooth Module(Recommended)
DHT11 Temperature and Humidity Module:
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
If you have never used the ESP8266 and ESP32 modules then I highly recommend watch my previous tutorials, in which I have explained how to install the ESP32 and Nodemcu ESP8266 boards.
As you know in this project two circuits are used, one is based on the Nodemcu ESP8266 Wifi Module, while the other one is based on the ESP32 WiFi + Bluetooth Module. So, first let’s start with the circuit diagram of the Nodemcu ESP8266.
Nodemcu ESP8266 and Potentiometer Circuit Diagram:
Let’s first of all, start with the 5v regulated Power supply which is used to power up the Nodemcu esp8266 wifi module. This Power Supply is based on the famous LM7805 voltage regulator. J1 is the female power jack and this is where we connect a 12v adaptor, battery or a solar panel. Two 470uf decoupling capacitors are connected at the input and output sides of the voltage regulator. A 330 ohm resistor is connected in series with a 2.5v led. This is a current limiting resistor. The output of the voltage regulator is connected with the Vin pin of the Nodemcu esp8266 wifi module and the ground is connected with the ground. SV1 to SV7 are the female headers.
The middle leg of the potentiometer or variable resistor is connected with the Analog pin A0 of the Nodemcu ESP8266 Wifi Module. While the other two legs are connected with the 3.3v and ground pins of the Nodemcu Module. The pushbutton is not used in this project.
ESP32 and DHT11 Circuit Diagram:
The 5V regulated power supply remains the same. The DHT11 Temperature and Humidity Module VDD pin is connected with the ESP32 module 3.3V pin. The DATA pin of the DHT11 sensor is connected with the GPIO15. Pin 3 of the DHT11 sensor is not connected, while the last pin of the DHT11 sensor is connected with the ground.
Creating a channel on ThingSpeak IoT Platform:
While your ThingSpeak account is open, click on the New Channel button.
Enter your channel Name. Write some description if you want, this is optional. I will need three fields one for the Potentiometer and the other two for the Humidity and Temperature. Scroll down and click on the Save Channel button. This will take you to the other page.
As you can see the three charts are added. Now the next step is to click on the API Keys.
Copy the Write API Key and paste it in the Nodemcu ESP8266 and ESP32 programs.
Next copy the Channel ID and paste in the two programs.
No matter how many IoT support devices you add, you will need to use the same Write API Key and the Channel ID. Our channel is created and now ready for the use. Now let’s have a look at the programs.
ESP8266 and ESP32 Programming ThingSpeak:
In this project, two Programs are used one program is written for the Nodemcu ESP8266 while the other program is written for the ESP32 Module. Before, you start the programming first of all; make sure you download all the necessary libraries.
Nodemcu ESP8266 ThingSpeak Programming:
Secrets.h File:
1 2 3 4 5 6 7 8 |
// Use this file to store all of the private credentials // and connection details #define SECRET_SSID "AndroidAP7DF8" // replace MySSID with your WiFi network name #define SECRET_PASS "jamshaid" // replace MyPassword with your WiFi password #define SECRET_CH_ID 1098173 // replace 0000000 with your channel number #define SECRET_WRITE_APIKEY "6WS8FFV9JIW83H4F" // replace XYZ with your channel write API Key |
Copy the above code and paste it into the Arduino IDE and save it as the secrets.h file.
Main Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
/* * ESP32 and DHT11 Temperature and Humidity Sensor with the Thingspeak IoT Platform * https://www.electroniclinic.com/ * Download Libraries * https://www.electroniclinic.com/arduino-libraries-download-and-projects-they-are-used-in-project-codes/ * This example is for FirebaseESP32 Arduino library v 3.7.3 and later * */ #include <WiFi.h> #include "DHTesp.h" #include "ThingSpeak.h" #include "secrets.h" #include<stdlib.h> #define DHTpin 15 DHTesp dht; char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) WiFiClient client; unsigned long myChannelNumber = SECRET_CH_ID; const char * myWriteAPIKey = SECRET_WRITE_APIKEY; // Initialize our values int number1 = 0; int number2 = random(0,100); int number3 = random(0,100); int number4 = random(0,100); String myStatus = ""; String mytemp; String myhum; void setup() { Serial.begin(115200); dht.setup(DHTpin, DHTesp::DHT11); WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak } void loop() { // Connect or reconnect to WiFi if(WiFi.status() != WL_CONNECTED){ Serial.print("Attempting to connect to SSID: "); Serial.println(SECRET_SSID); while(WiFi.status() != WL_CONNECTED){ WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network Serial.print("."); delay(5000); } Serial.println("\nConnected."); } delay(dht.getMinimumSamplingPeriod()); float humidity = dht.getHumidity(); float temperature = dht.getTemperature(); // set the fields with the values ThingSpeak.setField(2, humidity); ThingSpeak.setField(3, temperature); Serial.println(myhum); Serial.println(mytemp); // set the status ThingSpeak.setStatus(myStatus); // write to the ThingSpeak channel int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); if(x == 200){ Serial.println("Channel update successful."); } else{ Serial.println("Problem updating channel. HTTP error code " + String(x)); } // change the values number1++; if(number1 > 99){ number1 = 0; } number2 = random(0,100); number3 = random(0,100); number4 = random(0,100); delay(20000); // Wait 20 seconds to update the channel again } |
Make sure both the files are in the same folder.
The secrets.h file will remain the same, make sure you copy and paste this file with the ESP32 main code i.e both the files should be in the same folder, as we did for the Nodemcu ESP8266.
ESP32 ThingSpeak Programming:
Secrets.h code:
1 2 3 4 5 6 7 8 |
// Use this file to store all of the private credentials // and connection details #define SECRET_SSID "AndroidAP7DF8" // replace MySSID with your WiFi network name #define SECRET_PASS "jamshaid" // replace MyPassword with your WiFi password #define SECRET_CH_ID 1098173 // replace 0000000 with your channel number #define SECRET_WRITE_APIKEY "6WS8FFV9JIW83H4F" // replace XYZ with your channel write API Key |
ESP32 ThingSpeak Main Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
//https://www.electroniclinic.com/ // Download Libraries: https://www.electroniclinic.com/arduino-libraries-download-and-projects-they-are-used-in-project-codes/ #include "ThingSpeak.h" #include "secrets.h" #include <ESP8266WiFi.h> char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) WiFiClient client; unsigned long myChannelNumber = SECRET_CH_ID; const char * myWriteAPIKey = SECRET_WRITE_APIKEY; // Initialize our values int number1 = 0; int number2 = random(0,100); int number3 = random(0,100); int number4 = random(0,100); String myStatus = ""; // sensor int Pot = A0; void setup() { Serial.begin(115200); // Initialize serial pinMode(Pot,INPUT); WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak } void loop() { // Connect or reconnect to WiFi if(WiFi.status() != WL_CONNECTED){ Serial.print("Attempting to connect to SSID: "); Serial.println(SECRET_SSID); while(WiFi.status() != WL_CONNECTED){ WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network Serial.print("."); delay(5000); } Serial.println("\nConnected."); } int data = map(analogRead(Pot),0,1023,0,255); // set the fields with the values ThingSpeak.setField(1, data); // set the status ThingSpeak.setStatus(myStatus); // write to the ThingSpeak channel int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); if(x == 200){ Serial.println("Channel update successful."); } else{ Serial.println("Problem updating channel. HTTP error code " + String(x)); } // change the values number1++; if(number1 > 99){ number1 = 0; } number2 = random(0,100); number3 = random(0,100); number4 = random(0,100); delay(20000); // Wait 20 seconds to update the channel again } |
I have tried my best to keep the coding simple; both the programs are exactly the same. The only difference is, in the ESP32 programming I added a library for the DHT11 sensor, and defined some variables for the temperature and humidity values.
The most important thing that you need to take care of is, never use the same fields. As you can see in the Nodemcu programming, I have used the field 1 for the Potentiometer, and in the ESP32 programming, I used field 2 and field 3 for the Humidity and Temperature Values. So that’s all about the programming. Now the final step is to upload the programs.
I tested this project so many times and it worked without any errors. If you have any questions regarding this project, let me know in a comment. Don’t forget to Subscribe to my Website and YouTube Channel “Electronic Clinic”.
Watch Video Tutorial: