Merge pull request 'gateway: adapt it to publish sensors on MQTT' (#1) from gateway into master
Reviewed-on: #1
This commit is contained in:
commit
ce36b712db
@ -10,9 +10,11 @@ platform:
|
||||
steps:
|
||||
- name: upload
|
||||
commands:
|
||||
- service ser2net stop
|
||||
- cd gateway
|
||||
- pio run -e pro-mini
|
||||
- echo -n 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v
|
||||
- service ser2net start
|
||||
when:
|
||||
target:
|
||||
- production
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
#include <Arduino.h>
|
||||
#include <RCSwitch.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include "Tiny.h"
|
||||
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#include <ArduinoJson.h>
|
||||
#include "Dht.h"
|
||||
|
||||
|
||||
@ -43,6 +45,34 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
|
||||
jsonDoc["id"] = ID(value);
|
||||
|
||||
JsonObject sensor = jsonDoc.createNestedObject("sensor");
|
||||
|
||||
unsigned long voltage = GET_VCC(value);
|
||||
sensor["voltage"] = voltage == 0 ? NAN : (float)voltage / 1000;
|
||||
|
||||
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();
|
||||
@ -59,32 +89,7 @@ void readRcSwitch(JsonDocument& jsonDoc) {
|
||||
motion["basement"] = value == 1879048230L ? "on" : "off";
|
||||
return;
|
||||
}
|
||||
if (GET_TYPE(value) == SensorType::GENERIC) {
|
||||
JsonObject sensor = jsonDoc.createNestedObject("sensor");
|
||||
sensor["id"] = ID(value);
|
||||
sensor["value"] = GET_VALUE(value);
|
||||
sensor["voltage"] = GET_VCC(value);
|
||||
return;
|
||||
}
|
||||
if (GET_TYPE(value) == SensorType::TEMPERATURE) {
|
||||
JsonObject sensor = jsonDoc.createNestedObject("sensor");
|
||||
sensor["id"] = ID(value);
|
||||
sensor["temperature"] = GET_TEMP(value);
|
||||
sensor["voltage"] = GET_VCC(value);
|
||||
return;
|
||||
}
|
||||
if (GET_TYPE(value) == SensorType::HUMIDITY) {
|
||||
JsonObject sensor = jsonDoc.createNestedObject("sensor");
|
||||
sensor["id"] = ID(value);
|
||||
sensor["humidity"] = GET_HUMIDITY(value);
|
||||
sensor["voltage"] = GET_VCC(value);
|
||||
return;
|
||||
}
|
||||
if (GET_TYPE(value) == SensorType::CONTACT) {
|
||||
JsonObject sensor = jsonDoc.createNestedObject("contact");
|
||||
sensor["id"] = ID(value);
|
||||
sensor["state"] = GET_STATE(value) ? "on" : "off";
|
||||
sensor["voltage"] = GET_VCC(value);
|
||||
if (buildSensorJson(jsonDoc, value)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -105,7 +110,7 @@ void runJsonCommand(String cmd) {
|
||||
DeserializationError err = deserializeJson(jsonArray, cmd);
|
||||
if (err == DeserializationError::Ok) {
|
||||
JsonArray array = jsonArray.as<JsonArray>();
|
||||
for(JsonVariant jsonDoc : array) {
|
||||
for (JsonVariant jsonDoc : array) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObject rcSwitch = jsonDoc["rcSwitch"];
|
||||
unsigned int protocol = rcSwitch["protocol"];
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
#define GET_TEMP(value) (((unsigned long)value >> 18) & 0x2FF)
|
||||
#define GET_HUMIDITY(value) (((unsigned long)value >> 18) & 0x2FF)
|
||||
#define GET_VALUE(value) (((unsigned long)value >> 18) & 0x2FF)
|
||||
#define GET_VCC(value) ((value >> 5) & 0x1FFF)
|
||||
#define GET_VCC(value) (((unsigned long)value >> 5) & 0x1FFF)
|
||||
|
||||
enum SensorType {
|
||||
GENERIC = 4,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user