Categories
TUTORIALS

Monitoring Air Quality data with ThingSpeak using an ESP8266 module

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:

  1. Arduino UNO R3
  2. ESP8266 WiFi module mounted on an ESP8266 ESP-01 adapter
  3. USB cable
  4. Breadboard or Prototype Expansion Module (Optional)
  5. 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

ESP8266 WiFi module (Black) and ESP-01 adapter (Blue)
Mount the ESP8266 module on the adapter first before wiring, it should look like this.

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
ESP-01 with ESP8266 mounted over a Prototype Expansion Module.

Setting up ThingSpeak Channel

  1. Create an account on ThingSpeak
  2. 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
  3. 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.
  4. On API Keys tab you can get the keys to write or send data to the channel.
For this tutorial we only checked Field 1 and named it as Random Number

Writing the code

  1. Open Arduino IDE on your computer and start a new sketch.
  2. 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.
  3. Write the following code into your sketch:
/*
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.

Screenshot of how our channel looks like and the graph from the random number

One reply on “Monitoring Air Quality data with ThingSpeak using an ESP8266 module”

Leave a Reply

Your email address will not be published. Required fields are marked *