From 19c65aa1512d48a886167d520ec7e971103441d1 Mon Sep 17 00:00:00 2001 From: bmixed <2+bmixed@noreply.localhost> Date: Sun, 9 Aug 2026 08:56:34 +0000 Subject: [PATCH] Upload files to "/" --- esp32c3-dht-wifi.ino.txt | 290 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 esp32c3-dht-wifi.ino.txt diff --git a/esp32c3-dht-wifi.ino.txt b/esp32c3-dht-wifi.ino.txt new file mode 100644 index 0000000..4c273b0 --- /dev/null +++ b/esp32c3-dht-wifi.ino.txt @@ -0,0 +1,290 @@ +#include +#include +#include + +// ----------- WiFi ----------- +static const char* WIFI_SSID = "ssid"; +static const char* WIFI_PASS = "password"; + +// ----------- DHT ----------- +static const int DHT_PIN = 10; // GPIO for DHT DATA +static const int DHT_TYPE = DHT11; +DHT dht(DHT_PIN, DHT_TYPE); + +// ----------- Server ----------- +WebServer server(80); + +// ----------- Reading interval ----------- +static const unsigned long READ_MS = 2000; +unsigned long lastRead = 0; + +// ----------- Latest values ----------- +float latestH = NAN; +float latestT_F = NAN; + +String htmlPage() { + return R"HTML( + + + + + + ESP32-C3 DHT11 + + + +
+

ESP32 DHT

+
Updates every 2 seconds • Last ~60 samples
+ +
+
+

Temperature:

+

--

+
+
+

Humidity:

+

--

+
+
+ + +
+
Temp (°F)
+
Humidity (%)
+
+ +
+ +
+
+ + + + +)HTML"; +} + + +void handleRoot() { + server.send(200, "text/html", htmlPage()); +} + +void handleData() { + // Convert NaN to null for easy JSON handling + float h = latestH; + float t = latestT_F; + + String json = "{"; + json += "\"h\": " + (isnan(h) ? String("null") : String(h, 1)) + ","; + json += "\"t_f\": " + (isnan(t) ? String("null") : String(t, 1)); + json += "}"; + + server.send(200, "application/json", json); +} + +void setup() { + Serial.begin(115200); + delay(200); + Serial.println("ESP32-C3 DHT11 starting..."); + + dht.begin(); + + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(300); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected. IP address: "); + Serial.println(WiFi.localIP()); + + server.on("/", handleRoot); + server.on("/data", handleData); + server.begin(); + Serial.println("HTTP server started."); +} + +void loop() { + // keep server responsive + server.handleClient(); + + unsigned long now = millis(); + if (now - lastRead < READ_MS) return; + lastRead = now; + + float h = dht.readHumidity(); + float tC = dht.readTemperature(); // Celsius + + if (isnan(h) || isnan(tC)) { + Serial.println("DHT read failed (check wiring/pull-up)."); + latestH = NAN; + latestT_F = NAN; + return; + } + + latestH = h; + latestT_F = dht.convertCtoF(tC); + + Serial.print("Humidity: "); + Serial.print(latestH, 1); + Serial.print(" %\tTemp: "); + Serial.print(latestT_F, 1); + Serial.println(" F"); + + Serial.print("Connected. IP address: "); + Serial.println(WiFi.localIP()); +}