From 5af6ffafcaab03519d55f4995c4fefb680e07803 Mon Sep 17 00:00:00 2001 From: Ipmake Date: Wed, 20 Mar 2024 10:32:42 +0100 Subject: [PATCH] Added webserver --- .gitattributes | 0 platformio.ini | 7 ++ src/main.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 179 insertions(+), 7 deletions(-) mode change 100644 => 100755 .gitattributes diff --git a/.gitattributes b/.gitattributes old mode 100644 new mode 100755 diff --git a/platformio.ini b/platformio.ini index 16d27e5..d2a8c77 100755 --- a/platformio.ini +++ b/platformio.ini @@ -8,6 +8,9 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[env] +monitor_speed = 115200 + [env:denky32] platform = espressif32 board = denky32 @@ -16,3 +19,7 @@ lib_deps = adafruit/Adafruit Unified Sensor@^1.1.14 adafruit/DHT sensor library@^1.4.6 marcoschwartz/LiquidCrystal_I2C@^1.1.4 + esphome/ESPAsyncWebServer-esphome@^3.1.0 + +[env:env1] +lib_deps = esphome/ESPAsyncWebServer-esphome@^3.1.0 diff --git a/src/main.cpp b/src/main.cpp index 26ba7e0..c5440ce 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ +#include "ESPAsyncWebServer.h" #include #include #include +#include "WiFi.h" const int DHT_PIN = 4; const int PIEZO_PIN = 17; @@ -11,12 +13,126 @@ const int LEDRED_PIN = 26; const int LEDWH_PIN = 13; +const char* ssid =""; +const char* password=""; + +AsyncWebServer server(80); + + int prevRating = -1; DHT dht(DHT_PIN, DHT11); +String readDHTTemperature() { + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + // Read temperature as Celsius (the default) + float t = dht.readTemperature(); + // Read temperature as Fahrenheit (isFahrenheit = true) + //float t = dht.readTemperature(true); + // Check if any reads failed and exit early (to try again). + if (isnan(t)) { + Serial.println("Failed to read from DHT sensor!"); + return "--"; + } + else { + Serial.println(t); + return String(t); + } +} + +String readDHTHumidity() { + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + float h = dht.readHumidity(); + if (isnan(h)) { + Serial.println("Failed to read from DHT sensor!"); + return "--"; + } + else { + Serial.println(h); + return String(h); + } +} + +const char index_html[] PROGMEM = R"rawliteral( + + + + + + + +

ESP32 DHT

+

+ + Temperature + %TEMPERATURE% + °C +

+

+ + Feuchtigkeit + %HUMIDITY% + % +

+ + +)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(); +} + + LiquidCrystal_I2C lcd(0x27, 16, 5); +float temp; +float humid; + void playWarnTone() { tone(PIEZO_PIN, 2000); @@ -35,15 +151,31 @@ void OFFLEDS() digitalWrite(LEDRED_PIN, HIGH); } +void UpdateLCD() +{ + lcd.clear(); + lcd.setContrast(16); + Serial.println("Updating LCD"); + + lcd.setCursor(0, 0); + lcd.print("Temp: " + (String)temp); + lcd.setCursor(0, 1); + lcd.print("Humid: " + (String)humid); +} + void setup() { - Serial.begin(9600); + Serial.begin(115200); dht.begin(); lcd.init(); lcd.backlight(); - lcd.print("Hello"); + lcd.noAutoscroll(); + + lcd.print("Welcome!"); + lcd.setCursor(0, 1); + lcd.print("www.ipmake.dev"); pinMode(PIEZO_PIN, OUTPUT); @@ -60,13 +192,45 @@ void setup() tone(PIEZO_PIN, 2000); delay(250); noTone(PIEZO_PIN); + + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi.."); + } + + // Print ESP32 Local IP Address + Serial.println(WiFi.localIP()); + + // Route for root / web page + 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", readDHTTemperature().c_str()); + }); + server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/plain", readDHTHumidity().c_str()); + }); + + // Start server + server.begin(); + + // // Initialize the timer + // hw_timer_t *timer = timerBegin(1, 80, true); // Timer ID 1, prescaler 80 (1MHz clock) + // // Set the timer interval + // timerAlarmWrite(timer, 10000, true); + // // Attach the function to the timer + // timerAttachInterrupt(timer, &UpdateLCD, true); + // // Start the timer + // timerAlarmEnable(timer); } void loop() { - float temp = dht.readTemperature(); - float humid = dht.readHumidity(); - delay(100); + temp = dht.readTemperature(); + humid = dht.readHumidity(); + delay(1000); int rating = 0; @@ -103,6 +267,7 @@ void loop() } // Serial.println("Temp: " + (String)temp + " Humid: " + (String)humid + " Rating: " + (String)rating); - prevRating = rating; -} \ No newline at end of file + + UpdateLCD(); +}