Random Nerd Tutorials
Shares

ESP8266 Daily Task – Publish Temperature Readings to ThingSpeak

Shares

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:

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!


20 Easy Raspberry Pi Projects

Our new book is a beginner-friendly collection of electronics projects, perfectly suited for hobbyists, parents, kids, parents, and educators looking to level up their hardware skills - GET THE BOOK »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

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

Leave a Comment:

Add Your Reply