diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 79ac733..b1bf759 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -12,6 +12,8 @@ platform = atmelavr board = pro16MHzatmega328 framework = arduino +lib_extra_dirs = + ../libraries lib_deps = sui77/rc-switch@^2.6.3 bblanchon/ArduinoJson@6.16.1 diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 3eb7f5a..c2a6bae 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "Tiny.h" #define RESET_PIN 10 #define SEND_PIN 11 @@ -64,6 +65,20 @@ void readRcSwitch(JsonDocument& jsonDoc) { motion["basement"] = value == 1879048230L ? "on" : "off"; 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::CONTACT) { + JsonObject sensor = jsonDoc.createNestedObject("contact"); + sensor["id"] = ID(value); + sensor["state"] = GET_STATE(value) ? "on" : "off"; + sensor["voltage"] = GET_VCC(value); + return; + } } JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); rcSwitch["protocol"] = mySwitch.getReceivedProtocol(); diff --git a/libraries/Tiny/TempSensor.h b/libraries/Tiny/TempSensor.h index f36b680..0120ac5 100644 --- a/libraries/Tiny/TempSensor.h +++ b/libraries/Tiny/TempSensor.h @@ -3,12 +3,12 @@ #include #include -class TempSensor: public TinySensor { +class TempSensor : public TinySensor { SensorType sensorType = TEMPERATURE; public: TempSensor(short id, short senderPin) : - TinySensor(id, senderPin) { + TinySensor(id, senderPin) { } void sendTempAndVoltage(int temp) { diff --git a/libraries/Tiny/Tiny.h b/libraries/Tiny/Tiny.h index 6f8f250..c644c75 100644 --- a/libraries/Tiny/Tiny.h +++ b/libraries/Tiny/Tiny.h @@ -1,7 +1,20 @@ #pragma once -#include -#include +#define ID(value) (value & 0x1F) +#define STATE(value) ((value & 0x1) << 5) +#define VCC(value) ((value & 0x1FFF) << 6) +#define TEMP(value) (((unsigned long)value & 0x1FF) << 19) +#define TYPE(value) (((unsigned long)value & 0xF) << 28) + +#define GET_TYPE(value) (((unsigned long)value >> 28) & 0xF) +#define GET_TEMP(value) (((unsigned long)value >> 19) & 0x1FF) +#define GET_VCC(value) ((value >> 6) & 0x1FFF) +#define GET_STATE(value) ((value >> 5) & 0x1) + +typedef enum SensorType { + TEMPERATURE = 6, + CONTACT = 7 +}; enum SensorId : int { WINDOW1 = 1, diff --git a/libraries/Tiny/TinySensor.h b/libraries/Tiny/TinySensor.h index efdabcb..e1a6b90 100644 --- a/libraries/Tiny/TinySensor.h +++ b/libraries/Tiny/TinySensor.h @@ -1,18 +1,7 @@ #pragma once #include - -#define ID(value) (value & 0x1F) -#define STATE(value) ((value & 0x1) << 5) -#define VCC(value) ((value & 0x1FFF) << 6) -#define TEMP(value) (((unsigned long)value & 0x1FF) << 19) -#define TYPE(value) (((unsigned long)value & 0xF) << 28) - -typedef enum SensorType { - TEMPERATURE = 6, - CONTACT = 7 -}; - +#include "Tiny.h" class TinySensor { protected: diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index d2a4dc3..3ab2e14 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -1,14 +1,16 @@ #include -#include +#include #include #include #define DEBUG 0 #define SEND_INTERVAL (int)(5*60/8) +#define SEND_VCC_INTERVAL (int)(60*60/8) // Pins -#define SENDER PIN_B4 -#define TEMP_PIN A1 +#define TEMP_POSITIVE PIN_B3 +#define SENDER PIN_B2 +#define TEMP_PIN A2 #if DEBUG #define RxD PIN_B3 @@ -21,7 +23,6 @@ int readTemp(); TempSensor sensor = TempSensor(TEMP_SENSOR, SENDER); volatile int counter = 0; -volatile bool shouldSend = true; void setup() { @@ -31,21 +32,28 @@ void setup() { #endif sensor.setup(); analogReference(INTERNAL); + pinMode(TEMP_POSITIVE, OUTPUT); + digitalWrite(TEMP_POSITIVE, LOW); TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); } void loop() { - if (shouldSend) { - shouldSend = false; + if (counter % SEND_VCC_INTERVAL == 0) { + sensor.sendTempAndVoltage(readTemp()); + counter = 0; + } else if (counter % SEND_INTERVAL == 0) { sensor.sendTemp(readTemp()); } TinyPower::sleep(); } int readTemp() { + digitalWrite(TEMP_POSITIVE, HIGH); + delay(10); int reading = analogRead(TEMP_PIN); + digitalWrite(TEMP_POSITIVE, LOW); #if DEBUG AttinySerial.println(reading); #endif @@ -69,8 +77,4 @@ ISR(PCINT0_vect) { ISR(WDT_vect) { counter++; - if (counter % SEND_INTERVAL == 0) { - shouldSend = true; - counter = 0; - } }