ArduionJson 6 bits

This commit is contained in:
Paul Warren 2022-12-06 22:41:37 +11:00
parent a8a28f0af2
commit 4b93757d38
1 changed files with 41 additions and 35 deletions

View File

@ -46,7 +46,7 @@ WiFiUDP ntpUDP;
HTTPClient http; HTTPClient http;
NTPClient timeClient(ntpUDP, "pi.lan", 0, 60000); //use internal ntp server, update every 10 minutes, taking in to account the 9 seconds it takes to update the screen for the current time NTPClient timeClient(ntpUDP, "pi.lan", 0, 60000); //use internal ntp server, update every 10 minutes, taking in to account the 9 seconds it takes to update the screen for the current time
//NTPClient timeClient(ntpUDP, "au.pool.ntp.org", 0, 60000); //use internal ntp server, update every 10 minutes //NTPClient timeClient(ntpUDP, "au.pool.ntp.org", 0, 60000); //use external ntp server, update every 10 minutes
// defined in "credentials.h" // defined in "credentials.h"
const char* ssid = ssid_name; // The SSID (name) of your Wi-Fi network const char* ssid = ssid_name; // The SSID (name) of your Wi-Fi network
@ -250,53 +250,59 @@ void printDateTime(int update_epd)
void weatherUpdate(void) { void weatherUpdate(void) {
char buf[64]; char buf[64];
const size_t capacity = 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 150; const size_t capacity = 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 150;
DynamicJsonBuffer jsonBuffer(capacity); DynamicJsonDocument jsonBuffer(capacity);
http.begin(wfclient, ds_url); http.begin(wfclient, ds_url);
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode > 0) { if (httpCode > 0) {
JsonObject& root = jsonBuffer.parseObject(http.getString()); /* ArduinoJson v5 bits JsonObject& root = jsonBuffer.parseObject(http.getString());
if (!root.success()) { if (!root.success()) {
Serial.print(F("parseObject() failed: ")); Serial.print(F("parseObject() failed: "));
return; */
auto error = deserializeJson(jsonBuffer, http.getString());
if (error) {
Serial.print("Failed to parse json");
return; return;
} }
http.end(); http.end();
JsonObject& today = root["today"];
const char* today_day = today["day"]; // "Wednesday"
const char* today_icon = today["icon"]; // "sunny.jpg"
const char* today_max = today["max"]; // "27"
JsonObject& tomorrow = root["tomorrow"]; //JsonObject& today = jsonBuffer["today"];
const char* tomorrow_day = tomorrow["day"]; // "Thursday" const char* today_day = jsonBuffer["today"]["day"]; // "Wednesday"
const char* tomorrow_icon = tomorrow["icon"]; // "sunny.jpg" const char* today_icon = jsonBuffer["today"]["icon"]; // "sunny.jpg"
const char* tomorrow_max = tomorrow["max"]; // "31" const char* today_max = jsonBuffer["today"]["max"]; // "27"
//JsonObject& tomorrow = jsonBuffer["tomorrow"];
const char* tomorrow_day = jsonBuffer["tomorrow"]["day"]; // "Thursday"
const char* tomorrow_icon = jsonBuffer["tomorrow"]["icon"]; // "sunny.jpg"
const char* tomorrow_max = jsonBuffer["tomorrow"]["max"]; // "31"
//JsonObject& day_after = jsonBuffer["day_after"];
const char* day_after_day = jsonBuffer["day_after"]["day"]; // "Friday"
const char* day_after_icon = jsonBuffer["day_after"]["icon"]; // "pcloud.jpg"
const char* day_after_max = jsonBuffer["day_after"]["max"]; // "33"
//JsonObject& current = jsonBuffer["current"];
current_outside = jsonBuffer["current"]["outside"];
current_bedroom = jsonBuffer["current"]["bedroom"];
current_lounge = jsonBuffer["current"]["lounge"];
JsonObject& day_after = root["day_after"];
const char* day_after_day = day_after["day"]; // "Friday"
const char* day_after_icon = day_after["icon"]; // "pcloud.jpg"
const char* day_after_max = day_after["max"]; // "33"
JsonObject& current = root["current"];
current_outside = current["outside"];
current_bedroom = current["bedroom"];
current_lounge = current["lounge"];
Serial.println("Updating EPD with weather"); Serial.println("Updating EPD with weather");
epd_set_en_font(ASCII64); epd_set_en_font(ASCII64);
//Today //Today
epd_disp_bitmap(today_icon, 0, 290); epd_disp_bitmap(today_icon, 0, 290);
epd_disp_string(today_day, 0, 536); epd_disp_string(today_day, 0, 536);
epd_disp_string(today_max, 75, 460); epd_disp_string(today_max, 75, 460);
//Tomorrow //Tomorrow
epd_disp_bitmap(tomorrow_icon, 267, 290); epd_disp_bitmap(tomorrow_icon, 267, 290);
epd_disp_string(tomorrow_day, 260, 536); epd_disp_string(tomorrow_day, 260, 536);
epd_disp_string(tomorrow_max, 352, 460); epd_disp_string(tomorrow_max, 352, 460);
//Day After //Day After
epd_disp_bitmap(day_after_icon, 533, 290); epd_disp_bitmap(day_after_icon, 533, 290);
epd_disp_string(day_after_day, 533, 536); epd_disp_string(day_after_day, 533, 536);
@ -305,17 +311,17 @@ void weatherUpdate(void) {
Serial.println("Adding Temps"); Serial.println("Adding Temps");
sprintf(buf, "E: %s I: %s", sprintf(buf, "E: %s I: %s",
current_outside, current_outside,
current_lounge); current_lounge);
Serial.println(buf); Serial.println(buf);
epd_disp_string(buf, 500, 225); epd_disp_string(buf, 500, 225);
} else { } else {
Serial.print("Error on HTTP Request: "); Serial.print("Error on HTTP Request: ");
Serial.println(httpCode); Serial.println(httpCode);
} }
} }
void loop() { void loop() {