Cloud Temperature Logger with the ESP8266

Cloud Temperature Logger with the ESP8266

Last Update: February 12th 2019 / by Marco Schwartz

The ESP8266 is an amazingly cheap & small WiFi chip that can be used for several home automation applications. In this project, we are going to use it to automatically log temperature (and humidity) measurements in the cloud, and display these measurements inside an online dashboard.

By following this tutorial, you will be able to build a small & cheap measurement platform that logs data in the cloud. Of course, this can be applied to several type of sensors, like motion detectors. Let’s dive in!

Hardware & Software Requirements

For this project, you will of course need an ESP8266 chip. You can for example use an Olimex ESP8266 module.

You will also need a temperature sensor. I used a DHT11 sensor, which is very easy to use & that will allow us to measure the ambient temperature & humidity.

You will also need a 3.3V FTDI USB module to program the ESP8266 chip. Finally, you will also need some jumper wires & a breadboard.

This is a list of all the components that will be used in this guide:

On the software side, I will let you see my guide on how to set up the ESP8266 for a first use:

https://www.openhomeautomation.net/getting-started-esp8266/

At the end, you will need to have an ESP8266 chip ready to be used with the ESP8266 Arduino IDE.

Hardware Configuration

Again, for most of the hardware configuration I will redirect you to the article I wrote on setting up your ESP8266 chip:

https://www.openhomeautomation.net/getting-started-esp8266/

Once this is done, simply put the DHT11 sensor on the breadboard. Then, connect the left pin to VCC (red power rail), the right pin to GND (blue power rail), and the pin next to VCC to the GPIO5 pin on your ESP8266 chip. This is the final result, not showing the USB-to-Serial FTDI cables:

Cloud Temperature Logger with the ESP8266

Testing the Sensor

We are now going to test the sensor. Again, remember that we are using the modified version of the Arduino IDE, so we can code just like we would do using an Arduino board. Here, we will simply print the value of the temperature inside the Serial monitor of the Arduino IDE.

This is the complete code for this part:

// Libraries
#include "DHT.h"

// Pin
#define DHTPIN 5

// Use DHT11 sensor
#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);

void setup() {

 // Start Serial 
 Serial.begin(115200); 
 
 // Init DHT 
 dht.begin();
}

void loop() {

 // Reading temperature and humidity
 float h = dht.readHumidity();
 // Read temperature as Celsius
 float t = dht.readTemperature();

 // Display data
 Serial.print("Humidity: "); 
 Serial.print(h);
 Serial.print(" %\t");
 Serial.print("Temperature: "); 
 Serial.print(t);
 Serial.println(" *C ");
 
 // Wait a few seconds between measurements.
 delay(2000);

}

Let’s see the details of the code. You can see that all the measurement part is contained inside the loop() unction, which makes the code inside it repeat every 2 seconds.

Then, we read data from the DHT11 sensor, print the value of the temperature & humidity on the Serial port.

Note that the complete code can also be found inside the GitHub repository of the project:

https://github.com/openhomeautomation/esp8266-cloud

You can now paste this code in the Arduino IDE, and upload it to your board. Then, open the Serial monitor. You should immediately see that the temperature & humidity readings inside the Serial monitor. My sensor was reading around 24 degrees celsius when I tested it, which is a realistic value.

Logging Data to Dweet.io

We are now going to see how to log these measurements in the cloud. This will be really easy to do as our chip is already connected to the web. We will once more use the Dweet.io cloud service that I used in several articles of this website:

http://dweet.io/

As the code for this part is very long, we will only see the important parts here. You can of course get all the code from the GitHub repository for this project. Again all the measurements are done inside the loop() function of the sketch, which makes the code repeat every 10 seconds, using a delay() function.

Inside this loop, we connect to the Dweet.io server with:

WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
  Serial.println("connection failed");
  return;
}

Then, we read the data from the sensor with:

int h = dht.readHumidity();
int t = dht.readTemperature();

After that, we send data to the Dweet.io server with:

client.print(String("GET /dweet/for/myesp8266?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
  "Host: " + host + "\r\n" + 
  "Connection: close\r\n\r\n");

You might want to replace the ‘myesp8266’ here, which is your device name on Dweet.io. Use a complicated name (just like a password) to make sure you are creating an unique device on Dweet.io.

We also print any received data on the serial port with:

while(client.available()){
  String line = client.readStringUntil('\r');
  Serial.print(line);
}

Note that the complete code can also be found inside the GitHub repository of the project:

https://github.com/openhomeautomation/esp8266-cloud

Note that you also need to modify the code to insert your own WiFi network name & password. You can now upload the code to the ESP8266 board, and open the Serial monitor. You should see that every 10 seconds, the request is sent to the Dweet.io server, and you get the answer back:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json
Content-Length: 147
Date: Mon, 16 Mar 2015 10:03:37 GMT
Connection: keep-alive

{“this”:”succeeded”,”by”:”dweeting”,”the”:”dweet”,”with”:{“thing”:”myesp8266″,”created”:”2015-03-16T10:03:37.053Z”,”content”:{“temperature”:24, “humidity”:39}}}

If you can see the ‘succeeded’ message, congratulations, you just logged data in the cloud with your ESP8266 chip!

Display Data Using Freeboard.io

Now, we would like to actually display the recorded data inside a dashboard that can be accessed from anywhere in the world. For that, we are going to use a service that I love to use along with Dweet.io: Freeboard. You can create an account there by going to:

https://www.freeboard.io/

Then, also create a new dashboard, and inside this dashboard, create a new datasource. Link this datasource to your Dweet.io ‘thing’ that you defined in the ESP8266 code:

Cloud Temperature Logger with the ESP8266

Then, create a new ‘Gauge’ widget, that we will use to display the temperature. Give it a name, and link it to the temperature field of our datasource:

Cloud Temperature Logger with the ESP8266

This is the final result:

Cloud Temperature Logger with the ESP8266

You should see that the temperature data coming from the ESP8266 is logged every 10 seconds and is immediately displayed inside the Freeboard.io panel. Congratulations, you should built a very small & cheap temperature sensor that logs data in the cloud! You can then do the same with the humidity measurements & also display them on this dashboard.

How to Go Further

Let’s summarise what we did in this project. We built a simple & cheap WiFi temperature sensor based on the ESP8266 chip. We configured it to automatically log data on the Dweet.io service, and we displayed these measurements inside a dashboard.

There are many ways to improve this project. You can simply add more sensors to the project, and display these measurements as well inside the Freeboard.io dashboard. You can also for example completely change the project, by connecting a motion sensor to the ESP8266 board. You can then configure this sensor to automatically send you an alert when motion is detected, for example via email or using Twitter. The possibilities are endless!

What about you, did you also built cloud-connected projects using the ESP8266 chip? Please share in the comments!

Want to learn more? Get my free eBook about the ESP8266!
The ESP8266 is an amazing WiFi chip that you can use to build all sorts of projects. Download today my free eBook "Build a WiFi Weather Station with the ESP8266". Simply click on the button below!

Leave a Comment

Please enter your name.
Please enter your email.
Please enter your comment.
Felipe Duarte a year ago
Marco, How can I send info from freeboard to a ESP8266 in order to turn on a led if a temperature get a especific number. I could make some dashboards receiving info from esp8266 but I can´t figure out how to do in the oposite way. Thanks in Advance.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Sidd a year ago
Can we use freeboard to send data back to the controller?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Darryl Mittlestaedt 2 years ago
I'm working on a WiFi weather station using a Adafruit Feather Huzzah ESP8266 and I would like to send my data results to a cloud based MongoDB at mLab and then pull the data from there into my own website.I would like to ask if you would you be able to suggest a good way to do this and as a suggestion I think this would be a great example project for this website since most examples out don't give to much on feeding data to cloud based storage as MongoDB on mLab. (or least of what I have found)Also would you have any examples on doing something like this?Thank you!
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Tejas Shah 2 years ago
Hii i am tejas i need to know how to get data from any cloud using wemos d1 mini??
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
JeffPowell 2 years ago
Or you could literally just buy two components from wemos and have it up and running for $6
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Walter Lippens 2 years ago
Hi Marco,Great tutorial. I'm just a bit confused about the method for sending the read data to the dweet server. I was rather expecting a 'POST' instead of a 'GET'.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
MSK 3 years ago
Hi Marco,Am just beginning to explore IOT with different boards, Am looking at something like Controlling board(Adafruit huzzah Esp8266) with android mobile with some cloud(Azure) backend. Any pointers would be helpful.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz MSK 3 years ago
I recommend the Adafruit Feather ESP8266 board, it's the easiest to use in my opinion
Shwartz Samas 3 years ago
hello, it is a great projet , thanks you ! i want to now what i must change in this code if i send data to server of my university !! ikeep it the same and i change the adresse of web ! please help me
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Shwartz Samas 3 years ago
Hi, thanks! You basically need to change the URL, but also adapt the code so the server at your university can log the data.
Art Holder 3 years ago
Works if i use a static numbers for the temp and humidity however the data read from the DTH11 are both the same for the Temp and Humidity and these are are huge number eg "content":{"temperature":2147483647,"humidity":2147483647},"can anyone please help with a solution to fix this.Much appreciatedProblem Solved Changed DHT11 2x them working
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Louis Tjhin Art Holder 2 years ago
Hi Art Holder..I am having the same problem, but didn't quite understand what you did to solve it. Do you mind explaining in more details? Thank you very much.
soumil nitin 3 years ago
hie how to export data from freboard??? ty for wonderful tutorialfile which i am trying to export is in .json formatwhat to do ?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz soumil nitin 3 years ago
Hi! Have a look at Dweet.io, you can get all the data that was recorded in JSON :)
ajbellor 3 years ago
Hello MarcóI tried this project with a ESP8206 -01. I only change the I put of the DHT to the pin 2. And I get always the same error: signal : ilegal instructio. I load your file and the same. I have all libraries and follow your step very carefully. What could be happening ?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz ajbellor 3 years ago
hello! I'd make sure you have the correct library for DHT sensor (you need the one from Adafruit), there are many versions out there.
satish 3 years ago
Marco , Do you have any tutorial similar way using Thinkspeak ? Kindly share the link for same. It will be of great help.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz satish 3 years ago
Not yet, but that's a good idea!
satish 3 years ago
Thanks Marco , such a good tutorial , got my "thing" running , thanks once again !!Is there any possibility of free key by which we can try generating alerts.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz satish 3 years ago
You're welcome! You should see that directly with Dweet.io :)
rogera 3 years ago
Thanks for all of the great info here! I've done a decent bit of embedded but I'm very new to programming 'off the metal' and just starting to learn IOT. I have a project in mind where I'd like to be able to remotely monitor some signals in addition to controlling some outputs. In all of the examples I've found it seems to be either one or the other (i.e. sending data or receiving commands, but not both). Is there a relatively simple way to use an ESP8266 for bi-directional comms from an online server? I saw your great post on Adafruit that was along these lines, but in that case you used 2 separate micros...Is that necessary or was it purely done to simplify things?Thanks!
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz rogera 3 years ago
Hello Rogera! Well now yes :) I just released a cloud update for my aREST library, you can find a basic tutorial for the ESP8266 here: http://arest.io/esp8266-cloud/
m_ahmad 3 years ago
is it possible to connect esp8266 with arduino+gsm shield? i would like to add an extra alert method to my home security project.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz m_ahmad 3 years ago
Sure, it's complex but it's definitely possible
Darshan Mestry 3 years ago
hey . I have included ESP8266WiFi.h but when for compiling i am getting error as /root/sketchbook/libraries/ESP8266WiFi/WiFiClient.h fatal error memory No such file or directory #include <memory>how to remove this error?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Darshan Mestry 3 years ago
You don't need to include any file manually, whenever you select the ESP8266 board the library will be available. Make sure you have the latest version of the Arduino IDE & the ESP8266 board installed via the board manager.
UnNamed Aussie 4 years ago
YAY! i got my 'thing' running!!! thanks Mark, such a good tutorial! going to try to add a Baro sensor - i hopefully just add the sensor, add a library, change the sketch (sensor and dweet strings), then upload to the chip?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz UnNamed Aussie 4 years ago
You're welcome! Yes indeed that's the way to go.
JT 4 years ago
Hi Marco,Nice article.1. will freeboard.io be able to display past temperature data points? I can only see temperature the moment I open up the site and keep the site open.2. How to I store dweets in local server so that I'm not limited to number of data? Do you have tutorial for that?Thank you!
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz JT 4 years ago
Hey JT!1. Sure! It's called "Sparkline" in the interface2. No, I don't have a tutorial for that, but I would look at Node.js to build such a server.
Shares
Cloud Temperature Logger with the ESP8266 Cloud Temperature Logger with the ESP8266 Cloud Temperature Logger with the ESP8266 Cloud Temperature Logger with the ESP8266 Cloud Temperature Logger with the ESP8266 Cloud Temperature Logger with the ESP8266