return by value
split protocol handling
This commit is contained in:
parent
640368af42
commit
8f95262fd4
@ -13,14 +13,17 @@ namespace Dht {
|
|||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,7 +32,7 @@ namespace Dht {
|
|||||||
void setup() {
|
void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void read(JsonDocument& jsonDoc) {
|
void read() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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,31 +68,31 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void readRcSwitch(JsonDocument& jsonDoc) {
|
void handleProtocol2(JsonDocument& jsonDoc, unsigned long value) {
|
||||||
if (mySwitch.available()) {
|
switch (value) {
|
||||||
unsigned long value = mySwitch.getReceivedValue();
|
case 637541753L:
|
||||||
mySwitch.resetAvailable();
|
case 771759481L: {
|
||||||
|
|
||||||
if (mySwitch.getReceivedProtocol() == 2) {
|
|
||||||
if (value == 637541753L || value == 771759481L) {
|
|
||||||
JsonObject motion = jsonDoc.createNestedObject("motion");
|
JsonObject motion = jsonDoc.createNestedObject("motion");
|
||||||
motion["kitchen"] = value == 637541753L ? "on" : "off";
|
motion["kitchen"] = value == 637541753L ? "on" : "off";
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
if (value == 1879048230L || value == 1879048198L) {
|
case 1879048230L:
|
||||||
|
case 1879048198L: {
|
||||||
JsonObject motion = jsonDoc.createNestedObject("motion");
|
JsonObject motion = jsonDoc.createNestedObject("motion");
|
||||||
motion["basement"] = value == 1879048230L ? "on" : "off";
|
motion["basement"] = value == 1879048230L ? "on" : "off";
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
if (buildSensorJson(jsonDoc, value)) {
|
default:
|
||||||
return;
|
buildSensorJson(jsonDoc, value);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void buildRcSwitch(JsonDocument& jsonDoc, unsigned int protocol, unsigned long value) {
|
||||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||||
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
rcSwitch["protocol"] = protocol;
|
||||||
if (mySwitch.getReceivedProtocol() == 1) {
|
if (protocol == 1) {
|
||||||
RcDecoder::RcSwitch decoded;
|
RcDecoder::RcSwitch decoded = RcDecoder::decode(value);
|
||||||
RcDecoder::decode(value, decoded);
|
|
||||||
rcSwitch["state"] = decoded.state;
|
rcSwitch["state"] = decoded.state;
|
||||||
rcSwitch["group"] = String(decoded.group, BIN);
|
rcSwitch["group"] = String(decoded.group, BIN);
|
||||||
rcSwitch["channel"] = decoded.device;
|
rcSwitch["channel"] = decoded.device;
|
||||||
@ -105,6 +100,26 @@ void readRcSwitch(JsonDocument& jsonDoc) {
|
|||||||
rcSwitch["value"] = value;
|
rcSwitch["value"] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void readRcSwitch() {
|
||||||
|
if (mySwitch.available()) {
|
||||||
|
unsigned long value = mySwitch.getReceivedValue();
|
||||||
|
mySwitch.resetAvailable();
|
||||||
|
|
||||||
|
StaticJsonDocument<200> jsonDoc;
|
||||||
|
switch (mySwitch.getReceivedProtocol()) {
|
||||||
|
case 1:
|
||||||
|
handleProtocol2(jsonDoc, value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!jsonDoc.isNull()) {
|
||||||
|
serializeJson(jsonDoc, Serial);
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void blink() {
|
void blink() {
|
||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user