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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/**
* 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>
#include <FastLED.h>
#define RELAY_1_PIN 4 // pin number of first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 1 // Total number of attached relays
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
#define RGB_PIN 7
#define NUM_LEDS 30
#define RGB_CHIPSET WS2812B
#define RGB_COLOR_ORDER GRB
#define RGB_CHILD_ID 0
#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
#define TEMP_CHILD_ID 254
MyMessage msgRGB(RGB_CHILD_ID, 0);
static uint8_t brightness = 128;
MyMessage msgRelais(0, V_STATUS);
unsigned long lastTempUpdate = millis();
unsigned int numTempUpdates = 0;
float lastTemp = 0;
MyMessage msgTemp(TEMP_CHILD_ID, V_TEMP);
CRGB leds[NUM_LEDS];
void changeRelay(uint8_t relay, uint8_t val, bool send_update=false);
void before()
{
// set relay pins to output mode + restore to last known state
for (uint8_t relay = 0; relay < NUMBER_OF_RELAYS; relay++)
{
pinMode(relay + RELAY_1_PIN, OUTPUT);
digitalWrite(relay + RELAY_1_PIN, loadState(relay) ? RELAY_ON : RELAY_OFF);
}
#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_RFM69_ATC
_radio.enableAutoPower(-70);
debug(PSTR("ATC enabled\n"));
#endif
FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, NUM_LEDS);
//TODO restore mode(static/ambilight)/color/brightness from flash?
FastLED.setBrightness(brightness);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Ambilight", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(0, S_RGB_LIGHT, "ambilight");
#if 0
for (uint8_t relay = 0; relay < NUMBER_OF_RELAYS; relay++)
present(relay + 1, S_BINARY);
present(TEMP_CHILD_ID, S_TEMP);
#endif
delay(3000);
send(msgRGB.setType(V_STATUS).set(1));
delay(500);
send(msgRGB.setType(V_DIMMER).set(FastLED.getBrightness()));
delay(500);
send(msgRGB.setType(V_RGB).set("ffffff"));
FastLED.show();
}
void loop()
{
//TODO maybe call _radio.rcCalibration() all 1000x changes?
//FastLED.show();
//FastLED.delay(8);
#if 0
// check temperature
unsigned long now = millis();
if (now - lastTempUpdate > TEMP_READ_INTERVAL)
{
float temp = _radio.readTemperature() + TEMP_OFFSET;
lastTempUpdate = now;
if (isnan(temp))
Serial.println("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;
}
#endif
}
void receive(const MyMessage &message)
{
Serial.println(_radio.readRSSI());
if (message.sensor == RGB_CHILD_ID)
{
if (mGetCommand(message) == C_SET)
{
if (message.type == V_STATUS)
{
bool val = message.getBool();
// datatype=0, message=0/1
Serial.println("light on/off");
//TODO restore brightness.
}
else if (message.type == V_RGB && mGetLength(message) == 6)
{
uint32_t colorcode = strtol(message.getString(), NULL, 16);
fill_solid(leds, NUM_LEDS, CRGB(colorcode));
FastLED.show();
}
else if (message.type == V_PERCENTAGE)
{
//TODO fade?
uint8_t val = message.getByte();
if (val < 0 || val > 100)
return;
Serial.print("dim: ");
Serial.println(val, DEC);
brightness = map(val, 0, 100, 0, 255);
Serial.println(brightness, DEC);
// datatype=0, message=1-100
FastLED.setBrightness(brightness);
FastLED.show();
}
}
}
#if 0
if (message.type == V_STATUS && message.sensor >= 1)
{
uint8_t relay = message.sensor - 1;
if (relay >= NUMBER_OF_RELAYS)
{
Serial.print("Invalid relay index:");
Serial.println(relay);
return;
}
if (mGetCommand(message) == C_REQ)
send(msg.setSensor(relay + 1).set(digitalRead(relay + RELAY_1_PIN)));
else if (mGetCommand(message) == C_SET)
changeRelay(relay, message.getBool() ? RELAY_ON : RELAY_OFF);
}
#endif
}
void changeRelay(uint8_t relay, uint8_t value, bool send_update)
{
if (relay >= NUMBER_OF_RELAYS)
return;
Serial.print("Incoming change for relay: ");
Serial.print(relay);
Serial.print(", New status: ");
Serial.println(value);
// change relay state + store state in eeprom
digitalWrite(relay + RELAY_1_PIN, value);
saveState(relay, value);
// send msg
if (send_update)
send(msgRelais.setSensor(relay + 1).set(value));
}
|