diff --git a/code/espurna/broker.h b/code/espurna/broker.h index 7cf35002..7e6fee52 100644 --- a/code/espurna/broker.h +++ b/code/espurna/broker.h @@ -15,12 +15,12 @@ Copyright (C) 2017-2019 by Xose Pérez #include enum class TBrokerType { - SYSTEM, - STATUS, - SENSOR_READ, - SENSOR_REPORT, - DATETIME, - CONFIG + System, + Status, + SensorRead, + SensorReport, + Datetime, + Config }; template @@ -49,11 +49,11 @@ TBrokerCallbacks TBroker::callbacks; // --- Some known types. Bind them here to avoid .ino screwing with order --- -using StatusBroker = TBroker; +using StatusBroker = TBroker; -using SensorReadBroker = TBroker; -using SensorReportBroker = TBroker; +using SensorReadBroker = TBroker; +using SensorReportBroker = TBroker; -using ConfigBroker = TBroker; +using ConfigBroker = TBroker; #endif // BROKER_SUPPORT == 1 diff --git a/code/espurna/domoticz.h b/code/espurna/domoticz.h index 2073056f..d563ba2b 100644 --- a/code/espurna/domoticz.h +++ b/code/espurna/domoticz.h @@ -19,6 +19,7 @@ void domoticzSend(const char * key, T value); template void domoticzSend(const char * key, T nvalue, const char * svalue); +void domoticzSendMagnitude(unsigned char type, unsigned char index, double value, const char* buffer); void domoticzSendRelay(unsigned char relayID, bool status); void domoticzSendRelays(); diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 4e4b4912..5612f8fd 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -10,6 +10,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include "broker.h" #include "domoticz.h" +#include "sensor.h" #include "mqtt.h" #include "relay.h" @@ -194,6 +195,47 @@ void _domoticzBrokerCallback(const String& topic, unsigned char id, unsigned int #endif // BROKER_SUPPORT +#if SENSOR_SUPPORT + +void domoticzSendMagnitude(unsigned char type, unsigned char index, double value, const char* buffer) { + if (!_dcz_enabled) return; + + char key[15]; + snprintf_P(key, sizeof(key), PSTR("dczMagnitude%d"), index); + + // Domoticz expects some additional data, dashboard might break otherwise. + + // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Barometer + // TODO: Must send 'forecast' data. Default is last 3 hours: + // https://github.com/domoticz/domoticz/blob/6027b1d9e3b6588a901de42d82f3a6baf1374cd1/hardware/I2C.cpp#L1092-L1193 + // For now, just send invalid value. Consider simplifying sampling function and adding it here, with custom sampling time (3 hours, 6 hours, 12 hours etc.) + if (MAGNITUDE_PRESSURE == type) { + String svalue = buffer; + svalue += ";-1"; + domoticzSend(key, 0, svalue.c_str()); + // Special case to allow us to use it with switches directly + } else if (MAGNITUDE_DIGITAL == type) { + int nvalue = (buffer[0] >= 48) ? (buffer[0] - 48) : 0; + domoticzSend(key, nvalue, buffer); + // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Humidity + // svalue contains HUM_STAT, one of consts below + } else if (MAGNITUDE_HUMIDITY == type) { + const char status = 48 + ( + (value > 70) ? HUMIDITY_WET : + (value > 45) ? HUMIDITY_COMFORTABLE : + (value > 30) ? HUMIDITY_NORMAL : + HUMIDITY_DRY + ); + char svalue[2] = {status, '\0'}; + domoticzSend(key, buffer, svalue); + // Otherwise, send char string (nvalue is only for integers) + } else { + domoticzSend(key, 0, buffer); + } +} + +#endif // SENSOR_SUPPORT + #if WEB_SUPPORT bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) { @@ -246,11 +288,11 @@ void _domoticzConfigure() { template void domoticzSend(const char * key, T nvalue, const char * svalue) { if (!_dcz_enabled) return; - const auto idx = getSetting(key, 0); + const auto idx = getSetting(key, 0); if (idx > 0) { char payload[128]; snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); - mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); + mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); } } diff --git a/code/espurna/ntp.h b/code/espurna/ntp.h index 897dd171..3e8177c3 100644 --- a/code/espurna/ntp.h +++ b/code/espurna/ntp.h @@ -42,7 +42,7 @@ struct NtpCalendarWeekday { int utc_minute; }; -using NtpBroker = TBroker; +using NtpBroker = TBroker; String ntpDateTime(time_t ts); String ntpDateTime(); diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index aea12cfb..60ff73c5 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -12,6 +12,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include "broker.h" +#include "domoticz.h" #include "mqtt.h" #include "ntp.h" #include "relay.h" @@ -1859,30 +1860,10 @@ void _sensorReport(unsigned char index, double value) { #if THINGSPEAK_SUPPORT tspkEnqueueMeasurement(index, buffer); - #endif + #endif // THINGSPEAK_SUPPORT #if DOMOTICZ_SUPPORT - { - char key[15]; - snprintf_P(key, sizeof(key), PSTR("dczMagnitude%d"), index); - if (magnitude.type == MAGNITUDE_HUMIDITY) { - int status; - if (value > 70) { - status = HUMIDITY_WET; - } else if (value > 45) { - status = HUMIDITY_COMFORTABLE; - } else if (value > 30) { - status = HUMIDITY_NORMAL; - } else { - status = HUMIDITY_DRY; - } - char status_buf[5]; - itoa(status, status_buf, 10); - domoticzSend(key, buffer, status_buf); - } else { - domoticzSend(key, 0, buffer); - } - } + domoticzSendMagnitude(magnitude.type, index, value, buffer); #endif // DOMOTICZ_SUPPORT }