return by value

split protocol handling
This commit is contained in:
Nicu Hodos 2022-10-25 23:23:49 +02:00
parent a9be3d88eb
commit ab4e406b2f
3 changed files with 66 additions and 47 deletions

View File

@ -9,27 +9,30 @@ DHT dht = DHT(DHT11_PIN, DHT11);
unsigned long currentTime = 0; unsigned long currentTime = 0;
namespace Dht { namespace Dht {
void setup() { void setup() {
dht.begin(); dht.begin();
} }
void read(JsonDocument& jsonDoc) { void read() {
currentTime = millis(); currentTime = millis();
static unsigned long lastReadTime = 0; static unsigned long lastReadTime = 0;
if (currentTime > lastReadTime) { if (currentTime > lastReadTime) {
lastReadTime = currentTime + READ_INTERVAL(5); lastReadTime = currentTime + READ_INTERVAL(5);
StaticJsonDocument<200> jsonDoc;
JsonObject dht11 = jsonDoc.createNestedObject("dht11"); JsonObject dht11 = jsonDoc.createNestedObject("dht11");
dht11["temperature"] = dht.readTemperature(); dht11["temperature"] = dht.readTemperature();
dht11["humidity"] = dht.readHumidity(); dht11["humidity"] = dht.readHumidity();
serializeJson(jsonDoc, Serial);
Serial.println();
} }
} }
} }
#else #else
namespace Dht { namespace Dht {
void setup() { void setup() {
}
void read(JsonDocument& jsonDoc) {
} }
void read() {
}
} }
#endif #endif

View File

@ -10,13 +10,15 @@ namespace RcDecoder {
byte device; byte device;
}; };
void decode(unsigned long value, RcSwitch& decoded) { RcSwitch decode(unsigned long value) {
value = value >> 2; value = value >> 2;
unsigned long res = 0; unsigned long res = 0;
for (int i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {
res |= ((value & 1) ^ 1) << i; res |= ((value & 1) ^ 1) << i;
value = value >> 2; value = value >> 2;
} }
RcSwitch decoded;
decoded.state = RC_STATE(res); decoded.state = RC_STATE(res);
decoded.group = RC_GROUP(res); decoded.group = RC_GROUP(res);
switch (RC_DEVICE(res)) { switch (RC_DEVICE(res)) {
@ -36,5 +38,6 @@ namespace RcDecoder {
decoded.device = 5; decoded.device = 5;
break; break;
} }
return decoded;
} }
} }

View File

@ -13,7 +13,7 @@
RCSwitch mySwitch = RCSwitch(); RCSwitch mySwitch = RCSwitch();
void readRcSwitch(JsonDocument& jsonDoc); void readRcSwitch();
void readCommand(); void readCommand();
void setup() { void setup() {
@ -34,13 +34,8 @@ void setup() {
void loop() { void loop() {
readCommand(); readCommand();
StaticJsonDocument<200> jsonDoc; readRcSwitch();
readRcSwitch(jsonDoc); Dht::read();
Dht::read(jsonDoc);
if (!jsonDoc.isNull()) {
serializeJson(jsonDoc, Serial);
Serial.println();
}
} }
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
@ -73,36 +68,56 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
return true; return true;
} }
void readRcSwitch(JsonDocument& jsonDoc) { void handleProtocol2(JsonDocument& jsonDoc, unsigned long value) {
switch (value) {
case 637541753L:
case 771759481L: {
JsonObject motion = jsonDoc.createNestedObject("motion");
motion["kitchen"] = value == 637541753L ? "on" : "off";
break;
}
case 1879048230L:
case 1879048198L: {
JsonObject motion = jsonDoc.createNestedObject("motion");
motion["basement"] = value == 1879048230L ? "on" : "off";
break;
}
default:
buildSensorJson(jsonDoc, value);
break;
}
}
void buildRcSwitch(JsonDocument& jsonDoc, unsigned int protocol, unsigned long value) {
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
rcSwitch["protocol"] = protocol;
if (protocol == 1) {
RcDecoder::RcSwitch decoded = RcDecoder::decode(value);
rcSwitch["state"] = decoded.state;
rcSwitch["group"] = String(decoded.group, BIN);
rcSwitch["channel"] = decoded.device;
} else {
rcSwitch["value"] = value;
}
}
void readRcSwitch() {
if (mySwitch.available()) { if (mySwitch.available()) {
unsigned long value = mySwitch.getReceivedValue(); unsigned long value = mySwitch.getReceivedValue();
mySwitch.resetAvailable(); mySwitch.resetAvailable();
if (mySwitch.getReceivedProtocol() == 2) { StaticJsonDocument<200> jsonDoc;
if (value == 637541753L || value == 771759481L) { switch (mySwitch.getReceivedProtocol()) {
JsonObject motion = jsonDoc.createNestedObject("motion"); case 1:
motion["kitchen"] = value == 637541753L ? "on" : "off"; handleProtocol2(jsonDoc, value);
return; break;
} default:
if (value == 1879048230L || value == 1879048198L) { buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value);
JsonObject motion = jsonDoc.createNestedObject("motion"); break;
motion["basement"] = value == 1879048230L ? "on" : "off";
return;
}
if (buildSensorJson(jsonDoc, value)) {
return;
}
} }
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); if (!jsonDoc.isNull()) {
rcSwitch["protocol"] = mySwitch.getReceivedProtocol(); serializeJson(jsonDoc, Serial);
if (mySwitch.getReceivedProtocol() == 1) { Serial.println();
RcDecoder::RcSwitch decoded;
RcDecoder::decode(value, decoded);
rcSwitch["state"] = decoded.state;
rcSwitch["group"] = String(decoded.group, BIN);
rcSwitch["channel"] = decoded.device;
} else {
rcSwitch["value"] = value;
} }
} }
} }
@ -113,8 +128,7 @@ void blink() {
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
} }
void runRcSwitchCommand(JsonVariant jsonDoc) { void runRcSwitchCommand(JsonObjectConst rcSwitch) {
JsonObject rcSwitch = jsonDoc["rcSwitch"];
unsigned int protocol = rcSwitch["protocol"]; unsigned int protocol = rcSwitch["protocol"];
if (protocol == 1) { if (protocol == 1) {
mySwitch.setProtocol(protocol); mySwitch.setProtocol(protocol);
@ -125,9 +139,6 @@ void runRcSwitchCommand(JsonVariant jsonDoc) {
mySwitch.setProtocol(protocol); mySwitch.setProtocol(protocol);
mySwitch.send(rcSwitch["value"]); mySwitch.send(rcSwitch["value"]);
} }
serializeJson(jsonDoc, Serial);
Serial.println();
// blink();
} }
void runJsonCommands(const char* cmd) { void runJsonCommands(const char* cmd) {
@ -138,7 +149,9 @@ void runJsonCommands(const char* cmd) {
JsonArray array = jsonArray.as<JsonArray>(); JsonArray array = jsonArray.as<JsonArray>();
for (JsonVariant jsonDoc : array) { for (JsonVariant jsonDoc : array) {
if (jsonDoc.containsKey("rcSwitch")) { if (jsonDoc.containsKey("rcSwitch")) {
runRcSwitchCommand(jsonDoc); runRcSwitchCommand(jsonDoc["rcSwitch"]);
serializeJson(jsonDoc, Serial);
Serial.println();
} }
} }
} else { } else {