Rich Noordam
Published © LGPL

NodeMCU Web Server with DHT11 data

Need to serve data points for your 'connected' home? This is a very simple way.

BeginnerShowcase (no instructions)1 hour8,721
NodeMCU Web Server with DHT11 data

Things used in this project

Hardware components

9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
breadboard power supply
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

nodeMCU board manager
Arduino IDE
Arduino IDE

Story

Read more

Schematics

NodeMCU_WebServer with DHT11 Data

Code

NodeMCU_DHT11_WebServer

Arduino
code to create static IP based web server serving dht11 data.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

// IP address
IPAddress ip (192, 168, 1, 211);
IPAddress gateway(192, 168, 1, 1);  
IPAddress subnet(255, 255, 255, 0);  
const char* ssid = "YOURSSID";
const char* password = "YOURPASSKEY";

#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// built in LED to show activity
#define led 13

ESP8266WebServer server(80);

String doSensorReading(String sType){
  String returnString = "";
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  
  float f = dht.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);
    char cHumidity[7];
    char cTemperatureF[8];
    char cTemperatureC[8];
    char cHeatIndexF[8];
    char cHeatIndexC[8];
     
    String Humidity, TemperatureF, TemperatureC, HeatIndexF, HeatIndexC;
    dtostrf(h,6,1, cHumidity);
    dtostrf(f,6,1, cTemperatureF);
    dtostrf(t,6,1, cTemperatureC);
    dtostrf(hif,6,1, cHeatIndexF);
    dtostrf(hic,6,1, cHeatIndexC);
    Humidity = String(cHumidity);
    TemperatureF = String(cTemperatureF);
    TemperatureC = String(cTemperatureC);
    HeatIndexF = String(cHeatIndexF);
    HeatIndexC = String(cHeatIndexC);
    if (sType == "")
      returnString = "Humidity: " + Humidity + " %\t" +"Temperature: " + TemperatureC + " *C " + TemperatureF + " *F\t" + "Heat index: " + HeatIndexC + " *C " + HeatIndexF + " *F";
    if (sType == "TemperatureF")
      returnString = TemperatureF;
    if (sType == "TemperatureC")
      returnString = TemperatureC;
    if (sType == "Humidity")
      returnString = Humidity;
    if (sType == "HeatIndexF")
      returnString = HeatIndexF;
    if (sType == "HeatIndexC")
      returnString = HeatIndexC;
      // test stuff   Serial.print("Humidity: ");//    Serial.print(h);//    Serial.print(" %\t");//    Serial.print("Temperature: ");//    Serial.print(t);//    Serial.print(" *C ");//    Serial.print(f);//    Serial.print(" *F\t");//    Serial.print("Heat index: ");//    Serial.print(hic);//    Serial.print(" *C ");//    Serial.print(hif);//    Serial.println(" *F");  
  }
  return returnString;
}
void handleRoot() {
  digitalWrite(led, 1);
  String output = doSensorReading("");
  server.send(200, "text/plain", output );
  Serial.println(output);
  digitalWrite(led, 0);
}

void handleSensor(String sType) {
  digitalWrite(led, 1);
  String output = doSensorReading(sType);
  server.send(200, "text/plain", output );
  Serial.println(output);
  digitalWrite(led, 0);
}

void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup(void){
  
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet); 
 
  Serial.println("");
  dht.begin();

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }
  server.on("/", handleRoot);
  server.on("/TemperatureF", [](){
    handleSensor("TemperatureF");
  });
  server.on("/TemperatureC", [](){
    handleSensor("TemperatureC");
  });
  server.on("/Humidity", [](){
    handleSensor("Humidity");
  });
  server.on("/HeatIndexF", [](){
    handleSensor("HeatIndexF");
  });
  server.on("/HeatIndexC", [](){
    handleSensor("HeatIndexC");
  });
  server.onNotFound(handleNotFound);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  String retVal;
  server.handleClient();
}

Credits

Rich Noordam

Rich Noordam

10 projects • 29 followers
Many interests, computing obviously being one of them. MS SQL Server Database Administration, Reporting, Data Science, and more.

Comments