muwerk mupplet Sensor Library
muwerk applets; mupplets: functional units that support specific hardware or reusable applications for sensors
Loading...
Searching...
No Matches
mup_binary_sensor.h
1// mup_binary_sensor.h
2#pragma once
3
4#include "scheduler.h"
5#include "sensors.h"
6
7namespace ustd {
8
9// clang-format off
55// clang-format on
57 private:
58 String BinarySensor_VERSION = "0.1.0";
59 Scheduler *pSched;
60 int tID;
61 String name;
62 uint8_t digitalPort;
63 bool inverseLogic;
64 String topicName;
65 bool logicalState;
66 bool physicalState;
67 unsigned long basePollRate = 500000L;
68 uint32_t pollRateMs = 2000;
69 uint32_t lastPollMs = 0;
70 bool bActive = false;
71 bool initialPublish = false;
72
73 public:
74 BinarySensor(String name, uint8_t digitalPort, bool inverseLogic = false, String topicName = "state")
75 : name(name), digitalPort(digitalPort), inverseLogic(inverseLogic), topicName(topicName) {
82 }
83
85 }
86
95 physicalState = digitalRead(digitalPort);
96 return physicalState;
97 }
98
107 physicalState = digitalRead(digitalPort);
108 if (inverseLogic) {
109 logicalState = !physicalState;
110 } else {
111 logicalState = physicalState;
112 }
113 return logicalState;
114 }
115
116 void begin(Scheduler *_pSched, uint32_t _pollRateMs = 2000) {
122 pSched = _pSched;
123 pollRateMs = _pollRateMs;
124
125 auto ft = [=]() { this->loop(); };
126 tID = pSched->add(ft, name, basePollRate);
127
128 pinMode(digitalPort, INPUT_PULLUP);
129 logicalState = getBinarySensorLogicalState();
130 initialPublish = false;
131
132 auto fnall = [=](String topic, String msg, String originator) {
133 this->subsMsg(topic, msg, originator);
134 };
135 pSched->subscribe(tID, name + "/sensor/#", fnall);
136 pSched->subscribe(tID, name + "/binary_sensor/#", fnall);
137 pSched->subscribe(tID, name + "/mqtt/state", fnall);
138 bActive = true;
139 }
140
141 private:
142 void publishPhysicalState() {
143 char buf[32];
144 if (getPhysicalState()) {
145 strcpy(buf, "ON");
146 } else {
147 strcpy(buf, "OFF");
148 }
149 pSched->publish(name + "/sensor/physical/" + topicName, buf);
150 pSched->publish(name + "/binary_sensor/physical/" + topicName, buf);
151 }
152
153 void publishBinarySensor() {
154 char buf[32];
156 strcpy(buf, "ON");
157 } else {
158 strcpy(buf, "OFF");
159 }
160 pSched->publish(name + "/sensor/" + topicName, buf);
161 pSched->publish(name + "/binary_sensor/" + topicName, buf);
162 }
163
164 void loop() {
165 if (bActive) {
166 if (timeDiff(lastPollMs, millis()) >= pollRateMs || !initialPublish) {
167 bool hasChanged = false;
168 lastPollMs = millis();
169 bool oldLogicalState = logicalState;
170 bool st = getBinarySensorLogicalState();
171 if (st != oldLogicalState || !initialPublish) {
172 hasChanged = true;
173 }
174 if (hasChanged) {
175 publishBinarySensor();
176 initialPublish = true;
177 }
178 }
179 }
180 }
181
182 void subsMsg(String topic, String msg, String originator) {
183 if (topic == name + "/sensor/" + topicName + "/get" || topic == name + "/binary_sensor/" + topicName + "/get") {
184 publishBinarySensor();
185 } else if (topic == name + "/sensor/physical/" + topicName + "/get" || topic == name + "/binary_sensor/physical/" + topicName + "/get") {
186 publishBinarySensor();
187 } else if (topic == name + "mqtt/state") {
188 if (msg == "connected") {
189 initialPublish = false; // republish for mqtt.
190 }
191 }
192 }
193}; // rainAD
194
195} // namespace ustd
mupplet-sensor, binary sensor
Definition: mup_binary_sensor.h:56
bool getBinarySensorLogicalState()
Definition: mup_binary_sensor.h:99
BinarySensor(String name, uint8_t digitalPort, bool inverseLogic=false, String topicName="state")
Definition: mup_binary_sensor.h:74
void begin(Scheduler *_pSched, uint32_t _pollRateMs=2000)
Definition: mup_binary_sensor.h:116
bool getPhysicalState()
Definition: mup_binary_sensor.h:87