Design a site like this with WordPress.com
Get started

ESP8266 Daily Task – Publish Temperature Readings to ThingSpeak

In this project you’re going to learn how to build a sensor node with an ESP8266 that publishes the temperature and humidity once a day to a free service called Thing Speak.

Prerequisites

Before proceeding with this tutorial we recommend taking a look at these resources:

Parts required

For this project, you’ll need these components:

  • ESP8266 – read Best ESP8266 Wi-Fi Development Boards
  • DHT11 temperature and humidity sensor
  • 4.7k Ohm resistor
  • Breadboard
  • Jumper wires

Schematics

The circuit is very easy to assemble and no soldering is required, simply follow the next diagram.

Preparing Arduino IDE

Note: you should have the ESP8266 add-on in the Arduino IDE installed.

After having the circuit ready, open your Arduino IDE and install two Arduino libraries. Go to Sketch > Include library > Manage libraries…

Search for DHT (or download here). Press the “Install” button.

Note: you may also need to install the Adafruit Sensor library.

Then, you’ll also have to search for ESP Daily Task and install that library (or download here).

Thing Speak

For this project we’ll be using ThingSpeak.com. Thing Speak allows you to publish your sensor readings to their website and display them in a plot with time stamps. Then, you can access your readings from anywhere in the world.

It’s worth saying that this project can be easily modified to publish the values to your home automation hub or another application instead of Thing Speak.

The main concept I want to show you with this project is how you can put your ESP8266 constantly in deep sleep mode, publishing a value every 24 hours.

Creating New Channel

If you don’t have an account on Thing Speak you’ll have to create one. After your account is ready, sign in and open the “Channels” tab.

Press the create “New Channel” button:

Type a name for your channel, add a description, and enable a second field. Field 1 will receive the Humidity readings and Field 2 the Temperature readings. Once you’ve completed all these fields, save your channel.

Customizing Chart

These charts can be customized, go to your Private View tab and press the edit icon:

You can give each chart a title, for example the first chart will be called Humidity % and my second chart will be called Temperature ºC.

You can also customize the background color, x and y axis, and much more, as you can see. When you’re done, press the “Save” button.

Write API Key

In order to publish values with the ESP8266, you need the Write API Key. Open the “API Keys” tab and copy the Write API Key to a safe place because you’ll need it in a moment.

Uploading Code

Go back to your Arduino IDE and copy the code below:

/*
 * ESP8266 Daily Task
 * Gnd_To_Vcc 
 
 
#include <ESP8266WiFi.h>
#include <ESPDailyTask.h>
#include "DHT.h"

// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Replace with your SSID and Password
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Replace with your unique Thing Speak WRITE API KEY
const char* apiKey = "REPLACE_WITH_YOUR_ThingSpeak_WRITE_API_KEY";

const char* resource = "/update?api_key=";

// Thing Speak API server 
const char* server = "api.thingspeak.com";

// Set this for what time your daily code should run 
ESPDailyTask dailyTask(11*60 + 15); // 11:15am

// DHT Sensor
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Temporary variables
static char temperatureTemp[7];
static char humidityTemp[7];

void setup() {
  // Initializing serial port for debugging purposes
  Serial.begin(115200);
  delay(10);
  
  dailyTask.sleep1Day();
  
  // put your daily code here...
  dht.begin();
  
  initWifi();
  makeHTTPRequest();

  // and back to sleep once daily code is done  
  dailyTask.backToSleep();
}

void loop() {
  // sleeping so wont get here
}

// Establish a Wi-Fi connection with your router
void initWifi() {
  Serial.print("Connecting to: "); 
  Serial.print(ssid);
  WiFi.begin(ssid, password);  

  int timeout = 10 * 4; // 10 seconds
  while(WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if(WiFi.status() != WL_CONNECTED) {
     Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: "); 
  Serial.print(millis());
  Serial.print(", IP address: "); 
  Serial.println(WiFi.localIP());
}

// Make an HTTP request to Thing Speak
void makeHTTPRequest() {
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    strcpy(temperatureTemp,"Failed");
    strcpy(humidityTemp, "Failed");
    return;    
  }
  else {
    // Computes temperature values in Celsius + Fahrenheit and Humidity
    float hic = dht.computeHeatIndex(t, h, false); 
    // Comment the next line, if you prefer to use Fahrenheit      
    dtostrf(hic, 6, 2, temperatureTemp);
                 
    float hif = dht.computeHeatIndex(f, h);
    // Uncomment the next line, if you want to use Fahrenheit
    //dtostrf(hif, 6, 2, temperatureTemp);   
          
    dtostrf(h, 6, 2, humidityTemp);
    // You can delete the following Serial.print's, it's just for debugging purposes
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.print(" *F");
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
  }
  
  Serial.print("Connecting to "); 
  Serial.print(server);
  
  WiFiClient client;
  int retries = 5;
  while(!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if(!!!client.connected()) {
     Serial.println("Failed to connect, going back to sleep");
  }
  
  Serial.print("Request resource: "); 
  Serial.println(resource);
  client.print(String("GET ") + resource + apiKey + "&field1=" + humidityTemp + "&field2=" + temperatureTemp +
                  " HTTP/1.1\r\n" +
                  "Host: " + server + "\r\n" + 
                  "Connection: close\r\n\r\n");
                  
  int timeout = 5 * 10; // 5 seconds             
  while(!!!client.available() && (timeout-- > 0)){
    delay(100);
  }
  if(!!!client.available()) {
     Serial.println("No response, going back to sleep");
  }
  while(client.available()){
    Serial.write(client.read());
  }
  
  Serial.println("\nclosing connection");
  client.stop();
}

Preparing your code

There are 3 variables that you need to change. Add your SSID and password so that your ESP is able to establish a communication with your router:

// Replace with your SSID and Password
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Enter your Thing Speak Write API key in this variable:

// Replace with your unique Thing Speak WRITE API KEY
const char* apiKey = "REPLACE_WITH_YOUR_ThingSpeak_WRITE_API_KEY";

Understanding the code

Now we can take a closer look at the code. The setup() function starts by initializing the Serial port.

void setup() {
 // Initializing serial port for debugging purposes
 Serial.begin(115200);
 delay(10);

Then, it calls the ESP daily task function to determine whether 24 hours have passed or not since the last reading. If not, the ESP goes back to deep sleep mode.

 dailyTask.sleep1Day();

However, if 24 hours have passed since the last reading, the code will proceed. So, it will initialize the DHT sensor, connect to the Wi-Fi router and make an HTTP GET request to Thing Speak in order to publish the readings.

 // put your daily code here...
 dht.begin();

 initWifi();
 makeHTTPRequest();

Once the readings are published, the ESP goes back to deep sleep mode.

 // and back to sleep once daily code is done
 dailyTask.backToSleep();
}

There’s nothing in the loop() function, because your ESP is constantly in deep sleep mode and wakes itself up.

void loop() {
 // sleeping so wont get here
}

Other changes – different sensor and degrees Fahrenheit

Note that If you’re using a different sensor than the DHT11, you can comment and uncomment the right DEFINE line to use your sensor.

// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

There’s also another important thing you can change. By default this code publishes the temperature in degrees Celsius, but if you comment the variable highlighted in red and uncomment the variable highlighted in green it will publish the temperature in degrees Fahrenheit .

// Computes temperature values in Celsius + Fahrenheit and Humidity
float hic = dht.computeHeatIndex(t, h, false);
// Comment the next line, if you prefer to use Fahrenheit
dtostrf(hic, 6, 2, temperatureTemp);

float hif = dht.computeHeatIndex(f, h);
// Uncomment the next line, if you want to use Fahrenheit
//dtostrf(hif, 6, 2, temperatureTemp);

Once you’ve made all the changes, you can upload the code to your ESP board.

Testing the Project

Finally, open the Arduino IDE serial monitor at a baud rate of 115200. You can test your project and simulate each hour passing. Simply remove the wire that connects the RESET pin to D0 (GPIO 16), and then you just have to press the RESET button to simulate each hour passing.

So, you need to press 24 times to publish a reading. The counter variable increases every time you press the reset button. Once you reset the ESP8266 24 times, it will publish the readings to Thing Speak.

As you can see the Humidity and Temperature have been successfully published and plotted in the chart in real time.

After testing the circuit, you should reconnect the wire from RESET to D0. The ESP will wake up every hour, but it will only publish a value every 24 hours.

Wrapping Up

I hope this project was useful.

I hope this guide was useful. Thanks for reading!

Advertisement

Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

6 thoughts on “ESP8266 Daily Task – Publish Temperature Readings to ThingSpeak

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: