#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const String url = "https://environment.data.gov.uk/flood-monitoring/id/measures/L1803-level-stage-i-15_min-m";
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP=");
Serial.println(WiFi.localIP());
Serial.print("Fetching " + url + "... ");
Serial.println();
HTTPClient http;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP httpResponseCode #");
Serial.print(httpResponseCode);
Serial.println("#");
String payload = http.getString();
Serial.print("payload #");
Serial.print(payload);
Serial.println("#");
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
JsonObject item = doc["items"];
const char* label = item["label"]; // "Chapel Haddlesey - level-stage-i-15_min-m"
JsonObject latestReading = item["latestReading"];
const char* dateTime = latestReading["dateTime"]; // "2025-04-05T07:45:00Z"
float levelValue = latestReading["value"]; // -0.005
Serial.printf("Station; %s\nLatest reading (%s): %2.3f meters\n", label, dateTime, levelValue);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void loop() {
delay(100);
}