Unquoted NaN is not a valid JSON value
Created by: glrs
I have noticed that once I unplug a DHT sensor from arduino, and then try to read from it, it returns nan temp = nan
& hum = nan
. With the next piece of code, I create a JSON object and send it to RPi. When the sensor is disconnected, the returned nan values are transformed into NaN (unquoted) by the ArduinoJson. According to json.org the only acceptable unquoted value for a missing/undefined value is null. Passing unquoted NaN, causes problems to parse that JSON object - at least - in Javascript.
Here is part of my code:
#include <ArduinoJson.h>
#include <DHT.h>
#include <DHT_U.h>
float hum;
float temp;
...
void loop() {
hum = dht.readHumidity();
temp= dht.readTemperature();
StaticJsonDocument<200> root;
root["AirTemp"] = temp;
root["Humidity"] = hum;
serializeJson(root, Serial);
delay(30000);
}
I suggest to either pass "NaN" as string (I mean quoted, not String datatype necessarily), or just pass null.