mqttPublishSwitcher
This commit is contained in:
parent
920c78985d
commit
b87b45d22f
@ -7,11 +7,11 @@
|
||||
#define WINDOWS_GPIO D6
|
||||
#define LIGHT_GPIO D7
|
||||
|
||||
Input door("greenhouse/door", DOOR_GPIO, true, "DOOR_BOOLEAN");
|
||||
Input door("greenhouse/door", "DOOR",DOOR_GPIO, true);
|
||||
|
||||
Input windows("greenhouse/windows", WINDOWS_GPIO, true, "WINDOW_BOOLEAN");
|
||||
Input windows("greenhouse/windows", "WINDOW", WINDOWS_GPIO, true);
|
||||
|
||||
Output light("greenhouse/light", LIGHT_GPIO, true, "LIGHT_BOOLEAN");
|
||||
Output light("greenhouse/light", "LIGHT",LIGHT_GPIO, true);
|
||||
|
||||
TSL2561 greenhouseTSL("greenhouse");
|
||||
|
||||
|
||||
@ -11,8 +11,6 @@ class Input {
|
||||
|
||||
const boolean inverted;
|
||||
|
||||
const char* unit;
|
||||
|
||||
unsigned long debounceMs;
|
||||
|
||||
unsigned long sendMs;
|
||||
@ -27,7 +25,15 @@ public:
|
||||
|
||||
const String name;
|
||||
|
||||
explicit Input(String name, const int pin, const boolean inverted, const char* unit, const unsigned long debounceMs = 500, const unsigned long sendMs = 5000) : pin(pin), inverted(inverted), unit(unit), debounceMs(debounceMs), sendMs(sendMs), name(std::move(name)) {
|
||||
const String category;
|
||||
|
||||
explicit Input(String name, String category, const int pin, const boolean inverted, const unsigned long debounceMs = 500, const unsigned long sendMs = 5000) :
|
||||
pin(pin),
|
||||
inverted(inverted),
|
||||
debounceMs(debounceMs),
|
||||
sendMs(sendMs),
|
||||
name(std::move(name)),
|
||||
category(std::move(category)) {
|
||||
//
|
||||
}
|
||||
|
||||
@ -49,7 +55,7 @@ public:
|
||||
|
||||
if (changed || lastSent == 0 || now - lastSent >= sendMs) {
|
||||
lastSent = now;
|
||||
mqttPublishValue(name, state, unit);
|
||||
mqttPublishSwitcher(name, category, state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -11,8 +11,6 @@ class Output {
|
||||
|
||||
const boolean inverted;
|
||||
|
||||
const char* unit;
|
||||
|
||||
unsigned long sendMs;
|
||||
|
||||
unsigned long lastSent = 0UL;
|
||||
@ -23,7 +21,14 @@ public:
|
||||
|
||||
const String name;
|
||||
|
||||
explicit Output(String name, const int pin, const boolean inverted, const char* unit, const unsigned long sendMs = 5000) : pin(pin), inverted(inverted), unit(unit), sendMs(sendMs), name(std::move(name)) {
|
||||
const String category;
|
||||
|
||||
explicit Output(String name, String category, const int pin, const boolean inverted, const unsigned long sendMs = 5000) :
|
||||
pin(pin),
|
||||
inverted(inverted),
|
||||
sendMs(sendMs),
|
||||
name(std::move(name)),
|
||||
category(std::move(category)) {
|
||||
//
|
||||
}
|
||||
|
||||
@ -37,7 +42,7 @@ public:
|
||||
if (changed || lastSent == 0 || now - lastSent >= sendMs) {
|
||||
lastSent = now;
|
||||
digitalWrite(pin, inverted ^ state);
|
||||
mqttPublishValue(name, state, unit);
|
||||
mqttPublishSwitcher(name, category, state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -97,6 +97,17 @@ void mqttPublishValue(const String& name, const double value, const char* unit)
|
||||
mqttPublishValue(name, String(value), unit);
|
||||
}
|
||||
|
||||
void mqttPublishSwitcher(const String& name, const String& category, const boolean state) {
|
||||
char buffer[200];
|
||||
snprintf(buffer, sizeof buffer, R"({"name": "%s", "type": "SWITCHER", "category": "%s", "timestamp": %lld, "state": %s})",
|
||||
name.c_str(),
|
||||
category.c_str(),
|
||||
time(nullptr),
|
||||
state ? "true" : "false"
|
||||
);
|
||||
mqttPublish(name + "/type/SWITCHER", buffer);
|
||||
}
|
||||
|
||||
void mqttPublishValue(const String& name, const String& value, const char* unit) {
|
||||
char buffer[200];
|
||||
snprintf(buffer, sizeof buffer, R"({"name": "%s", "timestamp": %lld, "value": %s, "unit": "%s"})", name.c_str(), time(nullptr), value.c_str(), unit);
|
||||
|
||||
@ -44,6 +44,8 @@ void mqttPublishValue(const String& name, float value, const char* unit);
|
||||
|
||||
void mqttPublishValue(const String& name, double value, const char* unit);
|
||||
|
||||
void mqttPublishSwitcher(const String& name, const String& category, boolean state);
|
||||
|
||||
void mqttPublishValue(const String& name, const String& value, const char* unit);
|
||||
|
||||
void mqttPublish(const String& topic, const String& payload);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user