Esp8266 Dht11/Dht22 Temperature And Humidity Web Server With Arduino Ide With Code Examples

  • Updated
  • Posted in Programming
  • 6 mins read


Esp8266 Dht11/Dht22 Temperature And Humidity Web Server With Arduino Ide With Code Examples

This article will present you, through a collection of examples, how one can repair the Esp8266 Dht11/Dht22 Temperature And Humidity Web Server With Arduino Ide drawback that happens in code.

/*********
  Rui Santos
  Complete challenge particulars at https://randomnerdtutorials.com  
*********/

// Import required libraries
#embrace "WiFi.h"
#embrace "ESPAsyncWebServer.h"
#embrace <Adafruit_Sensor.h>
#embrace <DHT.h>

// Replace together with your community credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

#outline DHTPIN 27     // Digital pin related to the DHT sensor

// Uncomment the kind of sensor in use:
//#outline DHTTYPE    DHT11     // DHT 11
#outline DHTTYPE    DHT22     // DHT 22 (AM2302)
//#outline DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String readDHTTemperature() {
  // Sensor readings can also be as much as 2 seconds 'previous' (its a really gradual sensor)
  // Read temperature as Celsius (the default)
  float t = dht.learnTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float t = dht.learnTemperature(true);
  // Check if any reads failed and exit early (to attempt once more).
  if (isnan(t)) {    
    Serial.println("Failed to learn from DHT sensor!");
    return "--";
  }
  else {
    Serial.println(t);
    return String(t);
  }
}

String readDHTHumidity() {
  // Sensor readings can also be as much as 2 seconds 'previous' (its a really gradual sensor)
  float h = dht.learnHumidity();
  if (isnan(h)) {
    Serial.println("Failed to learn from DHT sensor!");
    return "--";
  }
  else {
    Serial.println(h);
    return String(h);
  }
}

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta title="viewport" content material="width=device-width, initial-scale=1">
  <hyperlink rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="nameless">
  <fashion>
    html {
     font-family: Arial;
     show: inline-block;
     margin: 0px auto;
     text-align: middle;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .models { font-size: 1.2rem; }
    .dht-labels{
      font-size: 1.5rem;
      vertical-align:center;
      padding-bottom: 15px;
    }
  </fashion>
</head>
<physique>
  <h2>ESP32 DHT Server</h2>
  <p>
    <i class="fas fa-thermometer-half" fashion="shade:#059e8a;"></i> 
    <span class="dht-labels">Temperature</span> 
    <span id="temperature">%TEMPERATURE%</span>
    <sup class="models">&deg;C</sup>
  </p>
  <p>
    <i class="fas fa-tint" fashion="shade:#00add6;"></i> 
    <span class="dht-labels">Humidity</span>
    <span id="humidity">%HUMIDITY%</span>
    <sup class="models">&percnt;</sup>
  </p>
</physique>
<script>
setInterval(perform ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = perform() {
    if (this.readyState == 4 && this.standing == 200) {
      doc.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.ship();
}, 10000 ) ;

setInterval(perform ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = perform() {
    if (this.readyState == 4 && this.standing == 200) {
      doc.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.ship();
}, 10000 ) ;
</script>
</html>)rawliteral";

// Replaces placeholder with DHT values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return readDHTTemperature();
  }
  else if(var == "HUMIDITY"){
    return readDHTHumidity();
  }
  return String();
}

void setup(){
  // Serial port for debugging functions
  Serial.start(115200);

  dht.start();
  
  // Connect to Wi-Fi
  WiFi.start(ssid, password);
  whereas (WiFi.standing() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / internet web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "textual content/html", index_html, processor);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "textual content/plain", readDHTTemperature().c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "textual content/plain", readDHTHumidity().c_str());
  });

  // Start server
  server.start();
}
 
void loop(){
  
}

Utilizing a variety of various examples allowed the Esp8266 Dht11/Dht22 Temperature And Humidity Web Server With Arduino Ide drawback to be resolved efficiently.

Is DHT22 higher than DHT11?

The DHT22 outshines the DHT11 in each facet from temperature vary, temperature accuracy, humidity vary to humidity accuracy. The solely draw back of the DHT22 is, after all, the marginally greater value however you’re paying for the higher specs.20-Apr-2020

How do I ship DHT11 information to Thingspeak utilizing ESP8266?

Embed the widget by yourself web site

  • Get PCBs for Your Project Manufactured.
  • Looking on the Thingspeak Platform.
  • MQTT by Mosquitto.
  • Setting up the ESP8266 for the assessments.
  • Download and setup the Arduino IDE.
  • Coding the module.
  • The ESP8266 sends information to Thingspeak.

How does DHT11 calculate temperature and humidity?

Introduction: Measuring Humidity Using Sensor (DHT11)

  • Relative Humidity = (density of water vapor / density of water vapor at saturation) x 100%
  • Absolute=Mass(vapour) / quantity.
  • Specific:Mass(vapour) / complete mass.
  • Dew Point:Temperature(above 0°C) at which the water vapor in a gasoline condenses to liquid water)

Can DHT22 work with 3.3 V?

The DHT22 has 4 pins however solely three are used. Pin 1 is for energy which with most purposes will likely be 5v – the specification is from 3v to 6v however in testing, I’ve discovered that beneath 5v the information output will be unreliable. Most boards it’s seemingly for use with supply each 3.3v and 5v.

Is DHT11 waterproof?

DHT22 is healthier then DHT11, nevertheless it is not a water-resistant sensor, verify DS18B20 for waterproof temperature sensor.

What is the vary of DHT11 sensor?

from 0 to 50 levels Celsius

How do I ship information to ThingSpeak utilizing ESP8266 and arduino?

ESP8266 IOT Using Arduino and ThingSpeak

  • Step 1: Materials You Need. You will want:
  • Step 2: Setup. Connect SFE Duino and ESP-8266 Wirelss Module with jumper wires as schematic above.
  • Step 3: Connect With ThingSpeak.
  • Step 4: Code.
  • Step 5: Monitor It!
  • 19 Comments.

How do I get information from arduino to my web site?

Working Explanation: First of all we have to join our Wi-Fi module to Wi-Fi router for community connectivity. Then we are going to Configure the native server, Send the information to Web and eventually Close the connection.

How join ESP8266 to arduino?

To hook every little thing up, join these wires:

  • Connect TX on the ESP8266 to TX on the Arduino Uno.
  • Connect RX on the ESP8266 to RX on the Arduino Uno.
  • Connect EN on the ESP8266 to three.3V on the Arduino Uno.
  • Connect 3v3 (or VCC) on the ESP8266 to three.3V on the Arduino Uno.
  • Connect GND on the ESP8266 to GND on the Arduino Uno.

Do I would like resistor for DHT11?

The DHT11 sensors often require exterior pull-up resistor of 10KΩ between VCC and Out pin for correct communication between sensor and the Arduino. However, the module has a built-in pull-up resistor, so you needn’t add it. The module additionally has a decoupling capacitor for filtering noise on the ability provide.

Leave a Reply