ESP-01 Temperature and Humidity Server


Wiring for ESP-01 and DHT11

ESP-01 + DHT11 = Temp and Humidity Wireless Web Server

    This is a little app that gets temperature and humidity information and displays it on a HTTP server. It's pretty simple, the app sets up the sensor and connects to WIFI, then listens for HTTP requests and gives a web page to the requester. The web page is a custom page created with variables that change each loop. This project uses a ESP-01, but you can use a ESP32 also. Wiring it all together is simple too thanks to the DHT11 only having three usable pins and the ESP-01 having built in WIFI. The AMS1117 regulates power from 4.7V-12V and turns it into 3.3V and the ESP-01 really needs its power to be exact cause it will happily shut off with a little less than 3.3V or go into a reboot cycle.

    Instead of an html file, like on a desktop computer, the web page is just a String object the ESP-01 gives to the computer trying to access your ESP-01. The String object includes the temperature and humidity data and is modified just before the ESP-01 gives the String to the other computer trying to access the ESP-01. To connect to your ESP-01, open a web browser and type "http://" and then the ESP-01's IP address. So if the IP was for example, 192.168.0.100, you would type "http//192.168.0.100". If You don't know the IP address, get your hands on a "Network Mapper" app that can tell you what's connected to your network.

Software code:


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <DHT.h>


#ifndef STASSID
#define STASSID "YOURWIFI"
#define STAPSK  "YOURPASSWORD"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

DHT dht(2, DHT11);

const int led = 13;

String tempString = "Unknown";
int tempCelcius = 0;
int temp = 0;
int tempHigh = 0;
int tempLow = 100;
int humidity = 0;
int humidityHigh = 0;
int humidityLow = 100;
String humidityString = "Unknown";

void handleRoot() {
  digitalWrite(led, 1);
  //server.send(200, "text/plain", "hello from esp8266!");
  server.send(200, "text/html", "<center><p><br><h1>Temp: </h1>" + tempString + "<p><br><h1>Humidity: </h2>" + humidityString + "<h1><br>Uptime: </h1><h2>" + (millis()/86400000.0) + " Days</h2> <br><br><button type=\"button\" onclick=\"location.href='/resetstats';\">Reset Stats</button>");
  
digitalWrite(led, 0); Serial.print("Root Opened"); } void handleNotFound() { digitalWrite(led, 1); 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); digitalWrite(led, 0); } void setup(void) { pinMode(led, OUTPUT); digitalWrite(led, 0); Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println("Connecting..."); // 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("littleGuy")) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/temp", []() { server.send(200, "text/plain", "temp: " + temp); }); server.on("/humidity", []() { server.send(200, "text/plain", "humidity: " + humidity); }); server.on("/resetstats", []() { tempCelcius = 0; temp = 0; tempHigh = 0; tempLow = 100; humidity = 0; humidityHigh = 0; humidityLow = 100; server.send(200, "text/html", "<meta http-equiv=\"Refresh\" content=\"1; url=/\">Please wait...<br><p><a href=\"/\">Click here to go back</a></p>");
}); server.onNotFound(handleNotFound); server.begin(); dht.begin(); Serial.println("HTTP server started and ready to go"); } void loop(void) { tempCelcius = dht.readTemperature(); temp = dht.readTemperature(true); humidity = dht.readHumidity(); // Get TEMP and check if its the highest or lowest tempString = temp; //tempString = "<h2>" + tempString + "F, " + tempCelcius + "C</h2>"; tempString = "<h2>" + tempString + "F</h2>";
if (temp > tempHigh){ tempHigh = temp; } if (temp < tempLow){ tempLow = temp; } tempString = tempString + "<br><b>Low:</b> " + tempLow + " <b>High:</b> " + tempHigh + "<p>";
// get HUMIDITY and check highest and lowest humidityString = humidity; humidityString = "<h2>" + humidityString + "%</h2>"; if (humidity > humidityHigh){ humidityHigh = humidity; } if (humidity < humidityLow){ humidityLow = humidity; } humidityString = humidityString + "<br><b>Low:</b> " + humidityLow + " <b>High:</b> " + humidityHigh + "<p>";
server.handleClient(); MDNS.update(); }

Parts you'll need: