Browse Source

dcz: add workaround for pressure sensors (#2215)

- Domoticz only accepts svalue <pressure>;<forecast>,
  failing to display anything otherwise.
  We don't have any forecast calculation (yet?), simply send dummy value.
- Move Domoticz code outside of sensor.ino
mcspr-patch-1
Max Prokhorov 4 years ago
committed by GitHub
parent
commit
5de6330890
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 35 deletions
  1. +10
    -10
      code/espurna/broker.h
  2. +1
    -0
      code/espurna/domoticz.h
  3. +44
    -2
      code/espurna/domoticz.ino
  4. +1
    -1
      code/espurna/ntp.h
  5. +3
    -22
      code/espurna/sensor.ino

+ 10
- 10
code/espurna/broker.h View File

@ -15,12 +15,12 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <utility> #include <utility>
enum class TBrokerType { enum class TBrokerType {
SYSTEM,
STATUS,
SENSOR_READ,
SENSOR_REPORT,
DATETIME,
CONFIG
System,
Status,
SensorRead,
SensorReport,
Datetime,
Config
}; };
template <typename... TArgs> template <typename... TArgs>
@ -49,11 +49,11 @@ TBrokerCallbacks<TArgs...> TBroker<type, TArgs...>::callbacks;
// --- Some known types. Bind them here to avoid .ino screwing with order --- // --- Some known types. Bind them here to avoid .ino screwing with order ---
using StatusBroker = TBroker<TBrokerType::STATUS, const String&, unsigned char, unsigned int>;
using StatusBroker = TBroker<TBrokerType::Status, const String&, unsigned char, unsigned int>;
using SensorReadBroker = TBroker<TBrokerType::SENSOR_READ, const String&, unsigned char, double, const char*>;
using SensorReportBroker = TBroker<TBrokerType::SENSOR_REPORT, const String&, unsigned char, double, const char*>;
using SensorReadBroker = TBroker<TBrokerType::SensorRead, const String&, unsigned char, double, const char*>;
using SensorReportBroker = TBroker<TBrokerType::SensorReport, const String&, unsigned char, double, const char*>;
using ConfigBroker = TBroker<TBrokerType::CONFIG, const String&, const String&>;
using ConfigBroker = TBroker<TBrokerType::Config, const String&, const String&>;
#endif // BROKER_SUPPORT == 1 #endif // BROKER_SUPPORT == 1

+ 1
- 0
code/espurna/domoticz.h View File

@ -19,6 +19,7 @@ void domoticzSend(const char * key, T value);
template<typename T> template<typename T>
void domoticzSend(const char * key, T nvalue, const char * svalue); 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 domoticzSendRelay(unsigned char relayID, bool status);
void domoticzSendRelays(); void domoticzSendRelays();


+ 44
- 2
code/espurna/domoticz.ino View File

@ -10,6 +10,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "broker.h" #include "broker.h"
#include "domoticz.h" #include "domoticz.h"
#include "sensor.h"
#include "mqtt.h" #include "mqtt.h"
#include "relay.h" #include "relay.h"
@ -194,6 +195,47 @@ void _domoticzBrokerCallback(const String& topic, unsigned char id, unsigned int
#endif // BROKER_SUPPORT #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 #if WEB_SUPPORT
bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) { bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
@ -246,11 +288,11 @@ void _domoticzConfigure() {
template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) { template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
if (!_dcz_enabled) return; if (!_dcz_enabled) return;
const auto idx = getSetting<int>(key, 0);
const auto idx = getSetting(key, 0);
if (idx > 0) { if (idx > 0) {
char payload[128]; char payload[128];
snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
mqttSendRaw(getSetting<String>("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
} }
} }


+ 1
- 1
code/espurna/ntp.h View File

@ -42,7 +42,7 @@ struct NtpCalendarWeekday {
int utc_minute; int utc_minute;
}; };
using NtpBroker = TBroker<TBrokerType::DATETIME, const NtpTick, time_t, const String&>;
using NtpBroker = TBroker<TBrokerType::Datetime, const NtpTick, time_t, const String&>;
String ntpDateTime(time_t ts); String ntpDateTime(time_t ts);
String ntpDateTime(); String ntpDateTime();


+ 3
- 22
code/espurna/sensor.ino View File

@ -12,6 +12,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <float.h> #include <float.h>
#include "broker.h" #include "broker.h"
#include "domoticz.h"
#include "mqtt.h" #include "mqtt.h"
#include "ntp.h" #include "ntp.h"
#include "relay.h" #include "relay.h"
@ -1859,30 +1860,10 @@ void _sensorReport(unsigned char index, double value) {
#if THINGSPEAK_SUPPORT #if THINGSPEAK_SUPPORT
tspkEnqueueMeasurement(index, buffer); tspkEnqueueMeasurement(index, buffer);
#endif
#endif // THINGSPEAK_SUPPORT
#if DOMOTICZ_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 #endif // DOMOTICZ_SUPPORT
} }


Loading…
Cancel
Save