104 lines
2.1 KiB
C++
104 lines
2.1 KiB
C++
#include <Arduino.h>
|
|
#include <RCSwitch.h>
|
|
#include "Dht.h"
|
|
#include "Protocol_1.h"
|
|
#include "Protocol_2.h"
|
|
|
|
#define RESET_PIN 10
|
|
#define SEND_PIN 11
|
|
#define RECEIVE_PIN 2
|
|
|
|
|
|
RCSwitch mySwitch = RCSwitch();
|
|
|
|
void setup() {
|
|
digitalWrite(RESET_PIN, HIGH);
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
pinMode(RESET_PIN, OUTPUT);
|
|
|
|
mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN));
|
|
mySwitch.enableTransmit(SEND_PIN);
|
|
mySwitch.setRepeatTransmit(10);
|
|
|
|
Dht::setup();
|
|
|
|
Serial.begin(9600);
|
|
|
|
delay(1000);
|
|
}
|
|
|
|
Protocol findProtocol(unsigned int protocol) {
|
|
switch (mySwitch.getReceivedProtocol()) {
|
|
case 1:
|
|
return protocol_1;
|
|
case 2:
|
|
return protocol_2;
|
|
default:
|
|
return Protocol(protocol);
|
|
}
|
|
}
|
|
|
|
void readRcSwitch() {
|
|
if (mySwitch.available()) {
|
|
unsigned long value = mySwitch.getReceivedValue();
|
|
mySwitch.resetAvailable();
|
|
|
|
StaticJsonDocument<200> jsonDoc;
|
|
findProtocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc);
|
|
if (!jsonDoc.isNull()) {
|
|
serializeJson(jsonDoc, Serial);
|
|
Serial.println();
|
|
}
|
|
}
|
|
}
|
|
|
|
void blink() {
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
delay(200);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
}
|
|
|
|
void runJsonCommands(const char* cmd) {
|
|
String origCmd = String(cmd);
|
|
StaticJsonDocument<512> jsonArray;
|
|
DeserializationError err = deserializeJson(jsonArray, cmd);
|
|
if (err == DeserializationError::Ok) {
|
|
JsonArray array = jsonArray.as<JsonArray>();
|
|
for (JsonVariant jsonDoc : array) {
|
|
if (jsonDoc.containsKey("rcSwitch")) {
|
|
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
|
findProtocol(rcSwitch["protocol"]).fromJson(rcSwitch, mySwitch);
|
|
serializeJson(jsonDoc, Serial);
|
|
Serial.println();
|
|
}
|
|
}
|
|
} else {
|
|
Serial.print(err.c_str());
|
|
Serial.print(": ");
|
|
Serial.println(origCmd);
|
|
}
|
|
}
|
|
|
|
void readCommand() {
|
|
if (Serial.available() > 0) {
|
|
String cmd = Serial.readStringUntil('\n');
|
|
if (cmd == "reset") {
|
|
Serial.println("resetting...");
|
|
delay(200);
|
|
digitalWrite(RESET_PIN, LOW);
|
|
Serial.println("resetting failed");
|
|
}
|
|
if (cmd.endsWith(",")) {
|
|
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
|
}
|
|
cmd = "[" + cmd + "]";
|
|
runJsonCommands(cmd.c_str());
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
readCommand();
|
|
readRcSwitch();
|
|
Dht::read();
|
|
}
|