rc-gateway/gateway/src/gateway.cpp
Nicu Hodos 8f95262fd4 return by value
split protocol handling
2022-10-25 23:23:49 +02:00

180 lines
4.1 KiB
C++

#include <Arduino.h>
#include <RCSwitch.h>
#include <Adafruit_Sensor.h>
#include "Tiny.h"
#include <ArduinoJson.h>
#include "Dht.h"
#include "RcDecoder.h"
#define RESET_PIN 10
#define SEND_PIN 11
#define RECEIVE_PIN 2
RCSwitch mySwitch = RCSwitch();
void readRcSwitch();
void readCommand();
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);
}
void loop() {
readCommand();
readRcSwitch();
Dht::read();
}
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
JsonObject sensor = jsonDoc.createNestedObject("sensor");
sensor["id"] = ID(value);
float voltage = (float)GET_VCC(value) / 1000;
if (voltage != 0) {
JsonObject diagnostic = sensor.createNestedObject("diagnostic");
diagnostic["voltage"] = voltage;
}
switch (GET_TYPE(value)) {
case SensorType::GENERIC:
sensor["value"] = GET_VALUE(value);
break;
case SensorType::TEMPERATURE:
sensor["temperature"] = (float)GET_TEMP(value) / 10;
break;
case SensorType::HUMIDITY:
sensor["humidity"] = (float)GET_HUMIDITY(value) / 10;
break;
case SensorType::CONTACT:
sensor["state"] = GET_STATE(value) ? "on" : "off";
break;
default:
return false;
}
return true;
}
void handleProtocol2(JsonDocument& jsonDoc, unsigned long value) {
switch (value) {
case 637541753L:
case 771759481L: {
JsonObject motion = jsonDoc.createNestedObject("motion");
motion["kitchen"] = value == 637541753L ? "on" : "off";
break;
}
case 1879048230L:
case 1879048198L: {
JsonObject motion = jsonDoc.createNestedObject("motion");
motion["basement"] = value == 1879048230L ? "on" : "off";
break;
}
default:
buildSensorJson(jsonDoc, value);
break;
}
}
void buildRcSwitch(JsonDocument& jsonDoc, unsigned int protocol, unsigned long value) {
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
rcSwitch["protocol"] = protocol;
if (protocol == 1) {
RcDecoder::RcSwitch decoded = RcDecoder::decode(value);
rcSwitch["state"] = decoded.state;
rcSwitch["group"] = String(decoded.group, BIN);
rcSwitch["channel"] = decoded.device;
} else {
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() {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
}
void runRcSwitchCommand(JsonObjectConst rcSwitch) {
unsigned int protocol = rcSwitch["protocol"];
if (protocol == 1) {
mySwitch.setProtocol(protocol);
char* group = rcSwitch["group"];
int channel = rcSwitch["channel"];
rcSwitch["state"] ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
} else {
mySwitch.setProtocol(protocol);
mySwitch.send(rcSwitch["value"]);
}
}
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")) {
runRcSwitchCommand(jsonDoc["rcSwitch"]);
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());
}
}