I get a blank result when filtering for main weather description and temperature. Is this normal? I have ArduinoJson 16.9.4 installed
Post your JSON and filter. A picture of part of each isn't very helpful.
It looks like you might have "main" and "weather" inside an array that doesn't exist. Try this filter:
{
"main": {
"temp": true
},
"weather": [
{
"description": true
}
]
}
That filter works for me on this output from OpenWeatherMap.org:
{
"coord": {
"lon": 77.75,
"lat": 20.9333
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}
],
"base": "stations",
"main": {
"temp": 301.45,
"feels_like": 303.3,
"temp_min": 301.45,
"temp_max": 301.45,
"pressure": 1004,
"humidity": 62,
"sea_level": 1004,
"grnd_level": 966
},
"visibility": 10000,
"wind": {
"speed": 3.14,
"deg": 293,
"gust": 7.39
},
"clouds": {
"all": 94
},
"dt": 1628958953,
"sys": {
"country": "IN",
"sunrise": 1628900858,
"sunset": 1628947200
},
"timezone": 19800,
"id": 1278718,
"name": "Amrāvati",
"cod": 200
}
Sorry - new at this!
Yes, that was the problem. Onwards to the next hurdle
Many thanks
Or take the Bodmer OpenWheater library with lightweight json decoder.
With ESP8266 and ILI9486 4" 480*320 OLED.
My code now compiles but I have an error deserializeJson() failed: InvalidInput
in the serial monitor. My code is below. Am I putting the doc text in the wrong place?
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID1;
char pass[] = SECRET_PSW1;
String apiKey= SECRET_APIKEY; //SECRET_APIKEY;
String location= "SE26,GB";
//generate time for the cos move
unsigned long timer = 0;
unsigned long previousTimer = 0;
boolean updateWeather = true;
int status = WL_IDLE_STATUS;
char server[] = "api.openweathermap.org";
WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// check if firmware is up-to-date
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 1 second for connection:
delay(1000);
}
Serial.println("Connected to wifi");
printWifiStatus();
httpRequest();
}
void loop() {
timer = millis(); // updating time
if(updateWeather == true){
// getWeather();
httpRequest();
updateWeather = false;
}
//if (timer - previousTimer >= 30000){ // check weather every 30s
if (timer - previousTimer >= 60000){ // check weather every 60s
// if (timer - previousTimer >= 1800000){ // check weather every 30 mins
previousTimer = timer;
updateWeather = true;
}
getWeather();
StaticJsonDocument<80> filter;
filter["main"]["temp"] = true;
filter["weather"][0]["description"] = true;
StaticJsonDocument<192> doc;
char input[] = "{\"coord\":{\"lon\":-0.0524,\"lat\":51.4268},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"base\":\"stations\",\"main\":{\"temp\":1.87,\"feels_like\":-3.59,\"temp_min\":0.55,\"temp_max\":2.79,\"pressure\":1017,\"humidity\":87},\"visibility\":8000,\"wind\":{\"speed\":7.2,\"deg\":160},\"rain\":{\"1h\":0.69\clouds\":{\"all\":20},\"dt\":1671362503,\"sys\":{\"type\":2,\"id\":2019646,\"country\":\"GB\",\"sunrise\":1671350476,\"sunset\":1671378735},\"timezone\":0,\"id\":2643741,\"name\":\"Westminster\",\"cod\":200}";
DeserializationError error = deserializeJson(doc, input, DeserializationOption::Filter(filter));
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
const char* weather_0_description = doc["weather"][0]["description"]; // "light rain"
float main_temp = doc["main"]["temp"]; // 1.87
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void httpRequest() {
// attempt to connect to the openweathermap server
Serial.println("\nStarting connection to server...");
// If you get a connection, report back via serial...
if (client.connect(server, 80)) {
Serial.println("connected to server");
// ... and make a HTTP request:
client.println("GET /data/2.5/weather?q=" + location + "&units=metric&APPID=" + apiKey); //metric ok
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
}
else {
Serial.println("unable to connect"); // if you couldn't make a connection
}
}
void getWeather() {
String line = "";
while (client.connected()) {
line = client.readStringUntil('\n');
Serial.println(line);
}
}
Use this
extended with your apikey to test. You can use it in a browser too.
Without the exlude you become a much longer JSon data.
This part looks wrong. Try changing it to:
\"rain\":{\"1h\":0.69,\"clouds\":
Ok, that gives me a slightly different error: deserializeJson() failed: IncompleteInput
A mistake in my termination perhaps?
Going cross-eyed looking through it for the error - well done spotting that one!
I think you are missing a } somewhere. Adding one at the end solves the syntax error.
Rather than escaping all of those quote marks, I suggest you put your JSON document in a "Raw String Literal"
https://en.cppreference.com/w/cpp/language/string_literal
char input[] = R"MyJSON({
"coord": {
"lon": -0.0524,
"lat": 51.4268
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"base": "stations",
"main": {
"temp": 1.87,
"feels_like": -3.59,
"temp_min": 0.55,
"temp_max": 2.79,
"pressure": 1017,
"humidity": 87
},
"visibility": 8000,
"wind": {
"speed": 7.2,
"deg": 160
},
"rain": {
"1h": 0.69,
"clouds": {
"all": 20
},
"dt": 1671362503,
"sys": {
"type": 2,
"id": 2019646,
"country": "GB",
"sunrise": 1671350476,
"sunset": 1671378735
},
"timezone": 0,
"id": 2643741,
"name": "Westminster",
"cod": 200
}
})MyJSON";
That first solution prints the full json return in the serial monitor but doesn't run the filter and the second compiles but takes me back to invalidInput.
Is there an example or tutorial you can recommend so I'm not bugging you line by line?
Cheers!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.