IoT Object : ESP8266 + DHT-11 on ThingSpeak.com

      No Comments on IoT Object : ESP8266 + DHT-11 on ThingSpeak.com

ESP-8266-DTH-11-main

There’s a lot of buzz around the Internet of Things (IoT) these days.

This post explains how to create your own IoT object that will read your house air temperature and humidity and send that information to a web site called ThingSpeak.com. Once your data is on ThingSpeak, you’ll be able to visualize it to see how the your house temperature/humidity varies in time. But, even better, you’ll be able to create rules (in ThingSpeak) so you are sent a Twitter message if a data threshold value is hit. In my case, the purpose of this project is to be informed if there’s an issue with my house heating system. If the temperature gets too low, I know my furnace doesn’t do its job properly even if I am not at home!

IoT-HighLevel

Our Internet Object

The Internet object we will create is based on the ESP-8266 wifi module and a DHT-11 sensor breakout. If you don’t have a DHT-11 breakout, but just the sensor, you can easily wire it as on the breadboard example. If you have the breakout and want to do your project on a breadboard, simply use it and remove the resistor between VCC and the sensor signal (yellow) wire. The ESP-8266 will be used to read the DHT-11 sensor values, to connect to our home wifi network and then make REST web service calls to send our data to ThingSpeak.com.

Our object on a breadboard

I simply used my FTDI programmer, as a power source, for the project on a breadboard. Make sure your programmer jumper is configured to 3.3V, not 5V. It can be replaced by another power source if need be.

Thingspeak-IoT-ESP8266-DHT11-Breadboard

Our object on a PCB

The schematic of the object:
IOT-ESP8266-DHT11-Schematics

As you can see, the circuit is very simple. The top portion of the schematic deals with the voltage regulator (3.3V) that we need to power the circuit. The core of the schematic (bottom part) essentially consists into 2 headers, one to connect our ESP-8266 and another one to connect the DHT-11 sensor. Replicating this circuit on a breadboard is also pretty easy if you use a small DIY breadboard adaptor as described in this other post.

The corresponding PCB:
IOT-ESP8266-DHT11-PCB
IOT-ESP8266-DHT11-PCB-REAL
I designed the PCB using Eagle, exported the corresponding GCode and then milled that PCB on my ShapeOko3 CNC as explained in this post.

ThingSpeak Setup

Before we can program our IoT object, we need to do some setup on ThingSpeak.com.

If you do not have a ThingSpeak account, start by creating one.
Login to ThingSpeak (https://www.thingspeak.com).

Then, the first step is to create a channel for our project. In ThingSpeak, a channel is a container for our IoT object data and rules. A channel is identified with a unique identifier that we will have to provide while sending data to ThingSpeak via a REST web service call.

Open the “Channels” menu and select the “My Channels” option.
A new screen will be displayed.
Click the “New Channel” button.
Thingspeak-newchannel

Then, simply populate the “Name” field, the description and the 2 data fields that we are interested into: Temperature and Humidity.
Thingspeak-newchannel-step2
Press the “Save Channel” button.

You should now see something like that:
Thingspeak-newchannel-step3
The 2 graphs are empty since we haven’t sent any data to ThingSpeak yet!

Click the “API Keys” tab. This is where we will get the information we need for our REST web services call: The Channel ID and the Write API key.
Thingspeak-newchannel-step4

We are done with ThingSpeak, for now so you may close the browser if you want.

Programming the ESP-8266

I won’t go into the details about how to upload the firmware on the ESP8266 module because it’s all explained in this post.

In the program, we start by importing the required libraries and define constants for what is likely to change from time to time. This is where you will have to enter your own wifi network name & password and also the channel write API key that you created sooner.

#include < DHT.h >
#include < ESP8266WiFi.h >

#define DHTPIN 2
#define DHTTYPE DHT11

const char* ssid     = "your SSID";
const char* password = "your password";

const char* host = "api.thingspeak.com";
const char* writeAPIKey = "your write API key (RU9B00FLNKY0025I in example)";

const int DELAY_BETWEEN_SENSOR_READ = 60*15;

In the setup function, we initialize the DHT-11 sensor and the wifi connection to our router.

DHT dht(DHTPIN, DHTTYPE, 15);

void setup() {
  // Initialize sensor
  dht.begin();
  delay(3000);

  // We start by connecting to a WiFi network
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

In the main loop, we start by reading our sensor values:

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  if (isnan(humidity) || isnan(temperature)) {
    return;
  }

Then we proceed with a call to ThingSpeak’s web service to send it our data:

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    return;
  }

  String url = "/update?key=";
  url+=writeAPIKey;
  url+="&field1=";
  url+=String(temperature);
  url+="&field2=";
  url+=String(humidity);
  url+="\r\n";
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

And, finally, we wait for 15 minutes before reading the sensor again and sending data to ThingSpeak.

  for(int i = 0; i < DELAY_BETWEEN_SENSOR_READ; i++) {  // delay 15 minutes
    delay(1000);
  }

}

As you noticed, there's not much error handling in that code. When I have some time, I will look at adding some. It should be possible to use the remaining I/O pin to use a LED for debugging. I should also create functions that I would call from the loop() function instead of having all that code in there. All that to say the program is not perfect but it contains all you need to get started!

Testing your object

Power on your object. Just as a side note, if you decided to power your IoT object using a battery, be aware that the ESP-8266 is very power hungry so your battery will not last a long time... If everything goes well, you will soon have some data in ThingSpeak!

To see if it's the case, go back to ThingSpeak.com, click the "Channels" button and you will see your channel.
Click on your channel name and you will get a screen similar to this one:

Thingspeak-newchannel-step5

You are now ready to start analyzing your data and creating rules that will send you Twits when some threshold is reached!

Receive a Twitter message when your house temperature is too low

Now that we have our data in ThingSpeak, we are ready to start using it.
In this example, I want to create a rule that will send me a Twitter message if my home temperature drops to 15 Celsius degrees or less.

This is performed in ThingSpeak.com so you have to go back on that site.
Click the "Apps" button.

Thingspeak-newchannel-step6

Then click the "React" application button.
Thingspeak-newchannel-step7

Click the "New React" button.
Thingspeak-newchannel-step8

Then configure your "React" as this:
Thingspeak-newchannel-step9

If you haven't configured your Twitter account yet, in ThingSpeak, the system will walk you through some simple additional steps for that.

Press the Save button and you are all set!

There are many things that you can do, using your data and ThingSpeak!
I will explore that a little bit and I will probably post other examples in the future.

Let me know what IoT objects you are creating on your side!

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Plugin