Robojax

ESP32 Tutorial 51 - Temperatuer and Humidty over WiFi with DHT | SunFounder's ESP32 IoT Learnig kit

دروس آردوینو به فارسی

ESP32 Tutorial 51 - Temperatuer and Humidty over WiFi with DHT | SunFounder's ESP32 IoT Learnig kit

This sketch is for ESP32 using DHT11, DHT22 to measure Temperature and humidity amnd display it over WiFi on the browser of Phone or Computer.

Purchase SunFounder's ESP32 Learning kit from Affiliated Stores


 
/*
 * Display temperature on Browswer screen using DHT11, DHT22 with ESP32
 * 
 * Written by Ahmad Shamshiri on Dec 18, 2023
 * 
 * Watch video instruciton for this video:  https://youtu.be/drrW1EHqKuM
 * 
 * I have combined DHT library of Adafruit with ESP8266 WebServer both links 
 * Adafruit DHT library on GitHub: https://github.com/adafruit/DHT-sensor-library
 * and 
 * ESP8266 on GitHub : https://github.com/esp8266/Arduino
 * 
   Copyright (c) 2015, Majenko Technologies
   All rights reserved.

   Redistribution and use in source and binary forms, with or without modification,
   are permitted provided that the following conditions are met:

 * * Redistributions of source code must retain the above copyright notice, this
     list of conditions and the following disclaimer.

 * * Redistributions in binary form must reproduce the above copyright notice, this
     list of conditions and the following disclaimer in the documentation and/or
     other materials provided with the distribution.

 * * Neither the name of Majenko Technologies nor the names of its
     contributors may be used to endorse or promote products derived from
     this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

const int refresh=3;//read every 3 seconds
boolean showSerial =true;//true or false

unsigned int unit=0;//0=C, 1=F, 
char *title[]={"Temperature","Temperature","Humidity"};
char *unitText[]={"°C","°F","%"};\

#include "DHT.h"
#define DHTPIN 14  // Set the pin connected to the DHT11 data pin
#define DHTTYPE DHT11 // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
float temperatureValue,temperatureFValue, humidityValue;// 


#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char *ssid = "dars";
const char *password = "5152535455";

WebServer server(80);

void sendTemp() {
//see video 
  String page = "<!DOCTYPE html>
";
  page +="<html>
";  
  page +="<head>
";
  page +="<title>Robojax DHT</title>
";
  page +="    <meta http-equiv='refresh' content='";
  page += String(refresh);// how often temperature is read
  page +="'/>
";  

  page +="<head>
";  
  page +="<body>
"; 
  page +="<h1>Robojax.com DHT Code</h1>
";  
  page +="<p style=\"font-size:50px\"> 
";  
  page +=title[unit];  
  page +=": ";
  if (DHTTYPE ==DHT11){
    page += String((int)temperatureValue);  
  }else{
    page += String(temperatureValue, 1);
  }
  page +=unitText[unit]; 
  page +="<br/>
";
  //humidity starts
  page +=title[2];  
  page +=": 
";  
  page += String((int)humidityValue);      
  page +=unitText[2];  
  //humidity ends
  page +="</p>
"; 
  
  page +="</body>";  
  page +="</html>
";  
  server.send(200,  "text/html", page);

}



void handleNotFound() {
  String message = "File Not Found

";
  message += "URI: ";
  message += server.uri();
  message += "
Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "
Arguments: ";
  message += server.args();
  message += "
";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "
";
  }

  server.send(404, "text/plain", message);

}

void setup(void) {
   dht.begin();
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("Open: http://");
  Serial.print(WiFi.localIP());
  Serial.println(" to read temperature");

  if (MDNS.begin("robojaxDHT")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", sendTemp);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  //Robojax.com code for ESP32 DHT11 DHT22
  server.handleClient();
  temperatureValue = dht.readTemperature();// Read temperature as Celsius (the default)
  humidityValue = dht.readHumidity();// Reading humidity 
  temperatureFValue = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)

    if(showSerial){
        Serial.print(title[unit]);
        Serial.print(": ");
        if (DHTTYPE ==DHT11){
      Serial.println((int)temperatureValue);  
        }else{
        Serial.print(temperatureValue,1);
        }
        
    }
  Serial.println();//just adds new line

  delay(300);// change this to larger value (1000 or more) if you don't need very often reading
  // Robojax.com code for ESP32 and DHT11 DHT22  
}