Skip to content

Commit

Permalink
dcz: add workaround for pressure sensors (#2215)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mcspr committed Apr 7, 2020
1 parent cae50fa commit 5de6330
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
20 changes: 10 additions & 10 deletions code/espurna/broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <utility>

enum class TBrokerType {
SYSTEM,
STATUS,
SENSOR_READ,
SENSOR_REPORT,
DATETIME,
CONFIG
System,
Status,
SensorRead,
SensorReport,
Datetime,
Config
};

template <typename... TArgs>
Expand Down Expand Up @@ -49,11 +49,11 @@ TBrokerCallbacks<TArgs...> TBroker<type, TArgs...>::callbacks;

// --- 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
1 change: 1 addition & 0 deletions code/espurna/domoticz.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void domoticzSend(const char * key, T value);
template<typename T>
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();

Expand Down
46 changes: 44 additions & 2 deletions code/espurna/domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>

#include "broker.h"
#include "domoticz.h"
#include "sensor.h"
#include "mqtt.h"
#include "relay.h"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -246,11 +288,11 @@ void _domoticzConfigure() {

template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
if (!_dcz_enabled) return;
const auto idx = getSetting<int>(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<String>("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/espurna/ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct NtpCalendarWeekday {
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();
Expand Down
25 changes: 3 additions & 22 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <float.h>

#include "broker.h"
#include "domoticz.h"
#include "mqtt.h"
#include "ntp.h"
#include "relay.h"
Expand Down Expand Up @@ -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

}
Expand Down

0 comments on commit 5de6330

Please sign in to comment.