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!