Code: Select all
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <math.h>
#include <WebServer.h>
// ---- WiFi ----
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
// ---- OpenWeatherMap ----
#define API_KEY "xxx"
#define LAT "53.1235"
#define LON "18.0076"
// ---- Dane pogodowe ----
float temperature = 0;
float humidity = 0;
float pressure = 0;
float dew_point = 0;
float wind_speed = 0;
float clouds = 0;
float visibility = 0;
float rain = 0;
float snow = 0;
float uv_index = 0;
// ---- WebServer ----
WebServer server(80);
void handleRoot() {
String html = "<!DOCTYPE html><html><head><meta charset='utf-8'>";
html += "<meta http-equiv='refresh' content='30'>"; // auto-refresh co 30s
html += "<title>Dane pogodowe ESP32</title>";
html += "<style>body{font-family:Arial;background:#f2f2f2;text-align:center;} ";
html += "h1{color:#333;} table{margin:auto;border-collapse:collapse;} ";
html += "td,th{border:1px solid #888;padding:8px;}</style></head><body>";
html += "<h1>Aktualne dane pogodowe</h1>";
html += "<table>";
html += "<tr><th>Parametr</th><th>Wartość</th></tr>";
html += "<tr><td>Temperatura</td><td>" + String(temperature, 2) + " °C</td></tr>";
html += "<tr><td>Wilgotność</td><td>" + String(humidity, 2) + " %</td></tr>";
html += "<tr><td>Ciśnienie</td><td>" + String(pressure, 2) + " hPa</td></tr>";
html += "<tr><td>Punkt rosy</td><td>" + String(dew_point, 2) + " °C</td></tr>";
html += "<tr><td>Wiatr</td><td>" + String(wind_speed, 2) + " m/s</td></tr>";
html += "<tr><td>Zachmurzenie</td><td>" + String(clouds, 2) + " %</td></tr>";
html += "<tr><td>Widoczność</td><td>" + String(visibility, 2) + " m</td></tr>";
html += "<tr><td>Deszcz</td><td>" + String(rain, 2) + " l/m²</td></tr>";
html += "<tr><td>Śnieg</td><td>" + String(snow, 2) + " l/m²</td></tr>";
html += "<tr><td>UV index</td><td>" + String(uv_index, 2) + "</td></tr>";
html += "</table>";
html += "<p><i>Odświeżanie co 30 sekund</i></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void fetchWeather() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("[BŁĄD] Brak połączenia z WiFi!");
return;
}
HTTPClient http;
String url = "https://api.openweathermap.org/data/2.5/weather?lat=" + String(LAT) + "&lon=" + String(LON) + "&appid=" + API_KEY + "&units=metric";
Serial.println("[INFO] Pobieram dane pogodowe...");
http.begin(url);
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
DynamicJsonDocument doc(4096);
if (deserializeJson(doc, payload) == DeserializationError::Ok) {
temperature = doc["main"]["temp"].as<float>();
humidity = doc["main"]["humidity"].as<float>();
pressure = doc["main"]["pressure"].as<float>();
wind_speed = doc["wind"]["speed"].as<float>();
clouds = doc["clouds"]["all"].as<float>();
visibility = doc["visibility"].as<float>();
if (doc.containsKey("rain") && doc["rain"]["1h"])
rain = doc["rain"]["1h"].as<float>() * 100;
else
rain = 0;
if (doc.containsKey("snow") && doc["snow"]["1h"])
snow = doc["snow"]["1h"].as<float>() * 100;
else
snow = 0;
dew_point = (237.7 * log(humidity / 100.0) + (17.27 * temperature)) /
(17.27 - log(humidity / 100.0));
}
}
http.end();
// UV index
url = "https://api.openweathermap.org/data/2.5/uvi?lat=" + String(LAT) + "&lon=" + String(LON) + "&appid=" + API_KEY;
Serial.println("[INFO] Pobieram UV index...");
http.begin(url);
httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
if (deserializeJson(doc, payload) == DeserializationError::Ok) {
uv_index = doc["value"].as<float>();
}
}
http.end();
// Debug do konsoli
Serial.println("=== Dane pogodowe z OpenWeatherMap ===");
Serial.printf("Temperatura: %.2f °C\n", temperature);
Serial.printf("Wilgotność: %.2f %%\n", humidity);
Serial.printf("Ciśnienie: %.2f hPa\n", pressure);
Serial.printf("Punkt rosy: %.2f °C\n", dew_point);
Serial.printf("Prędkość wiatru: %.2f m/s\n", wind_speed);
Serial.printf("Zachmurzenie: %.2f %%\n", clouds);
Serial.printf("Widoczność: %.2f m\n", visibility);
Serial.printf("Deszcz: %.2f l/m²\n", rain);
Serial.printf("Śnieg: %.2f l/m²\n", snow);
Serial.printf("UV index: %.2f\n", uv_index);
Serial.println("=====================================");
}
unsigned long lastUpdate = 0;
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\n[START] Uruchamianie ESP32...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.printf("[INFO] Łączenie z WiFi: %s\n", WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n[OK] Połączono z WiFi!");
Serial.print("[INFO] Adres IP: ");
Serial.println(WiFi.localIP());
// Serwer WWW
server.on("/", handleRoot);
server.begin();
Serial.println("[OK] Serwer WWW uruchomiony");
fetchWeather(); // pierwsze pobranie
lastUpdate = millis();
}
void loop() {
server.handleClient();
if (millis() - lastUpdate > 600000) { // co 10 minut
fetchWeather();
lastUpdate = millis();
}
}
