Shares
Cloud Temperature Logger with the ESP8266

Cloud Temperature Logger with the ESP8266

Last Update: / 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.
ubibotuk 3 months ago
Such a great information, these tips helpful for me to gain knowledge. Experience best monitoring sensors with Ubibot. Visit https://www.ubibot.io/ubibot-ws1/.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Felipe Duarte 2 years 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 2 years 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 3 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 4 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 4 years ago
Hi! Have a look at Dweet.io, you can get all the data that was recorded in JSON :)