Overview
In this tutorial, we will show you how to connect an Arduino UNO R3 with an ESP8266 WiFi module mounted on an ESP-01 adapter. We will also explain the steps for setting up a new channel on ThingSpeak and how to write data on the channel. This tutorial is suitable for beginners who want to learn how to use an Arduino UNO R3 and ESP8266 WiFi module to send data to ThingSpeak and can potentially work as a base to connect any air quality sensor and share data online.
Materials Required:
- Arduino UNO R3
- ESP8266 WiFi module mounted on an ESP8266 ESP-01 adapter
- USB cable
- Breadboard or Prototype Expansion Module (Optional)
- Female to male Dupont wires
The ESP-01 Adapter is used for easier interfacing with the ESP8266 WiFi module, it includes an 3.3V voltage regulator and 5V to 3.3V logic level shifting.
ESP-01 can be wired directly to UNO but in case that you are also considering to connect a sensor, using a breadboard or the prototype expansion module (which can be mounted on top as a shield) allows you to have more space and connections for more sensors.
Wiring Diagram
Wiring Steps
Connect the ESP-01 with ESP8266 WiFi module to the Arduino UNO R3 board. To do this, connect the following pins:
- ESP-01 GND pin to Arduino GND pin
- ESP-01 VCC pin to Arduino 5V pin
- ESP-01 RX Receive Data pin to Arduino pin 11
- ESP-01 TX Transmit Data pin to Arduino pin 10
Setting up ThingSpeak Channel
- Create an account on ThingSpeak
- Create a new channel, on Channel Settings tab you can configure the name, description and fields used on your channel. Each channel can have up to 8 fields
- On Sharing tab, under Channel Sharing Settings select the option “Share channel view with everyone” this will make it public and anyone will be able to see your shared data.
- On API Keys tab you can get the keys to write or send data to the channel.
Writing the code
- Open Arduino IDE on your computer and start a new sketch.
- In this tutorial we’ll only need SoftwareSerial library, if we try to connect ESP-01 RX/TX pins to Uno’s Serial TX/RX it will block the connection between Uno and IDE as the USB serial works on the same pins to communicate to our computer, Software serial allows us to connect ESP-01 through pins 10 and 11.
- Write the following code into your sketch:
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 |
/* Eduardo Martinez Villanueva CPLN 5710 Sensing the City / Weitzman School of Design This sketch was made for: "Monitoring Air Quality data with ThingSpeak using an ESP8266 module" More info on: https://www.sensingthecity.com/ Part of this sketch was adapted from the code and contributions of Christopher Grant https://cgrant.medium.com/using-the-esp8266-wifi-module-with-arduino-uno-publishing-to-thingspeak-99fc77122e82 */ #include <SoftwareSerial.h> //Include this library to allow ESP-01 adapter to interact with UNO using pins 10/11 #define RX 10 // Receive data pin - Connects to TX on UNO #define TX 11 // Transmit data pin - Connects to RX on UNO String AP = "Xfinity-214"; // Write here name of WiFi Network String PASS = "PSA214hh"; // Write here password of WiFi Network String API = "ONHGC66G1GYO40OH"; // Write API KEY from ThingSpeak channel String HOST = "api.thingspeak.com"; String PORT = "80"; // Port number to connect to ThingSpeak server String field = "field1"; // Declare which fields are going to be used on the channel int countTrueCommand; int countTimeCommand; boolean found = false; int valSensor = 1; SoftwareSerial esp8266(RX,TX); int tempPin = 0; // Define pin used by sensor (Optional) void setup() { // Setup code to run once, Serial Monitor runs on 9600 baud, ESP8266 ON 115200 baud Serial.begin(9600); esp8266.begin(115200); sendCommand("AT",5,"OK"); // Communication to ESP8266 is through AT commands, this checks connection with module sendCommand("AT+CWMODE=1",5,"OK"); // Set the WiFi mode 1:Station mode sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"",15,"OK"); // Connect ESP8266 module to targeted WiFi countTrueCommand = 0; } void loop() { //Following code allows to connect and write data to ThingSpeak valSensor = getSensorData(); String getData = "GET /update?api_key="+ API +"&"+ field + "=" +String(valSensor); sendCommand("AT+CIPMUX=1",5,"OK"); sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST + "\"," + PORT,15,"OK"); sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">"); esp8266.println(getData);delay(1500);countTrueCommand++; sendCommand("AT+CIPCLOSE=0",5,"OK"); } int getSensorData(){ return random(1000); // Write your sensor code here, currently this is only a random number generator } void sendCommand(String command, int maxTime, char readReplay[]) { Serial.print(countTrueCommand); Serial.print(". at command => "); Serial.print(command); Serial.print(" "); while(countTimeCommand < (maxTime*1)) { esp8266.println(command);//at+cipsend if(esp8266.find(readReplay))//ok { found = true; break; } countTimeCommand++; } if(found == true) { Serial.println("OYI"); countTrueCommand++; countTimeCommand = 0; } if(found == false) { Serial.println("Fail"); countTrueCommand = 0; countTimeCommand = 0; } found = false; } |
Now we can upload our code into Uno and test how it generates a random number and writes it into our channel, ThingSpeak allows us to share and visualize publicly our data, for our PhillyAir Mist Alert project we will replace this number with air quality sensor data.
One reply on “Monitoring Air Quality data with ThingSpeak using an ESP8266 module”
Very exciting and explained nicely