diff --git a/stofananas.ino b/stofananas.ino index 1d967ff..fa5a530 100644 --- a/stofananas.ino +++ b/stofananas.ino @@ -21,6 +21,7 @@ #include "fwupdate.h" #include "fsimage.h" #include "geolocate.h" +#include "stookwijzer.h" #define printf Serial.printf @@ -214,8 +215,10 @@ static int do_update(int argc, char *argv[]) const char *url = (argc > 1) ? argv[1] : "https://github.com/bertrik/stofananas/releases/latest/download/firmware.bin"; WiFiClientSecure client; + client.setInsecure(); + HTTPClient http; http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); if (http.begin(client, url)) { @@ -251,6 +254,13 @@ static int do_update(int argc, char *argv[]) return result; } +static int do_stook(int argc, char *argv[]) +{ + stookwijzer_get(52.01809622303658, 4.70760876719758); + + return 0; +} + const cmd_t commands[] = { { "help", do_help, "Show help" }, { "get", do_get, "[id] GET the PM2.5 value from stofradar.nl" }, @@ -261,6 +271,7 @@ const cmd_t commands[] = { { "led", do_led, " Set the LED to a specific value (hex)" }, { "unpack", do_unpack, " Unpack files" }, { "update", do_update, "[url] Update firmware from URL" }, + { "stook", do_stook, "Interact with stookwijzer" }, { NULL, NULL, NULL } }; @@ -347,6 +358,9 @@ void setup(void) latitude = savedata.latitude; longitude = savedata.longitude; } + + // stookwijzer + stookwijzer_begin(); } void loop(void) diff --git a/stookwijzer.cpp b/stookwijzer.cpp new file mode 100644 index 0000000..09ae58c --- /dev/null +++ b/stookwijzer.cpp @@ -0,0 +1,71 @@ +#include + +#include +#include + +#include + +#include "stookwijzer.h" + +#define printf Serial.printf + +static StaticJsonDocument < 256 > filter; // https://arduinojson.org/v6/assistant +static WiFiClientSecure client; +static HTTPClient http; + +void stookwijzer_begin(void) +{ + // allow https communication without actually checking certificates + client.setInsecure(); + + // configure HTTP client: follow redirects, non-chunked mode, disable connection re-use + http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); + http.useHTTP10(true); + http.setReuse(false); + + // pre-define the streaming JSON filter + filter["features"][0]["properties"]["pc4"] = true; + filter["features"][0]["properties"]["lki"] = true; + filter["features"][0]["properties"]["wind"] = true; + filter["features"][0]["properties"]["advies_0"] = true; + filter["features"][0]["properties"]["advies_6"] = true; + filter["features"][0]["properties"]["advies_12"] = true; + filter["features"][0]["properties"]["advies_18"] = true; + filter["features"][0]["properties"]["definitief_0"] = true; + filter["features"][0]["properties"]["definitief_6"] = true; + filter["features"][0]["properties"]["definitief_12"] = true; + filter["features"][0]["properties"]["definitief_18"] = true; + + printf("JSON filter:\n"); + serializeJsonPretty(filter, Serial); + printf("\n"); +} + +bool stookwijzer_get(double latitude, double longitude) +{ + double delta = 0.00001; + char url[300]; + sprintf(url, "https://data.rivm.nl/geo/alo/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo" + "&QUERY_LAYERS=stookwijzer_v2&LAYERS=stookwijzer_v2&info_format=application/json&feature_count=1" + "&I=0&J=0&WIDTH=1&HEIGHT=1&CRS=CRS:84&BBOX=%.5f,%.5f,%.5f,%.5f", longitude - delta, + latitude - delta, longitude + delta, latitude + delta); + + bool result = false; + if (http.begin(client, url)) { + printf("GET %s... ", url); + int httpCode = http.GET(); + printf("%d\n", httpCode); + if (httpCode == HTTP_CODE_OK) { + DynamicJsonDocument doc(2048); + printf("Deserializing from HTTP... "); + DeserializationError error = + deserializeJson(doc, http.getStream(), DeserializationOption::Filter(filter)); + printf("%s\n", error.c_str()); + serializeJsonPretty(doc, Serial); + printf("\n"); + result = true; + } + http.end(); + } + return result; +} diff --git a/stookwijzer.h b/stookwijzer.h new file mode 100644 index 0000000..90b60c2 --- /dev/null +++ b/stookwijzer.h @@ -0,0 +1,6 @@ +#include + +void stookwijzer_begin(void); + +bool stookwijzer_get(double lat, double lon); +