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.

Watch the Video Tutorial and Project Demo

This guide is available in video format (watch below) and in written format (continue reading).

Prerequisites

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

If you like ESP8266 and you want to learn more about it. I recommend downloading my course: Home Automation using ESP8266.

Parts required

For this project, you’ll need these components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

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
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */
 
#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();
}

View raw code

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. Now, you know how to apply the daily task concept to any project.

This is an excerpt from my Home Automation using ESP8266 eBook. If you like ESP8266 and you want to learn more about it. I recommend downloading my course: Home Automation using ESP8266.

I hope this guide was useful. Thanks for reading!



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

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

    • Hi.
      The !!! means the same as one exclamation mark (!) – this means “NOT”.
      The key idea behind it is to improve visibility for the programmer.
      Thanks 🙂

      Reply
  1. Hello, Sara.

    I’m trying to use ESP8266 to send a push notification via GCM (Google Cloud Message). When I use Client.Connect to send a POST message the method returno FALSE and I do not know why this error occurs. Would you help me?

    I’m using this library:

    github.com/beegee-tokyo/GCM-ESP8266

    Reply
    • Hi Rafael.
      I’ve never used ESP8266 with GCM, so I can’t tell you why that happens.

      Alternatively, you can also try using IFTTT ifttt.com/

      Reply
  2. Olá Rui e Sara
    Obrigado pelo excelente tutorial e parabéns pela ótima qualidade do site.
    Gostaria de montar este mesmo circuito porém para enviar ao ThingSpeak temperatura e umidade a cada hora. Seria possível?

    Reply
  3. By the void makeHTTPRequest, the comment says “// Make an HTTP request to the Node-RED software”, but I thought the goal here was to WRITE to ThingSpeak. Is the comment wrong? Also, does the code “client.print(String(GET “) + response + apiKey {bla bla bla}” write the values to ThingSpeak? I was expecting a POST, and don’t see how the variable “h” and “t” got mapped to &field1 and $field2, or to humidityTemp or temperatureTemp. Can you explain a bit further? Thanks!

    Reply
  4. Hello Sara

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

    Once downloaded what might this library be called when it is listed in the Library Manager?

    Reply
  5. Hi Sara & Rui, thanks a lot for the great tutorials!
    It seems that ThingSpeak slightly changed their setup and the above code now fails. This seems to be related to the spaces in front of the values passed to the field1/field2.
    I overcame the problem by wrapping the humidityTemp and temperatureTemp by String(float(humidityTemp))

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.