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