102 lines
2.3 KiB
C++
102 lines
2.3 KiB
C++
#include <Arduino.h>
|
|
#include <RCSwitch.h>
|
|
#include <SerialReader.h>
|
|
#include "pins.h"
|
|
#include "Dht.h"
|
|
#include "Protocol_1.h"
|
|
#include "Protocol_2.h"
|
|
#include "Protocol_Doorbell.h"
|
|
|
|
RCSwitch mySwitch;
|
|
SerialReader<200> serialReader;
|
|
void runJsonCommand(char* cmd);
|
|
|
|
#if defined(ESP8266)
|
|
#include "huzzah.h"
|
|
#else
|
|
#include "pro-mini.h"
|
|
#endif
|
|
|
|
|
|
void setup() {
|
|
mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN));
|
|
mySwitch.enableTransmit(SEND_PIN);
|
|
mySwitch.setRepeatTransmit(10);
|
|
|
|
Serial.begin(9600);
|
|
|
|
Dht::setup();
|
|
Board::setup();
|
|
|
|
delay(1000);
|
|
}
|
|
|
|
Protocol* findProtocol(unsigned int protocol) {
|
|
switch (protocol) {
|
|
case PROTOCOL_1:
|
|
return &protocol1;
|
|
case PROTOCOL_2:
|
|
return &protocol2;
|
|
case PROTOCOL_13:
|
|
return &doorbell;
|
|
default:
|
|
return &fallbackProtocol.setProtocol(protocol);
|
|
}
|
|
}
|
|
|
|
void readRcSwitch() {
|
|
if (mySwitch.available()) {
|
|
#if DEBUG_RAW
|
|
if (mySwitch.available()) {
|
|
output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
|
|
mySwitch.resetAvailable();
|
|
}
|
|
#else
|
|
unsigned long value = mySwitch.getReceivedValue();
|
|
mySwitch.resetAvailable();
|
|
|
|
StaticJsonDocument<128> jsonDoc;
|
|
auto p = findProtocol(mySwitch.getReceivedProtocol());
|
|
p->toJson(value, jsonDoc);
|
|
if (!jsonDoc.isNull()) {
|
|
serializeJson(jsonDoc, Serial);
|
|
Serial.println();
|
|
Board::publishResponse(jsonDoc);
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void handleJsonError(DeserializationError err, const char* cmd) {
|
|
StaticJsonDocument<50> jsonError;
|
|
JsonObject error = jsonError.createNestedObject("error");
|
|
error["msg"] = err.c_str();
|
|
error["orig_cmd"] = cmd;
|
|
serializeJson(jsonError, Serial);
|
|
Serial.println();
|
|
Board::handleJsonError(jsonError);
|
|
}
|
|
|
|
void runJsonCommand(char* cmd) {
|
|
StaticJsonDocument<128> jsonDoc;
|
|
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
|
if (err == DeserializationError::Ok) {
|
|
if (jsonDoc.containsKey("rcSwitch")) {
|
|
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
|
auto p = findProtocol(rcSwitch["protocol"]);
|
|
p->fromJson(rcSwitch, mySwitch);
|
|
serializeJson(jsonDoc, Serial);
|
|
Serial.println();
|
|
Board::publishResponse(jsonDoc);
|
|
}
|
|
} else {
|
|
handleJsonError(err, cmd);
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
readRcSwitch();
|
|
Board::loop();
|
|
Dht::read();
|
|
}
|