summaryrefslogtreecommitdiffstats
path: root/lr_switch/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lr_switch/src/main.cpp')
-rw-r--r--lr_switch/src/main.cpp197
1 files changed, 0 insertions, 197 deletions
diff --git a/lr_switch/src/main.cpp b/lr_switch/src/main.cpp
deleted file mode 100644
index 673eb3a..0000000
--- a/lr_switch/src/main.cpp
+++ /dev/null
@@ -1,197 +0,0 @@
1/**
2 * The MySensors Arduino library handles the wireless radio link and protocol
3 * between your home built sensors/actuators and HA controller of choice.
4 * The sensors forms a self healing radio network with optional repeaters. Each
5 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6 * network topology allowing messages to be routed to nodes.
7 */
8
9// Enable debug prints to serial monitor
10//#define MY_DEBUG
11
12// configure radio
13#define MY_RADIO_RFM69
14
15/** @brief RFM69 frequency to use (RF69_433MHZ for 433MHz, RF69_868MHZ for 868MHz or RF69_915MHZ for 915MHz). */
16#define MY_RFM69_FREQUENCY RF69_868MHZ
17
18/** @brief Enable this if you're running the RFM69HW model. */
19#define MY_IS_RFM69HW
20
21/** @brief RFM69 Network ID. Use the same for all nodes that will talk to each other. */
22#define MY_RFM69_NETWORKID 1
23
24/** @brief Node id defaults to AUTO (tries to fetch id from controller). */
25#define MY_NODE_ID 3
26
27/** @brief If set, transport traffic is unmonitored and GW connection is optional */
28#define MY_TRANSPORT_DONT_CARE_MODE
29
30/** @brief Node parent defaults to AUTO (tries to find a parent automatically). */
31#define MY_PARENT_NODE_ID 0
32
33/** @brief The user-defined AES key to use for EEPROM personalization */
34#include "aes_key.h"
35
36// Enable repeater functionality for this node
37//#define MY_REPEATER_FEATURE
38
39/** @brief Enables RFM69 automatic transmit power control class. */
40//#define MY_RFM69_ATC
41
42#ifdef MY_AES_KEY
43/** @brief enables RFM69 encryption */
44#define MY_RFM69_ENABLE_ENCRYPTION
45#endif
46
47#include <Arduino.h>
48#include <MySensors.h>
49
50enum sensor_type : uint8_t
51{
52 SENSOR_RELAY = (1u << 0),
53 SENSOR_DIMMER = (1u << 1),
54 SENSOR_BUTTON = (1u << 2),
55 SENSOR_SCENE = (1u << 3),
56};
57
58struct sensor_t
59{
60 uint8_t id;
61 uint8_t type;
62 struct
63 {
64 uint8_t pin; // push button pin
65 } button;
66};
67
68struct sensor_t sensors[] = {
69 {
70 .id = 0,
71 .type = SENSOR_BUTTON | SENSOR_SCENE,
72 .button = { .pin = 3 },
73 },
74};
75
76#define NUM(a) (sizeof(a) / sizeof(*a))
77
78#define TEMP_SENSOR_ID -1
79#define TEMP_READ_INTERVAL 1000L // read temp every 1 sec
80#define TEMP_N_READS_MSG 60*60 // force temp message every n reads
81#define TEMP_OFFSET 0
82
83MyMessage msg(0, V_SCENE_ON);
84
85inline void checkTemperature(void);
86void wakeUp(void);
87
88void before()
89{
90#ifdef MY_AES_KEY
91 const uint8_t user_aes_key[16] = { MY_AES_KEY };
92 uint8_t cur_aes_key[16];
93 hwReadConfigBlock((void*)&cur_aes_key, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, sizeof(cur_aes_key));
94 if (memcmp(&user_aes_key, &cur_aes_key, 16) != 0)
95 {
96 hwWriteConfigBlock((void*)user_aes_key, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, sizeof(user_aes_key));
97 debug(PSTR("AES key written\n"));
98 }
99#endif
100}
101
102void setup()
103{
104#ifdef MY_IS_RFM69HW
105 _radio.setHighPower(true);
106#endif
107 //_radio.setPowerLevel(10);
108#ifdef MY_RFM69_ATC
109 _radio.enableAutoPower(-70);
110 debug(PSTR("ATC enabled\n"));
111#endif
112
113 for (uint8_t i = 0; i < NUM(sensors); i++)
114 {
115 struct sensor_t *sensor = &sensors[i];
116 if (sensor->type & SENSOR_BUTTON)
117 pinMode(sensor->button.pin, INPUT_PULLUP);
118 }
119}
120
121void presentation()
122{
123 sendSketchInfo("LRSwitch", "1.0");
124
125 // register all sensors to gw (they will be created as child devices)
126 for (uint8_t i = 0; i < NUM(sensors); i++)
127 {
128 struct sensor_t *sensor = &sensors[i];
129 if (sensor->type & SENSOR_SCENE)
130 present(sensor->id, S_SCENE_CONTROLLER);
131 }
132
133#if TEMP_SENSOR_ID >= 0
134 present(TEMP_SENSOR_ID, S_TEMP);
135#endif
136}
137
138void loop()
139{
140 //TODO maybe call _radio.rcCalibration() all 1000x changes?
141
142 struct sensor_t *sensor = &sensors[0];
143 uint8_t pin = sensor->button.pin;
144
145 // delay() instead of sleep()
146 // sleep() will for whatever reason trigger the external interrupt?!
147 delay(1000);
148
149 Serial.println("r");
150 Serial.flush();
151 uint8_t state_before = digitalRead(pin);
152 int8_t intr = sleep(digitalPinToInterrupt(pin), CHANGE, 0);
153 if (intr == digitalPinToInterrupt(pin))
154 {
155 uint8_t state_now = digitalRead(pin);
156 if (state_before != state_now)
157 {
158 Serial.println("m");
159 send(msg.setType(V_SCENE_ON).setSensor(sensor->id).set(1));
160 }
161 }
162
163#if TEMP_SENSOR_ID >= 0
164 checkTemperature();
165#endif
166}
167
168inline void checkTemperature(void)
169{
170 static unsigned long lastTempUpdate = millis();
171 static unsigned int numTempUpdates = 0;
172 static float lastTemp = 0;
173 static MyMessage msgTemp(TEMP_SENSOR_ID, V_TEMP);
174
175 unsigned long now = millis();
176 if (now - lastTempUpdate > TEMP_READ_INTERVAL)
177 {
178 float temp = _radio.readTemperature() + TEMP_OFFSET;
179 lastTempUpdate = now;
180 if (isnan(temp))
181 Serial.println(F("Failed reading temperature"));
182 else if (abs(temp - lastTemp) >= 2 || numTempUpdates == TEMP_N_READS_MSG)
183 {
184 lastTemp = temp;
185 numTempUpdates = 0;
186 send(msgTemp.set(temp, 2));
187#ifdef MY_DEBUG
188 char str_temp[6];
189 dtostrf(temp, 4, 2, str_temp);
190 debug(PSTR("Temperature: %s °C\n"), str_temp);
191#endif
192 }
193 else
194 ++numTempUpdates;
195 }
196}
197