aggregate all json elements and send all
This commit is contained in:
parent
e8ecbffb0a
commit
eba6d80ddc
@ -4,8 +4,6 @@
|
|||||||
#include <Adafruit_Sensor.h>
|
#include <Adafruit_Sensor.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
|
|
||||||
#define DEBUG 0
|
|
||||||
|
|
||||||
#define RESET_PIN 10
|
#define RESET_PIN 10
|
||||||
#define SEND_PIN 11
|
#define SEND_PIN 11
|
||||||
#define RECEIVE_PIN 2
|
#define RECEIVE_PIN 2
|
||||||
@ -19,7 +17,6 @@ unsigned long lastReadTime = -READ_INTERVAL(10);
|
|||||||
RCSwitch mySwitch = RCSwitch();
|
RCSwitch mySwitch = RCSwitch();
|
||||||
DHT dht = DHT(DHT11_PIN, DHT11);
|
DHT dht = DHT(DHT11_PIN, DHT11);
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
pinMode(RESET_PIN, INPUT_PULLUP);
|
pinMode(RESET_PIN, INPUT_PULLUP);
|
||||||
@ -36,27 +33,51 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long currentTime = millis();
|
StaticJsonDocument<200> jsonDoc;
|
||||||
if ((currentTime - lastReadTime) > readInterval) {
|
readCommand();
|
||||||
lastReadTime = currentTime;
|
readRcSwitch(jsonDoc);
|
||||||
StaticJsonDocument<256> jsonDoc;
|
readDht(jsonDoc);
|
||||||
JsonObject dht11 = jsonDoc.createNestedObject("dht11");
|
if (!jsonDoc.isNull()) {
|
||||||
dht11["temperature"] = dht.readTemperature();
|
|
||||||
dht11["humidity"] = dht.readHumidity();
|
|
||||||
serializeJson(jsonDoc, Serial);
|
serializeJson(jsonDoc, Serial);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void readRcSwitch(JsonDocument& jsonDoc) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||||
|
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
||||||
|
rcSwitch["value"] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void readCommand() {
|
||||||
if (Serial.available() > 0) {
|
if (Serial.available() > 0) {
|
||||||
String cmd = Serial.readStringUntil('\n');
|
String cmd = Serial.readStringUntil('\n');
|
||||||
if (cmd == "reset") {
|
if (cmd == "reset") {
|
||||||
pinMode(RESET_PIN, OUTPUT);
|
pinMode(RESET_PIN, OUTPUT);
|
||||||
digitalWrite(RESET_PIN, LOW);
|
digitalWrite(RESET_PIN, LOW);
|
||||||
}
|
}
|
||||||
StaticJsonDocument<256> jsonBuffer;
|
StaticJsonDocument<256> jsonDoc;
|
||||||
DeserializationError err = deserializeJson(jsonBuffer, cmd);
|
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
||||||
if (err == DeserializationError::Ok) {
|
if (err == DeserializationError::Ok) {
|
||||||
if (jsonBuffer.containsKey("rcSwitch")) {
|
if (jsonDoc.containsKey("rcSwitch")) {
|
||||||
JsonObject rcSwitch = jsonBuffer["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);
|
||||||
@ -76,32 +97,14 @@ void loop() {
|
|||||||
Serial.println(err.c_str());
|
Serial.println(err.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mySwitch.available()) {
|
}
|
||||||
unsigned long value = mySwitch.getReceivedValue();
|
|
||||||
mySwitch.resetAvailable();
|
void readDht(JsonDocument& jsonDoc) {
|
||||||
#if DEBUG
|
unsigned long currentTime = millis();
|
||||||
StaticJsonDocument<100> json1;
|
if ((currentTime - lastReadTime) > readInterval) {
|
||||||
JsonObject rcSwitch = json1.createNestedObject("rcSwitch");
|
lastReadTime = currentTime;
|
||||||
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
JsonObject dht11 = jsonDoc.createNestedObject("dht11");
|
||||||
rcSwitch["value"] = value;
|
dht11["temperature"] = dht.readTemperature();
|
||||||
serializeJson(json1, Serial);
|
dht11["humidity"] = dht.readHumidity();
|
||||||
Serial.println();
|
|
||||||
#endif
|
|
||||||
if (mySwitch.getReceivedProtocol() == 2) {
|
|
||||||
if (value == 637541753L || value == 771759481L) {
|
|
||||||
StaticJsonDocument<100> jsonBuffer;
|
|
||||||
JsonObject motion = jsonBuffer.createNestedObject("motion");
|
|
||||||
motion["kitchen"] = value == 637541753L ? "on" : "off";
|
|
||||||
serializeJson(jsonBuffer, Serial);
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
if (value == 1879048230L || value == 1879048198L) {
|
|
||||||
StaticJsonDocument<100> jsonBuffer;
|
|
||||||
JsonObject motion = jsonBuffer.createNestedObject("motion");
|
|
||||||
motion["basement"] = value == 1879048230L ? "on" : "off";
|
|
||||||
serializeJson(jsonBuffer, Serial);
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user