79 lines
2.6 KiB
C++
79 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <RCSwitch.h>
|
|
#include "RcDecoder.h"
|
|
|
|
RCSwitch mySwitch = RCSwitch();
|
|
|
|
namespace Rc {
|
|
void setup() {
|
|
mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN));
|
|
mySwitch.enableTransmit(SEND_PIN);
|
|
mySwitch.setRepeatTransmit(10);
|
|
}
|
|
|
|
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
|
|
jsonDoc["id"] = ID(value);
|
|
|
|
float voltage = (float)GET_VCC(value) / 1000;
|
|
if (voltage != 0) {
|
|
JsonObject diagnostic = jsonDoc.createNestedObject("diagnostic");
|
|
diagnostic["voltage"] = voltage;
|
|
}
|
|
|
|
JsonObject sensor = jsonDoc.createNestedObject("sensor");
|
|
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 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;
|
|
}
|
|
if (buildSensorJson(jsonDoc, value)) {
|
|
return;
|
|
}
|
|
}
|
|
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
|
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
|
if (mySwitch.getReceivedProtocol() == 1) {
|
|
RcDecoder::RcSwitch decoded;
|
|
RcDecoder::decode(value, decoded);
|
|
rcSwitch["state"] = decoded.state;
|
|
rcSwitch["group"] = String(decoded.group, BIN);
|
|
rcSwitch["channel"] = decoded.device;
|
|
} else {
|
|
rcSwitch["value"] = value;
|
|
}
|
|
}
|
|
}
|
|
}
|