Skip to content

Commit

Permalink
Initial stookwijzer support.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Jan 8, 2025
1 parent 106a19f commit 1b71d08
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
14 changes: 14 additions & 0 deletions stofananas.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "fwupdate.h"
#include "fsimage.h"
#include "geolocate.h"
#include "stookwijzer.h"

#define printf Serial.printf

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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" },
Expand All @@ -261,6 +271,7 @@ const cmd_t commands[] = {
{ "led", do_led, "<RRGGBB> Set the LED to a specific value (hex)" },
{ "unpack", do_unpack, "<force> Unpack files" },
{ "update", do_update, "[url] Update firmware from URL" },
{ "stook", do_stook, "Interact with stookwijzer" },
{ NULL, NULL, NULL }
};

Expand Down Expand Up @@ -347,6 +358,9 @@ void setup(void)
latitude = savedata.latitude;
longitude = savedata.longitude;
}

// stookwijzer
stookwijzer_begin();
}

void loop(void)
Expand Down
71 changes: 71 additions & 0 deletions stookwijzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdbool.h>

#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>

#include <WiFiClientSecure.h>

#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;
}
6 changes: 6 additions & 0 deletions stookwijzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdbool.h>

void stookwijzer_begin(void);

bool stookwijzer_get(double lat, double lon);

0 comments on commit 1b71d08

Please sign in to comment.