ESP32 Tutorial 51/55 - Temperatuer and Humidty over WiFi with DHT | SunFounder's ESP32 IoT Learnig kit
SunFounder Documentation page ESP32 Learning Kit
Purchase SunFounder ESP32 Learning Kit
In this video we learn how to use ESP32 as a server and measure temperture and humidity using DHT11 and dispaly it on mobile phone or computer browser. Full code is provided and explained.
Topics in this lesson
Use Chapters from timeline or click on the time
- 00:00 Introduction
- 1:45 Introduction to DHT wifi project
- 3:59 Arduino Code for ESP32 DHT Wifi
- 13:31 Selecting ESP32 board and COM port in Arduino IDE
- 15:13 Demonstration of Temperature over Wifi
/* * Display temperature on Browswer screen using DHT11, DHT22 with ESP32 * * Written by Ahmad Shamshiri on Dec 18, 2023 Watch video instruction for this code on YouTube https://youtu.be/drrW1EHqKuM Resource page for this lesson and code: http://robojax.com/course3 Tutoril by https://youTube.com/@robojax * * 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#include #include #include const char *ssid = "dars"; const char *password = "DDDDDDDD"; WebServer server(80); void sendTemp() { //see video String page = "\n"; page +="\n"; page +="\n"; page +=" Robojax DHT \n"; page +=" \n"; page +="\n"; page +="\n"; page +="Robojax.com DHT Code
\n"; page +="\n"; page +=title[unit]; page +=": "; if (DHTTYPE ==DHT11){ page += String((int)temperatureValue); }else{ page += String(temperatureValue, 1); } page +=unitText[unit]; page +="
\n"; page +=""; page +="\n"; server.send(200, "text/html", page); } 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) { 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 }
\n"; //humidity starts page +=title[2]; page +=": \n"; page += String((int)humidityValue); page +=unitText[2]; //humidity ends page +="