DHT11 sensor data to Thingspeak using a Wemos D1

In this example we will connect a DHT11 sensor to our Wemos D1, we will then send the temperature and humidity values to thingspeak.

Wemos_D1_ESP8266

DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. Application of a dedicated digital modules collection technology and the temperature and humidity sensing technology, to ensure that the product has high reliability and excellent long-term stability. The sensor includes a resistive sense of wet components and an NTC temperature measurement devices, and connected with a high-performance 8-bit microcontroller.

Typically a DHT11 can be bought in a basic breakout board format but can be bought as a standalone part as well, here is a picture of a typical breakout board, this board holds the sensor and a resistor, the resistor is connected between Vdd (3v3) and the signal pin on the board.

dht11 breakout

If you do not use a module like the one aboveyou can build a similar circuit on a breadboard. Here is a typical schematic, which is basically what the breakout above actually is

 

Dht11_application

You will connect the Gnd of the module (DHt11) to a Gnd on your Wemos board, connect 5v or Vdd of the module (DHt11) to a 3v3 connection on your Wemos and the Sig pin (data pin on the DH11) goes to whatever input you decide to use on your Wemos, in the code below you can see this as

#define DHTPIN 2

 

You will now need to create a new account at thingspeak – https://thingspeak.com. Once done create a new channel and add two new fields, one will hold the temperature reading and the other will hold the humidity reading from our DHT11. This should look something like this

thingspeak channel

thingspeak channel

 

Code

You will need an api key from thingspeak.

#include <DHT.h>
#include <ESP8266WiFi.h>
 
// replace with your channel’s thingspeak API key and your SSID and password
String apiKey = "thingspeak api key";
const char* ssid = "ssid name";
const char* password = "ssid password";
const char* server = "api.thingspeak.com";
 
#define DHTPIN D2
#define DHTTYPE DHT11 
 
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
 
void setup() 
{
Serial.begin(115200);
delay(10);
dht.begin();
 
WiFi.begin(ssid, password);
 
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
 
WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) 
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
 
}
 
void loop() 
{
 
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) 
{
Serial.println("Failed to read from DHT sensor!");
return;
}
 
if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
 
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
 
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celsius Humidity: ");
Serial.print(h);
Serial.println("Sending data to Thingspeak");
}
client.stop();
 
Serial.println("Waiting 20 secs");
// thingspeak needs at least a 15 sec delay between updates
// 20 seconds to be safe
delay(20000);
}

Results

If you login to your thingspeak account you can view live channel data, here is an iframe of some data from my test

32 comments to DHT11 sensor data to Thingspeak using a Wemos D1

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

*