NodeMCU web server showing DHT11 data

In this example we will connect a DHT11 to a Node MCU v1.0 (you could use any ESP8266 board) board and we will display the temperature and humidity readings on a web page. The web page updates every 5 seconds I wouldn’t recommend changing this to a lower value.

 

Layout

This is the layout, I used D4 for the data in from the DHT11 but you could easily use another IO pin but you would have to change the code example later

 

nodemcu and dht11_bb

 

Code

This is similar to the Arduino version, you will need the DHT library support added.

[codesyntax lang=”cpp”]

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

const char* ssid = "iainhendry";
const char* password = "iain061271";
 
#define DHTPIN D4     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

WiFiServer server(80);
 
void setup() 
{
  Serial.begin(115200);
  // Connect to WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
  dht.begin(); 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
   
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
    
}
 
void loop() 
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
   
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Return the response
   client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connnection: close");
  client.println();
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<meta http-equiv=\"refresh\" content=\"5\">");
  client.println("<br />"); 
  client.println(""); //  do not forget this one
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

          client.print("Temperature (C): ");
          client.println(t, 1);  
          client.println("<br />");  
          
          
          client.print("Humidity (%): ");
          client.println(h, 0);  
          client.println("<br />");   

          client.println("</html>");
  delay(1);
  Serial.println("Client disconnected");

 
}

[/codesyntax]

 

Testing

Open teh Serial monitor and take a note of the IP address.  Navigate to the IP address you see in the serial monitor, you shoudl see something like the following, the temperature and humidity will change every 5 seconds. This can also be changed in the code as well

Temperature (C): 24.0
Humidity (%): 36

 

Links