#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Twoja sieć testowa (Wokwi ma wirtualne Wi-Fi – możesz wpisać cokolwiek)
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Reverse geocoding przez OpenStreetMap / Nominatim
String getLocationNameFromCoords(double lat, double lon) {
if (WiFi.status() != WL_CONNECTED)
return "Brak WiFi";
HTTPClient http;
String url = "https://nominatim.openstreetmap.org/reverse?format=json" "&lat=" + String(lat, 6) + "&lon=" + String(lon, 6);
http.begin(url);
http.addHeader("User-Agent", "ESP32-MoonTracker"); // Wymagane przez API
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
http.end();
DynamicJsonDocument doc(4096);
DeserializationError err = deserializeJson(doc, payload);
if (err) {
Serial.println("JSON error!");
return "JSON error";
}
// Pobierz nazwę miejscowości z różnych pól
const char* city = doc["address"]["city"];
if (!city) city = doc["address"]["town"];
if (!city) city = doc["address"]["village"];
if (!city) city = doc["address"]["municipality"];
if (!city) city = doc["address"]["county"];
if (city) {
Serial.print("Miasto: ");
Serial.println(city);
return String(city);
}
return "Brak nazwy";
} else {
Serial.printf("HTTP error %d\n", httpCode);
}
http.end();
return "Błąd API";
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Łączenie z WiFi...");
WiFi.begin(ssid, password);
int retry = 0;
while (WiFi.status() != WL_CONNECTED && retry < 20) {
delay(500);
Serial.print(".");
retry++;
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Połączono z WiFi!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
// Test na współrzędnych (Caino, IT)
double lat = 45.6148;
double lon = 10.3287;
// Test na współrzędnych (Portomaggiore, IT)
///double lat = 44.7;
///double lon = 11.8;
// Test na współrzędnych (Grudziądz, PL)
///double lat = 53.488056;
///double lon = 18.753056;
String city = getLocationNameFromCoords(lat, lon);
Serial.print("Lokalizacja: ");
Serial.println(city);
} else {
Serial.println("Brak połączenia z WiFi.");
}
}
void loop() {
// nic
}