108 lines
2.9 KiB
C++
108 lines
2.9 KiB
C++
#include "Arduino.h"
|
|
#include <RCSwitch.h>
|
|
#include <ArduinoJson.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <DHT.h>
|
|
|
|
#define DEBUG 0
|
|
|
|
#define RESET_PIN 10
|
|
#define SEND_PIN 11
|
|
#define RECEIVE_PIN 2
|
|
#define DHT11_PIN 12
|
|
|
|
#define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes
|
|
|
|
const unsigned long readInterval = READ_INTERVAL(5);
|
|
unsigned long lastReadTime = -READ_INTERVAL(10);
|
|
|
|
RCSwitch mySwitch = RCSwitch();
|
|
DHT dht = DHT(DHT11_PIN, DHT11);
|
|
|
|
|
|
void setup() {
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
pinMode(RESET_PIN, INPUT_PULLUP);
|
|
|
|
mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN));
|
|
mySwitch.enableTransmit(SEND_PIN);
|
|
mySwitch.setRepeatTransmit(10);
|
|
|
|
dht.begin();
|
|
|
|
Serial.begin(9600);
|
|
|
|
delay(1000);
|
|
}
|
|
|
|
void loop() {
|
|
unsigned long currentTime = millis();
|
|
if ((currentTime - lastReadTime) > readInterval) {
|
|
lastReadTime = currentTime;
|
|
StaticJsonDocument<256> jsonDoc;
|
|
JsonObject dht11 = jsonDoc.createNestedObject("dht11");
|
|
dht11["temperature"] = dht.readTemperature();
|
|
dht11["humidity"] = dht.readHumidity();
|
|
serializeJson(jsonDoc, Serial);
|
|
Serial.println();
|
|
}
|
|
if (Serial.available() > 0) {
|
|
String cmd = Serial.readStringUntil('\n');
|
|
if (cmd == "reset") {
|
|
pinMode(RESET_PIN, OUTPUT);
|
|
digitalWrite(RESET_PIN, LOW);
|
|
}
|
|
StaticJsonDocument<256> jsonBuffer;
|
|
DeserializationError err = deserializeJson(jsonBuffer, cmd);
|
|
if (err == DeserializationError::Ok) {
|
|
if (jsonBuffer.containsKey("rcSwitch")) {
|
|
JsonObject rcSwitch = jsonBuffer["rcSwitch"];
|
|
unsigned int protocol = rcSwitch["protocol"];
|
|
if (protocol == 1) {
|
|
mySwitch.setProtocol(protocol);
|
|
char* group = rcSwitch["group"];
|
|
int channel = rcSwitch["channel"];
|
|
if (rcSwitch["state"]) {
|
|
mySwitch.switchOn(group, channel);
|
|
} else {
|
|
mySwitch.switchOff(group, channel);
|
|
}
|
|
} else {
|
|
mySwitch.setProtocol(protocol);
|
|
mySwitch.send(rcSwitch["value"]);
|
|
}
|
|
}
|
|
} else {
|
|
Serial.println(err.c_str());
|
|
}
|
|
}
|
|
if (mySwitch.available()) {
|
|
unsigned long value = mySwitch.getReceivedValue();
|
|
mySwitch.resetAvailable();
|
|
#if DEBUG
|
|
StaticJsonDocument<100> json1;
|
|
JsonObject rcSwitch = json1.createNestedObject("rcSwitch");
|
|
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
|
rcSwitch["value"] = value;
|
|
serializeJson(json1, Serial);
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|