use builtin NaN for 0 voltage

This commit is contained in:
Nicu Hodos 2022-10-13 12:13:13 +02:00
parent 1a8ddfacae
commit 41e777f586
2 changed files with 10 additions and 10 deletions

View File

@ -1,8 +1,10 @@
#include <Arduino.h> #include <Arduino.h>
#include <RCSwitch.h> #include <RCSwitch.h>
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <ArduinoJson.h>
#include "Tiny.h" #include "Tiny.h"
#define ARDUINOJSON_ENABLE_NAN 1
#include <ArduinoJson.h>
#include "Dht.h" #include "Dht.h"
@ -48,10 +50,8 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
JsonObject sensor = jsonDoc.createNestedObject("sensor"); JsonObject sensor = jsonDoc.createNestedObject("sensor");
float voltage = (float)GET_VCC(value)/1000; unsigned long voltage = GET_VCC(value);
if (voltage != 0) { sensor["voltage"] = voltage == 0 ? NAN : (float)voltage / 1000;
sensor["voltage"] = voltage;
}
switch (GET_TYPE(value)) { switch (GET_TYPE(value)) {
case SensorType::GENERIC: case SensorType::GENERIC:

View File

@ -13,7 +13,7 @@
#define GET_TEMP(value) (((unsigned long)value >> 18) & 0x2FF) #define GET_TEMP(value) (((unsigned long)value >> 18) & 0x2FF)
#define GET_HUMIDITY(value) (((unsigned long)value >> 18) & 0x2FF) #define GET_HUMIDITY(value) (((unsigned long)value >> 18) & 0x2FF)
#define GET_VALUE(value) (((unsigned long)value >> 18) & 0x2FF) #define GET_VALUE(value) (((unsigned long)value >> 18) & 0x2FF)
#define GET_VCC(value) ((value >> 5) & 0x1FFF) #define GET_VCC(value) (((unsigned long)value >> 5) & 0x1FFF)
enum SensorType { enum SensorType {
GENERIC = 4, GENERIC = 4,