Add files via upload
This commit is contained in:
55
1exampleWifiTime/1exampleWifiTime.ino
Normal file
55
1exampleWifiTime/1exampleWifiTime.ino
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// 🔹 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
|
||||||
|
}
|
||||||
|
|
||||||
115
2exampleTimeInBrowser/2exampleTimeInBrowser.ino
Normal file
115
2exampleTimeInBrowser/2exampleTimeInBrowser.ino
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// ====== 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(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>ESP32 Time</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; margin: 24px; }
|
||||||
|
.card { padding: 16px; border: 1px solid #ddd; border-radius: 12px; max-width: 420px; }
|
||||||
|
#t { font-size: 1.6rem; font-weight: 700; }
|
||||||
|
.small { color: #666; margin-top: 8px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="card">
|
||||||
|
<div>Current time:</div>
|
||||||
|
<div id="t">Loading...</div>
|
||||||
|
<div class="small">Updates every second</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
async function updateTime(){
|
||||||
|
try{
|
||||||
|
const r = await fetch('/time', { cache: 'no-store' });
|
||||||
|
const text = await r.text();
|
||||||
|
document.getElementById('t').textContent = text;
|
||||||
|
} catch(e){
|
||||||
|
document.getElementById('t').textContent = 'Error';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateTime();
|
||||||
|
setInterval(updateTime, 1000);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)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();
|
||||||
|
}
|
||||||
|
|
||||||
84
3exampleMDNS/3exampleMDNS.ino
Normal file
84
3exampleMDNS/3exampleMDNS.ino
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include <ESPmDNS.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// ===== 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(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>ESP32 Local Time</title>
|
||||||
|
<meta http-equiv="refresh" content="1">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>ESP32 Local Time</h2>
|
||||||
|
<p>)rawliteral" + getTimeString() + R"rawliteral(</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)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();
|
||||||
|
}
|
||||||
|
|
||||||
117
4exampleJavaUpdate/4exampleJavaUpdate.ino
Normal file
117
4exampleJavaUpdate/4exampleJavaUpdate.ino
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include <ESPmDNS.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// ================== 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(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>ESP32 Time</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; margin: 24px; }
|
||||||
|
.card { padding: 16px; border: 1px solid #ddd; border-radius: 12px; max-width: 420px; }
|
||||||
|
#clock { font-size: 1.8rem; font-weight: 700; margin-top: 8px; }
|
||||||
|
.small { color: #666; margin-top: 10px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="card">
|
||||||
|
<div>Current time:</div>
|
||||||
|
<div id="clock">Loading...</div>
|
||||||
|
<div class="small">Updates every second • Served by ESP32</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
async function updateTime() {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/time', { cache: 'no-store' });
|
||||||
|
const t = await r.text();
|
||||||
|
document.getElementById('clock').textContent = t;
|
||||||
|
} catch (e) {
|
||||||
|
document.getElementById('clock').textContent = 'Error getting time';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateTime();
|
||||||
|
setInterval(updateTime, 1000);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)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();
|
||||||
|
}
|
||||||
221
5exampleAddingTemperature/5exampleAddingTemperature.ino
Normal file
221
5exampleAddingTemperature/5exampleAddingTemperature.ino
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include <ESPmDNS.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_BMP085.h> // 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(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>ESP32 Temperature</title>
|
||||||
|
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;600;900&display=swap" rel="stylesheet">
|
||||||
|
|
||||||
|
<style>
|
||||||
|
html, body {
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
font-family: 'Inter', sans-serif;
|
||||||
|
background: linear-gradient(135deg, #0b132b, #1c2541, #3a506b);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
text-align: center;
|
||||||
|
padding: 24px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
.temp {
|
||||||
|
font-size: clamp(4.5rem, 14vw, 8rem);
|
||||||
|
font-weight: 900;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
line-height: 1;
|
||||||
|
}
|
||||||
|
.time {
|
||||||
|
margin-top: 12px;
|
||||||
|
font-size: clamp(2.2rem, 6vw, 3.6rem);
|
||||||
|
font-weight: 600;
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
.date {
|
||||||
|
margin-top: 10px;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
.small {
|
||||||
|
margin-top: 18px;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="temp" id="temp">--.- °C</div>
|
||||||
|
<div class="time" id="time">--:--:--</div>
|
||||||
|
<div class="date" id="date"></div>
|
||||||
|
<div class="small">myhome.local • ESP32</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
async function update(){
|
||||||
|
try {
|
||||||
|
document.getElementById('temp').textContent =
|
||||||
|
await (await fetch('/temp', { cache: 'no-store' })).text();
|
||||||
|
|
||||||
|
document.getElementById('time').textContent =
|
||||||
|
await (await fetch('/time', { cache: 'no-store' })).text();
|
||||||
|
|
||||||
|
document.getElementById('date').textContent =
|
||||||
|
await (await fetch('/date', { cache: 'no-store' })).text();
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
setInterval(update, 1000);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)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
|
||||||
|
}
|
||||||
|
|
||||||
278
6exampleAddedPresUI/6exampleAddedPresUI.ino
Normal file
278
6exampleAddedPresUI/6exampleAddedPresUI.ino
Normal file
@@ -0,0 +1,278 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include <ESPmDNS.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_BMP085.h>
|
||||||
|
|
||||||
|
// ================== 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(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>ESP32 Ambient</title>
|
||||||
|
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">
|
||||||
|
|
||||||
|
<style>
|
||||||
|
:root{
|
||||||
|
--glass: rgba(255,255,255,0.045);
|
||||||
|
--glass2: rgba(255,255,255,0.07);
|
||||||
|
--border: rgba(255,255,255,0.08);
|
||||||
|
--text: rgba(255,255,255,0.88);
|
||||||
|
--muted: rgba(255,255,255,0.55);
|
||||||
|
}
|
||||||
|
|
||||||
|
* { box-sizing: border-box; }
|
||||||
|
|
||||||
|
html,body{
|
||||||
|
height:100%;
|
||||||
|
margin:0;
|
||||||
|
font-family: Inter, system-ui, sans-serif;
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
body{
|
||||||
|
background:
|
||||||
|
radial-gradient(900px 600px at 20% 10%, rgba(120,120,255,0.12), transparent 60%),
|
||||||
|
radial-gradient(900px 600px at 80% 90%, rgba(255,120,200,0.10), transparent 60%),
|
||||||
|
linear-gradient(135deg, #0b1020, #0e1628);
|
||||||
|
}
|
||||||
|
|
||||||
|
.wrap{
|
||||||
|
height:100%;
|
||||||
|
display:grid;
|
||||||
|
place-items:center;
|
||||||
|
padding:24px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card{
|
||||||
|
width:min(880px,100%);
|
||||||
|
display:grid;
|
||||||
|
grid-template-columns: 1.2fr 0.8fr;
|
||||||
|
gap:16px;
|
||||||
|
padding:18px;
|
||||||
|
border-radius:24px;
|
||||||
|
background: linear-gradient(180deg,var(--glass2),var(--glass));
|
||||||
|
border:1px solid var(--border);
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media(max-width:820px){
|
||||||
|
.card{grid-template-columns:1fr;}
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel{
|
||||||
|
padding:18px;
|
||||||
|
border-radius:18px;
|
||||||
|
background: rgba(255,255,255,0.04);
|
||||||
|
border:1px solid var(--border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.title{
|
||||||
|
font-size:.85rem;
|
||||||
|
letter-spacing:.08em;
|
||||||
|
text-transform:uppercase;
|
||||||
|
color:var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.temp{
|
||||||
|
margin-top:10px;
|
||||||
|
font-size:clamp(4.2rem,9vw,6.8rem);
|
||||||
|
font-weight:700;
|
||||||
|
opacity:.92;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chip{
|
||||||
|
display:inline-block;
|
||||||
|
margin-top:12px;
|
||||||
|
padding:8px 12px;
|
||||||
|
border-radius:999px;
|
||||||
|
font-weight:600;
|
||||||
|
background: rgba(255,255,255,0.05);
|
||||||
|
border:1px solid var(--border);
|
||||||
|
opacity:.85;
|
||||||
|
}
|
||||||
|
|
||||||
|
.time{
|
||||||
|
margin-top:14px;
|
||||||
|
font-size:clamp(2.1rem,5vw,3rem);
|
||||||
|
font-weight:600;
|
||||||
|
opacity:.85;
|
||||||
|
}
|
||||||
|
|
||||||
|
.date{
|
||||||
|
margin-top:8px;
|
||||||
|
font-size:1rem;
|
||||||
|
color:var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv{
|
||||||
|
display:flex;
|
||||||
|
justify-content:space-between;
|
||||||
|
padding:10px 12px;
|
||||||
|
margin-top:10px;
|
||||||
|
border-radius:14px;
|
||||||
|
background: rgba(255,255,255,0.04);
|
||||||
|
border:1px solid var(--border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.k{ color:var(--muted); }
|
||||||
|
.v{ font-weight:600; opacity:.9; }
|
||||||
|
|
||||||
|
.footer{
|
||||||
|
margin-top:14px;
|
||||||
|
font-size:.9rem;
|
||||||
|
color:var(--muted);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="wrap">
|
||||||
|
<div class="card">
|
||||||
|
|
||||||
|
<div class="panel">
|
||||||
|
<div class="title">Indoor climate</div>
|
||||||
|
<div class="temp" id="temp">--.- °C</div>
|
||||||
|
<div class="chip" id="press">----.- hPa</div>
|
||||||
|
<div class="time" id="time">--:--:--</div>
|
||||||
|
<div class="date" id="date"></div>
|
||||||
|
<div class="footer">myhome.local • ESP32</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="panel">
|
||||||
|
<div class="title">Details</div>
|
||||||
|
|
||||||
|
<div class="kv"><div class="k">Temperature</div><div class="v" id="t2"></div></div>
|
||||||
|
<div class="kv"><div class="k">Pressure</div><div class="v" id="p2"></div></div>
|
||||||
|
<div class="kv"><div class="k">Time</div><div class="v" id="time2"></div></div>
|
||||||
|
<div class="kv"><div class="k">Date</div><div class="v" id="date2"></div></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
async function update(){
|
||||||
|
try{
|
||||||
|
const t = await (await fetch('/temp')).text();
|
||||||
|
const p = await (await fetch('/press')).text();
|
||||||
|
const ti = await (await fetch('/time')).text();
|
||||||
|
const d = await (await fetch('/date')).text();
|
||||||
|
|
||||||
|
temp.textContent = t;
|
||||||
|
press.textContent = p;
|
||||||
|
time.textContent = ti;
|
||||||
|
date.textContent = d;
|
||||||
|
|
||||||
|
t2.textContent = t;
|
||||||
|
p2.textContent = p;
|
||||||
|
time2.textContent = ti;
|
||||||
|
date2.textContent = d;
|
||||||
|
}catch(e){}
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
setInterval(update,1000);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user