I'm trying to parse the JSON string from the National Weather Service Alerts and am encountering a problem where the code can't parse the value and the device auto-resets. I've used this code before on a different API to get the current weather conditions, and that works fine - I'm not sure what I'm doing wrong and would greatly appreciate any advice or help. Thanks!
Here is the error:
21:13:07.521 -> wdt reset 21:13:07.521 -> load 0x4010f000, len 3424, room 16 21:13:07.521 -> tail 0 21:13:07.521 -> chksum 0x2e 21:13:07.521 -> load 0x3fff20b8, len 40, room 8 21:13:07.521 -> tail 0 21:13:07.521 -> chksum 0x2b 21:13:07.521 -> csum 0x2b 21:13:07.521 -> v00045d00 21:13:07.521 -> ~ld 21:13:07.555 -> ����n�r��n|�l�l`b��|r�l�n��n�l`��r�l�l��
And here's my code:
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const char* ssid = "mySSID"; // SSID of local network
const char* password = "myPassword"; // Password on network
WiFiClient client;
char weatherServerName[] = "api.weather.gov";
String result;
//For testing change this zone to any zone from the NWS that's currently experiencing a weather alert
//Find an active alert area by going to https://api.weather.gov/alerts/active and looking for the code in the "affectedZones"
String zone = "LEZ144";
bool hasAlert = false;
void setup() {
Serial.begin(115200);
Serial.println("=====================================================================================================");
Serial.println("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connected");
delay(1000);
}
void loop() {
Serial.println(":D");
getWeatherAlerts();
delay(100000);
}
void getWeatherAlerts()
{
String server = weatherServerName;
if (client.connect(server, 443))
{
client.println("GET /alerts/active?zone=" + zone);
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
//waits for data
while (client.connected() || client.available())
{ //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}
client.stop(); //stop client
result.replace('[', ' ');
result.replace(']', ' ');
Serial.println(result);
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '\0';
StaticJsonDocument<8196> root;
DeserializationError error = deserializeJson(root, jsonArray);
if (error)
{
Serial.println("parseObject() failed");
}
String features = root["features"];
Serial.println(features);
if(features.length() == 0)
{
Serial.println(features);
hasAlert = false;
}
else
{
String severity = root["features"]["severity"];
Serial.println(severity);
if(severity == "Moderate" || severity == "Severe")
{
hasAlert = true;
}
else
{
hasAlert = false;
}
}
Serial.println(hasAlert);
}