/********* KY-015 (DHT11) data webserver with OTA Configured for - ESP-01 (single digital input) - Arduino IDE First upload to a new device must be done through USB cable to USB port Subsequent uploads are done to URL address which is available as a port in the Arduino IDE Tools / Ports menu item. This application does not use the Serial device. Therefore ESP01 TX and RX are available as GPIO 01 and 03 repectively. GPIO01 is used for this example. *********/ // ESP8266 (ESP01) DHT Temperature Sensor // // The web page display code is sourced from: // https://theiotprojects.com/esp8266-dht11-dht22-temperature-humidity-with-local-web-server/ // // Import required libraries #include #include #include #include #include #include #include #include #include #include // Replace with your network credentials const char* ssid = "---------"; // Your WiFi Name const char* password = "----------"; // Your Wifi Password #define DHTPIN 1 // Digital pin connected to the DHT sensor // Uncomment the type of sensor in use: #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE); // current temperature & humidity, updated in loop() float t = 0.0; float h = 0.0; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); // Create a variable to track elapsed time unsigned long previousMillis = 0; // Create a constant for the update interval const long interval = 10000; // Create the web page template const char index_html[] PROGMEM = R"rawliteral(

Filament Box Remote Monitoring

Humidity %HUMIDITY% %

Temperature %TEMPERATURE% °C

)rawliteral"; // Method to Replace placeholder with DHT values String processor(const String& var) { //Serial.println(var); if (var == "TEMPERATURE") { return String(t); } else if (var == "HUMIDITY") { return String(h); } return String(); } void setup() { // Serial port for debugging purposes Serial.begin(115200,SERIAL_8N1,SERIAL_TX_ONLY); dht.begin(); // Connect to Wi-Fi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(2000); Serial.print("."); } // Print ESP8266 Local IP Address Serial.println(WiFi.localIP()); // Landing place for web page events server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/html", index_html, processor); }); server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/plain", String(t).c_str()); }); server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/plain", String(h).c_str()); }); // Port defaults to 8266 // ArduinoOTA.setPort(8266); // Hostname defaults to esp8266-[ChipID] // ArduinoOTA.setHostname("myesp8266"); // No authentication by default // ArduinoOTA.setPassword("admin"); // Password can be set with it's md5 value as well // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); // OTA Setup ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { // U_FS type = "filesystem"; } // NOTE: if updating FS this would be the place to unmount FS using FS.end() Serial.println("Start updating " + type); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { Serial.println("Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { Serial.println("Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { Serial.println("Receive Failed"); } else if (error == OTA_END_ERROR) { Serial.println("End Failed"); } }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Start server server.begin(); } void loop() { // Handle OTA if triggered ArduinoOTA.handle(); // Read data and display unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time the DHT values were updated previousMillis = currentMillis; // Read temperature as Celsius (the default) float newT = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) //float newT = dht.readTemperature(true); // if temperature read failed, don't change t value if (isnan(newT)) { Serial.println("Failed to read from DHT sensor!"); } else { t = newT; Serial.println(t); } // Read Humidity float newH = dht.readHumidity(); // if humidity read failed, don't change h value if (isnan(newH)) { Serial.println("Failed to read from DHT sensor!"); } else { h = newH; Serial.println(h); } } }