update temp_sensor and adapt gateway to it

This commit is contained in:
Nicu Hodos 2021-12-23 16:28:29 +01:00
parent d1cd5d6560
commit d622d70dc0
6 changed files with 49 additions and 26 deletions

View File

@ -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

View File

@ -3,6 +3,7 @@
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#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();

View File

@ -3,12 +3,12 @@
#include <TinySensor.h>
#include <RCSwitch.h>
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) {

View File

@ -1,7 +1,20 @@
#pragma once
#include <ContactSensor.h>
#include <TempSensor.h>
#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,

View File

@ -1,18 +1,7 @@
#pragma once
#include <RCSwitch.h>
#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:

View File

@ -1,14 +1,16 @@
#include <Arduino.h>
#include <Tiny.h>
#include <TempSensor.h>
#include <TinyPower.h>
#include <SoftwareSerial_Tiny.h>
#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;
}
}