IoT Object : ESP8266 + DHT-11 on

      No Comments on IoT Object : ESP8266 + DHT-11 on


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 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!


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

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.


Our object on a PCB

The schematic of the object:

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:
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

If you do not have a ThingSpeak account, start by creating one.
Login to ThingSpeak (

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.

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

You should now see something like that:
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.

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 = "";
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.


void setup() {
  // Initialize sensor

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

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)) {

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)) {

  String url = "/update?key=";
  // 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


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, 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:


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 so you have to go back on that site.
Click the "Apps" button.


Then click the "React" application button.

Click the "New React" button.

Then configure your "React" as this:

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