use builtin NaN for 0 voltage
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Nicu Hodos 2022-10-13 12:13:13 +02:00
parent 0143191cb5
commit c084a8c5be
2 changed files with 10 additions and 10 deletions

View File

@ -1,8 +1,10 @@
#include <Arduino.h>
#include <RCSwitch.h>
#include <Adafruit_Sensor.h>
#include <ArduinoJson.h>
#include "Tiny.h"
#define ARDUINOJSON_ENABLE_NAN 1
#include <ArduinoJson.h>
#include "Dht.h"
@ -48,20 +50,18 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
JsonObject sensor = jsonDoc.createNestedObject("sensor");
float voltage = (float)GET_VCC(value)/1000;
if (voltage != 0) {
sensor["voltage"] = voltage;
}
unsigned long voltage = GET_VCC(value);
sensor["voltage"] = voltage == 0 ? NAN : (float)voltage / 1000;
switch (GET_TYPE(value)) {
case SensorType::GENERIC:
sensor["value"] = GET_VALUE(value);
break;
case SensorType::TEMPERATURE:
sensor["temperature"] = (float)GET_TEMP(value)/10;
sensor["temperature"] = (float)GET_TEMP(value) / 10;
break;
case SensorType::HUMIDITY:
sensor["humidity"] = (float)GET_HUMIDITY(value)/10;
sensor["humidity"] = (float)GET_HUMIDITY(value) / 10;
break;
case SensorType::CONTACT:
sensor["state"] = GET_STATE(value) ? "on" : "off";
@ -69,7 +69,7 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
default:
return false;
}
return true;
}
@ -110,7 +110,7 @@ void runJsonCommand(String cmd) {
DeserializationError err = deserializeJson(jsonArray, cmd);
if (err == DeserializationError::Ok) {
JsonArray array = jsonArray.as<JsonArray>();
for(JsonVariant jsonDoc : array) {
for (JsonVariant jsonDoc : array) {
if (jsonDoc.containsKey("rcSwitch")) {
JsonObject rcSwitch = jsonDoc["rcSwitch"];
unsigned int protocol = rcSwitch["protocol"];

View File

@ -13,7 +13,7 @@
#define GET_TEMP(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_VCC(value) ((value >> 5) & 0x1FFF)
#define GET_VCC(value) (((unsigned long)value >> 5) & 0x1FFF)
enum SensorType {
GENERIC = 4,