return by value
split protocol handling
This commit is contained in:
parent
a9be3d88eb
commit
ab4e406b2f
@ -9,27 +9,30 @@ DHT dht = DHT(DHT11_PIN, DHT11);
|
||||
unsigned long currentTime = 0;
|
||||
|
||||
namespace Dht {
|
||||
void setup() {
|
||||
void setup() {
|
||||
dht.begin();
|
||||
}
|
||||
}
|
||||
|
||||
void read(JsonDocument& jsonDoc) {
|
||||
void read() {
|
||||
currentTime = millis();
|
||||
static unsigned long lastReadTime = 0;
|
||||
if (currentTime > lastReadTime) {
|
||||
lastReadTime = currentTime + READ_INTERVAL(5);
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
JsonObject dht11 = jsonDoc.createNestedObject("dht11");
|
||||
dht11["temperature"] = dht.readTemperature();
|
||||
dht11["humidity"] = dht.readHumidity();
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
namespace Dht {
|
||||
void setup() {
|
||||
}
|
||||
|
||||
void read(JsonDocument& jsonDoc) {
|
||||
void setup() {
|
||||
}
|
||||
|
||||
void read() {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -10,13 +10,15 @@ namespace RcDecoder {
|
||||
byte device;
|
||||
};
|
||||
|
||||
void decode(unsigned long value, RcSwitch& decoded) {
|
||||
RcSwitch decode(unsigned long value) {
|
||||
value = value >> 2;
|
||||
unsigned long res = 0;
|
||||
for (int i = 0; i < 12; i++) {
|
||||
res |= ((value & 1) ^ 1) << i;
|
||||
value = value >> 2;
|
||||
}
|
||||
|
||||
RcSwitch decoded;
|
||||
decoded.state = RC_STATE(res);
|
||||
decoded.group = RC_GROUP(res);
|
||||
switch (RC_DEVICE(res)) {
|
||||
@ -36,5 +38,6 @@ namespace RcDecoder {
|
||||
decoded.device = 5;
|
||||
break;
|
||||
}
|
||||
return decoded;
|
||||
}
|
||||
}
|
||||
@ -13,7 +13,7 @@
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void readRcSwitch(JsonDocument& jsonDoc);
|
||||
void readRcSwitch();
|
||||
void readCommand();
|
||||
|
||||
void setup() {
|
||||
@ -34,13 +34,8 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
readCommand();
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
readRcSwitch(jsonDoc);
|
||||
Dht::read(jsonDoc);
|
||||
if (!jsonDoc.isNull()) {
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
readRcSwitch();
|
||||
Dht::read();
|
||||
}
|
||||
|
||||
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
|
||||
@ -73,36 +68,56 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
|
||||
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()) {
|
||||
unsigned long value = mySwitch.getReceivedValue();
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
if (mySwitch.getReceivedProtocol() == 2) {
|
||||
if (value == 637541753L || value == 771759481L) {
|
||||
JsonObject motion = jsonDoc.createNestedObject("motion");
|
||||
motion["kitchen"] = value == 637541753L ? "on" : "off";
|
||||
return;
|
||||
}
|
||||
if (value == 1879048230L || value == 1879048198L) {
|
||||
JsonObject motion = jsonDoc.createNestedObject("motion");
|
||||
motion["basement"] = value == 1879048230L ? "on" : "off";
|
||||
return;
|
||||
}
|
||||
if (buildSensorJson(jsonDoc, value)) {
|
||||
return;
|
||||
}
|
||||
StaticJsonDocument<200> jsonDoc;
|
||||
switch (mySwitch.getReceivedProtocol()) {
|
||||
case 1:
|
||||
handleProtocol2(jsonDoc, value);
|
||||
break;
|
||||
default:
|
||||
buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value);
|
||||
break;
|
||||
}
|
||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
||||
if (mySwitch.getReceivedProtocol() == 1) {
|
||||
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;
|
||||
if (!jsonDoc.isNull()) {
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -113,8 +128,7 @@ void blink() {
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
|
||||
void runRcSwitchCommand(JsonVariant jsonDoc) {
|
||||
JsonObject rcSwitch = jsonDoc["rcSwitch"];
|
||||
void runRcSwitchCommand(JsonObjectConst rcSwitch) {
|
||||
unsigned int protocol = rcSwitch["protocol"];
|
||||
if (protocol == 1) {
|
||||
mySwitch.setProtocol(protocol);
|
||||
@ -125,9 +139,6 @@ void runRcSwitchCommand(JsonVariant jsonDoc) {
|
||||
mySwitch.setProtocol(protocol);
|
||||
mySwitch.send(rcSwitch["value"]);
|
||||
}
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
// blink();
|
||||
}
|
||||
|
||||
void runJsonCommands(const char* cmd) {
|
||||
@ -138,7 +149,9 @@ void runJsonCommands(const char* cmd) {
|
||||
JsonArray array = jsonArray.as<JsonArray>();
|
||||
for (JsonVariant jsonDoc : array) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
runRcSwitchCommand(jsonDoc);
|
||||
runRcSwitchCommand(jsonDoc["rcSwitch"]);
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user