From 945278cc80993141ff37ba30c1d79f3e3f339f60 Mon Sep 17 00:00:00 2001 From: VolosR <62460330+VolosR@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:27:45 +0100 Subject: [PATCH] Add files via upload --- 1exampleWifiTime/1exampleWifiTime.ino | 55 ++++ .../2exampleTimeInBrowser.ino | 115 ++++++++ 3exampleMDNS/3exampleMDNS.ino | 84 ++++++ 4exampleJavaUpdate/4exampleJavaUpdate.ino | 117 ++++++++ .../5exampleAddingTemperature.ino | 221 ++++++++++++++ 6exampleAddedPresUI/6exampleAddedPresUI.ino | 278 ++++++++++++++++++ 6 files changed, 870 insertions(+) create mode 100644 1exampleWifiTime/1exampleWifiTime.ino create mode 100644 2exampleTimeInBrowser/2exampleTimeInBrowser.ino create mode 100644 3exampleMDNS/3exampleMDNS.ino create mode 100644 4exampleJavaUpdate/4exampleJavaUpdate.ino create mode 100644 5exampleAddingTemperature/5exampleAddingTemperature.ino create mode 100644 6exampleAddedPresUI/6exampleAddedPresUI.ino diff --git a/1exampleWifiTime/1exampleWifiTime.ino b/1exampleWifiTime/1exampleWifiTime.ino new file mode 100644 index 0000000..670deea --- /dev/null +++ b/1exampleWifiTime/1exampleWifiTime.ino @@ -0,0 +1,55 @@ +#include +#include + +// 🔹 CHANGE THESE +const char* ssid = "Bertovic"; +const char* password = "18072019"; + +// NTP server +const char* ntpServer = "pool.ntp.org"; + +// Timezone offset (seconds) +// Example: UTC +5:30 = 5.5 * 3600 = 19800 +const long gmtOffset_sec = 0; // Change for your timezone +const int daylightOffset_sec = 0; // Change if DST applies + +void setup() { + Serial.begin(115200); + + // Connect to Wi-Fi + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\nWiFi connected!"); + + // Initialize time + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + + Serial.println("Time initialized"); +} + +void loop() { + struct tm timeinfo; + + if (!getLocalTime(&timeinfo)) { + Serial.println("Failed to obtain time"); + delay(1000); + return; + } + + Serial.printf("Date: %04d-%02d-%02d ", + timeinfo.tm_year + 1900, + timeinfo.tm_mon + 1, + timeinfo.tm_mday); + + Serial.printf("Time: %02d:%02d:%02d\n", + timeinfo.tm_hour, + timeinfo.tm_min, + timeinfo.tm_sec); + + delay(1000); // Update every second +} + diff --git a/2exampleTimeInBrowser/2exampleTimeInBrowser.ino b/2exampleTimeInBrowser/2exampleTimeInBrowser.ino new file mode 100644 index 0000000..632bb89 --- /dev/null +++ b/2exampleTimeInBrowser/2exampleTimeInBrowser.ino @@ -0,0 +1,115 @@ +#include +#include +#include + +// ====== CHANGE THESE ====== +const char* ssid = "Bertovic"; +const char* password = "18072019"; + +// Time / NTP +const char* ntpServer = "pool.ntp.org"; + +// Timezone: Croatia (Europe/Zagreb) is usually UTC+1, and UTC+2 in DST. +// Simplest fixed offset: set UTC+1 (3600). If you want DST auto, tell me and I’ll give that version. +const long gmtOffset_sec = 3600; // UTC+1 +const int daylightOffset_sec = 0; // Set 3600 if you want to force DST (not automatic) + +// Web server +WebServer server(80); + +String formatTimeNow() { + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) { + return String("Time not available (NTP not synced yet)"); + } + + char buf[32]; + // YYYY-MM-DD HH:MM:SS + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &timeinfo); + return String(buf); +} + +void handleRoot() { + // Simple HTML page that fetches /time every second + String html = R"rawliteral( + + + + + + ESP32 Time + + + +
+
Current time:
+
Loading...
+
Updates every second
+
+ + + + +)rawliteral"; + + server.send(200, "text/html", html); +} + +void handleTime() { + server.send(200, "text/plain", formatTimeNow()); +} + +void setup() { + Serial.begin(115200); + + // Connect Wi-Fi + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\nWiFi connected!"); + Serial.print("ESP32 IP address: "); + Serial.println(WiFi.localIP()); + + // NTP time init + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + + // Optional: wait a bit for first sync + Serial.println("Syncing time..."); + for (int i = 0; i < 15; i++) { + if (formatTimeNow().indexOf("not available") == -1) break; + delay(500); + } + Serial.println("Current time: " + formatTimeNow()); + + // Web routes + server.on("/", handleRoot); + server.on("/time", handleTime); + + server.begin(); + Serial.println("Web server started. Open the IP in your browser."); +} + +void loop() { + server.handleClient(); +} + diff --git a/3exampleMDNS/3exampleMDNS.ino b/3exampleMDNS/3exampleMDNS.ino new file mode 100644 index 0000000..c3c5391 --- /dev/null +++ b/3exampleMDNS/3exampleMDNS.ino @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +// ===== WIFI ===== +const char* ssid = "Bertovic"; +const char* password = "18072019"; + +// ===== NTP ===== +const char* ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = 3600; // adjust to your timezone +const int daylightOffset_sec = 0; + +// ===== WEB ===== +WebServer server(80); + +// Format time +String getTimeString() { + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) { + return "Time not ready"; + } + char buf[32]; + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &timeinfo); + return String(buf); +} + +// Root page +void handleRoot() { + String page = R"rawliteral( + + + + + ESP32 Local Time + + + +

ESP32 Local Time

+

)rawliteral" + getTimeString() + R"rawliteral(

+ + +)rawliteral"; + + server.send(200, "text/html", page); +} + +void setup() { + Serial.begin(115200); + + // WiFi + WiFi.begin(ssid, password); + Serial.print("Connecting"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println("\nWiFi connected"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + // mDNS name -> myhome.local + if (!MDNS.begin("myhome")) { + Serial.println("mDNS failed"); + } else { + Serial.println("mDNS started: http://myhome.local/"); + } + + // Time + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + + // Web server + server.on("/", handleRoot); + server.begin(); + + Serial.println("Web server started"); +} + +void loop() { + server.handleClient(); +} + diff --git a/4exampleJavaUpdate/4exampleJavaUpdate.ino b/4exampleJavaUpdate/4exampleJavaUpdate.ino new file mode 100644 index 0000000..84daba4 --- /dev/null +++ b/4exampleJavaUpdate/4exampleJavaUpdate.ino @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +// ================== CHANGE THESE ================== +const char* ssid = "Bertovic"; +const char* password = "18072019"; + +// mDNS name -> http://myhome.local/ +const char* mdnsName = "myhome"; + +// NTP +const char* ntpServer = "pool.ntp.org"; + +// Timezone example: Europe/Zagreb (CET = UTC+1). DST is not automatic in this simple offset mode. +// If you want automatic DST, tell me and I’ll give the TZ-string version. +const long gmtOffset_sec = 3600; // UTC+1 +const int daylightOffset_sec = 0; +// ================================================== + +WebServer server(80); + +String getTimeString() { + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) { + return "Time not ready (NTP sync)"; + } + char buf[32]; + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &timeinfo); + return String(buf); +} + +void handleRoot() { + // Smooth updates: page loads once, then fetches /time every second + String page = R"rawliteral( + + + + + + ESP32 Time + + + +
+
Current time:
+
Loading...
+
Updates every second • Served by ESP32
+
+ + + + +)rawliteral"; + + server.send(200, "text/html", page); +} + +void handleTime() { + server.send(200, "text/plain", getTimeString()); +} + +void setup() { + Serial.begin(115200); + + // Wi-Fi connect + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\nWiFi connected!"); + Serial.print("ESP32 IP: "); + Serial.println(WiFi.localIP()); + + // Start mDNS + if (!MDNS.begin(mdnsName)) { + Serial.println("mDNS start FAILED"); + } else { + Serial.print("mDNS started: http://"); + Serial.print(mdnsName); + Serial.println(".local/"); + } + + // NTP time + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + + // Web routes + server.on("/", handleRoot); + server.on("/time", handleTime); + + server.begin(); + Serial.println("HTTP server started"); +} + +void loop() { + server.handleClient(); +} diff --git a/5exampleAddingTemperature/5exampleAddingTemperature.ino b/5exampleAddingTemperature/5exampleAddingTemperature.ino new file mode 100644 index 0000000..2fc55c5 --- /dev/null +++ b/5exampleAddingTemperature/5exampleAddingTemperature.ino @@ -0,0 +1,221 @@ +#include +#include +#include +#include +#include +#include // Works for BMP180 too + +// ================== CHANGE THESE ================== +const char* ssid = "Bertovic"; +const char* password = "18072019"; + +// mDNS name => http://myhome.local/ +const char* mdnsName = "myhome"; + +// NTP +const char* ntpServer = "pool.ntp.org"; + +// Timezone example: Europe/Zagreb (CET = UTC+1). This offset version does NOT auto-handle DST. +const long gmtOffset_sec = 3600; // UTC+1 +const int daylightOffset_sec = 0; + +// BMP180 I2C pins (your values) +// NOTE: On many ESP32 boards, GPIO 6/7 are not usable (often connected to flash). +// If it doesn't work, use e.g. SDA=21, SCL=22 (common defaults). +#define SDA_PIN 7 +#define SCL_PIN 6 +// ================================================== + +WebServer server(80); +Adafruit_BMP085 bmp; + +bool bmpOk = false; +float lastTempC = NAN; +unsigned long lastTempReadMs = 0; + +String getTimeString() { + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) return "Time not ready"; + char buf[32]; + strftime(buf, sizeof(buf), "%H:%M:%S", &timeinfo); + return String(buf); +} + +String getDateString() { + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) return ""; + char buf[64]; + strftime(buf, sizeof(buf), "%A, %d %B %Y", &timeinfo); + return String(buf); +} + +void updateTemperatureIfNeeded() { + // Read BMP180 at most once per second + if (!bmpOk) return; + + unsigned long now = millis(); + if (now - lastTempReadMs < 1000) return; + lastTempReadMs = now; + + float t = bmp.readTemperature(); + lastTempC = t; + + // Optional Serial output (like your original code) + Serial.print("Temperature: "); + Serial.print(lastTempC); + Serial.println(" °C"); +} + +String getTempString() { + updateTemperatureIfNeeded(); + + if (!bmpOk) return "Sensor not detected"; + if (isnan(lastTempC)) return "Reading..."; + + char buf[16]; + // 1 decimal place, e.g. 23.4 °C + snprintf(buf, sizeof(buf), "%.1f °C", lastTempC); + return String(buf); +} + +void handleRoot() { + String page = R"rawliteral( + + + + + + ESP32 Temperature + + + + + + +
+
--.- °C
+
--:--:--
+
+
myhome.local • ESP32
+
+ + + + +)rawliteral"; + + server.send(200, "text/html", page); +} + +void handleTime() { server.send(200, "text/plain", getTimeString()); } +void handleDate() { server.send(200, "text/plain", getDateString()); } +void handleTemp() { server.send(200, "text/plain", getTempString()); } + +void setup() { + Serial.begin(115200); + delay(1000); + + // I2C init with your custom pins + Wire.begin(SDA_PIN, SCL_PIN); + + Serial.println("Initializing BMP180..."); + bmpOk = bmp.begin(); + if (!bmpOk) { + Serial.println("Could not find BMP180 sensor!"); + } else { + Serial.println("BMP180 detected!"); + } + + // Wi-Fi + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\nWiFi connected!"); + Serial.print("ESP32 IP: "); + Serial.println(WiFi.localIP()); + + // mDNS + if (!MDNS.begin(mdnsName)) { + Serial.println("mDNS start FAILED"); + } else { + Serial.print("mDNS started: http://"); + Serial.print(mdnsName); + Serial.println(".local/"); + } + + // Time sync + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + + // Web routes + server.on("/", handleRoot); + server.on("/time", handleTime); + server.on("/date", handleDate); + server.on("/temp", handleTemp); + + server.begin(); + Serial.println("HTTP server started"); +} + +void loop() { + server.handleClient(); + updateTemperatureIfNeeded(); // keep temp fresh even if nobody is polling +} + diff --git a/6exampleAddedPresUI/6exampleAddedPresUI.ino b/6exampleAddedPresUI/6exampleAddedPresUI.ino new file mode 100644 index 0000000..a49c0ef --- /dev/null +++ b/6exampleAddedPresUI/6exampleAddedPresUI.ino @@ -0,0 +1,278 @@ +#include +#include +#include +#include +#include +#include + +// ================== CHANGE THESE ================== +const char* ssid = "Bertovic"; +const char* password = "18072019"; + +const char* mdnsName = "myhome"; + +const char* ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = 3600; +const int daylightOffset_sec = 0; + +// BMP180 I2C pins (recommended ESP32 defaults) +#define SDA_PIN 7 +#define SCL_PIN 6 +// ================================================== + +WebServer server(80); +Adafruit_BMP085 bmp; + +bool bmpOk = false; +float tempC = NAN; +float press_hPa = NAN; +unsigned long lastRead = 0; + +// ---------- TIME ---------- +String getTimeString() { + struct tm t; + if (!getLocalTime(&t)) return "--:--:--"; + char b[16]; + strftime(b, sizeof(b), "%H:%M:%S", &t); + return String(b); +} + +String getDateString() { + struct tm t; + if (!getLocalTime(&t)) return ""; + char b[64]; + strftime(b, sizeof(b), "%A, %d %B %Y", &t); + return String(b); +} + +// ---------- SENSOR ---------- +void updateSensor() { + if (!bmpOk) return; + if (millis() - lastRead < 1000) return; + lastRead = millis(); + + tempC = bmp.readTemperature(); + press_hPa = bmp.readPressure() / 100.0f; +} + +String getTemp() { + updateSensor(); + if (!bmpOk || isnan(tempC)) return "--.- °C"; + char b[16]; + snprintf(b, sizeof(b), "%.1f °C", tempC); + return String(b); +} + +String getPress() { + updateSensor(); + if (!bmpOk || isnan(press_hPa)) return "----.- hPa"; + char b[20]; + snprintf(b, sizeof(b), "%.1f hPa", press_hPa); + return String(b); +} + +// ---------- WEB ---------- +void handleRoot() { + String page = R"rawliteral( + + + + + +ESP32 Ambient + + + + + + + +
+
+ +
+
Indoor climate
+
--.- °C
+
----.- hPa
+
--:--:--
+
+ +
+ +
+
Details
+ +
Temperature
+
Pressure
+
Time
+
Date
+
+ +
+
+ + + + +)rawliteral"; + + server.send(200,"text/html",page); +} + +void setup(){ + Serial.begin(115200); + Wire.begin(SDA_PIN,SCL_PIN); + + bmpOk = bmp.begin(); + + WiFi.begin(ssid,password); + while(WiFi.status()!=WL_CONNECTED) delay(500); + + MDNS.begin(mdnsName); + configTime(gmtOffset_sec,daylightOffset_sec,ntpServer); + + server.on("/",handleRoot); + server.on("/time",[](){server.send(200,"text/plain",getTimeString());}); + server.on("/date",[](){server.send(200,"text/plain",getDateString());}); + server.on("/temp",[](){server.send(200,"text/plain",getTemp());}); + server.on("/press",[](){server.send(200,"text/plain",getPress());}); + + server.begin(); +} + +void loop(){ + server.handleClient(); + updateSensor(); +}