-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLutronController.ino
More file actions
338 lines (283 loc) · 7.89 KB
/
LutronController.ino
File metadata and controls
338 lines (283 loc) · 7.89 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
LutronController
/rowantrollope
*/
using namespace std;
#include <map>;
#include "InternetButton.h";
#include "LutronBridge.h"
LutronBridge lutron;
byte lutronIP[] = { 10, 0, 0, 201 }; // My RadioRA2 server
InternetButton b = InternetButton();
Thread *ledBreatherThread;
String sLastStatus;
String sDimmerStates;
int nLastPress=0;
// Disconnect reconnect to Telnet every once in a while, currently every
bool bConnected=false;
// This is the code for a periodic reconnect - as I've seen inconsistencies when
// we stay connected for too long
bool bReconnect=false;
/*
Timer reconnectTimer(20 * 60 * 1000, reconnectTelnet);
void reconnectTelnet() {
bReconnect=true;
}
*/
typedef struct
{
int buttonID;
int ledID;
int deviceID;
} BUTTON_DATA;
BUTTON_DATA button1 = { 2, 3, 71 };
BUTTON_DATA button2 = { 4, 9, 72 };
void setup()
{
// For my Lutron Bridge I'm doubling up and using a Photon in an InternetButton
// shield so I can control a few lights.
b.begin();
Particle.function("sendCmd", sendCommand);
Particle.function("setDimmer", setDimmer);
Particle.function("getDimmer", getDimmer);
Particle.function("getAll", getAllDimmers);
Particle.function("setAll", setAllDimmers);
Particle.function("setButton1", setBtn1);
Particle.function("setButton2", setBtn2);
Particle.function("reconnect", reconnectLutron);
Particle.variable("lastStatus", sLastStatus);
Particle.variable("dimmerStates", sDimmerStates);
// Make sure your Serial Terminal app is closed before powering your device
Serial.begin(9600);
// initialize LEDs
ledBreatherThread = new Thread("ledBreather", ledBreather, NULL);
b.allLedsOff();
lutron.setNotifyCallback(dimmerChanged);
// set this flag to initialize all light states
lutron.m_bMonitor = true;
bConnected = lutron.connect(lutronIP);
}
// debug: testing reconnect
int reconnectLutron(String sCommand)
{
bReconnect = true;
}
int getAllDimmers(String sCommand)
{
sDimmerStates = lutron.getAllDimmers();
return 1;
}
int setAllDimmers(String sCommand)
{
return lutron.setAllDimmers(sCommand);
}
int sendCommand(String sCommand)
{
return lutron.sendCommand(sCommand);
}
int setDimmer(String sDimmer)
{
return lutron.setDimmer(sDimmer);
}
int getDimmer(String sDimmer)
{
return lutron.getDimmer(sDimmer);
}
void loop()
{
if(!bConnected)
{
bConnected = lutron.connect(lutronIP);
delay(3000);
}
if(bReconnect)
{
lutron.disconnect();
bConnected = bReconnect = false;
}
// Check if any buttons are depressed, turn on the LED then send the
// command to Lutron bridge
bool bButtonPressed=false;
if(b.buttonOn(2))
{
nLastPress = 2;
handleButtonPress(2);
delay(500);
return;
}
else if (b.buttonOn(4))
{
nLastPress = 4;
handleButtonPress(4);
delay(500);
return;
}
else if(b.buttonOn(1)) // RAISE
{
LUTRON_DEVICE device = lutron.getDevice(getDeviceIDFromButton(nLastPress));
if(device.currentLevel == 100)
return;
while(b.buttonOn(1) && device.currentLevel < 100)
{
delay(200);
device.currentLevel += 10;
setLEDState(nLastPress, device.currentLevel, false);
}
if (device.currentLevel > 100)
device.currentLevel = 100;
lutron._setDimmer(getDeviceIDFromButton(nLastPress), device.currentLevel);
}
else if (b.buttonOn(3)) // LOWER
{
LUTRON_DEVICE device = lutron.getDevice(getDeviceIDFromButton(nLastPress));
if(device.currentLevel == 0)
return;
while(b.buttonOn(3) && device.currentLevel > 0)
{
delay(200);
device.currentLevel -= 10;
setLEDState(nLastPress, device.currentLevel, false);
}
if(device.currentLevel < 0)
device.currentLevel = 0;
lutron._setDimmer(getDeviceIDFromButton(nLastPress), device.currentLevel);
}
}
void dimmerChanged(int nDeviceID)
{
// Check if we care about this light
// LutronBridge will inform us of ALL light events
if(nDeviceID == button1.deviceID || nDeviceID == button2.deviceID)
{
float fLevel = lutron.getDevice(nDeviceID).currentLevel;
String sEventData = String::format("dimmerChanged() - DEVICE=%i,LEVEL=%.02f", nDeviceID, fLevel);
//Particle.publish("lutron/device/changed", sEventData);
Serial.println(sEventData);
setLEDState(getButtonIDFromDevice(nDeviceID), fLevel, true);
}
else
{
//String str = String::format("dimmerChanged() - IGNORING DEVICE: %i", nDeviceID);
//Serial.println(str);
return;
}
}
int getDeviceIDFromButton(int button)
{
if(button1.buttonID == button)
return button1.deviceID;
else if(button2.buttonID == button)
return button2.deviceID;
else
return -1;
}
int getButtonIDFromDevice(int device)
{
if(button1.deviceID == device)
return button1.buttonID;
else if(button2.deviceID == device)
return button2.buttonID;
else
return -1;
}
void handleButtonPress(int nButtonID)
{
Serial.println("loop() - Button Pressed.");
int deviceID = getDeviceIDFromButton(nButtonID);
LUTRON_DEVICE device = lutron.getDevice(deviceID);
String sDebug = String::format("handleButtonPress() - DeviceID: %i, device.currentLevel: %.02f", deviceID, device.currentLevel);
Serial.println(sDebug);
if(device.currentLevel > 0) // If light is ON, turn it off
{
b.playSong("c7,8");
lutron._setDimmer(deviceID, 0);
device.currentLevel = 0;
lutron.updateDevice(deviceID, device);
}
else // Set to ON
{
b.playSong("c5,4");
lutron._setDimmer(deviceID, device.onLevel);
device.currentLevel = device.onLevel;
lutron.updateDevice(deviceID, device);
}
}
void setLEDState(int nButtonID, float nLightLevel, bool bSoft)
{
int nLED = 0;
if(button1.buttonID == nButtonID)
nLED = button1.ledID;
else if(button2.buttonID == nButtonID)
nLED = button2.ledID;
else
return;
if(nLightLevel == 0)
{
String sDebug = String::format("setLEDState() - LED %n OFF", nLED);
Serial.println(sDebug);
b.ledOff(nLED);
return;
}
String sDebug = String::format("setLEDState() - LED: %i - LEVEL: %.02f", nLED, nLightLevel);
Serial.println(sDebug);
if(bSoft)
softLEDOn(nLED,nLightLevel, nLightLevel, nLightLevel);
else
b.ledOn(nLED, nLightLevel, nLightLevel, nLightLevel);
}
void softLEDOn(int nID, int nRed, int nGreen, int nBlue)
{
int nSteps = 10;
for(float i=0;i<1;i+=.1)
{
b.ledOn(nID, nRed*i, nGreen*i, nBlue*i);
delay(50);
}
}
os_thread_return_t ledBreather(void* param)
{
Serial.println("ledBreather() - Thread Started!");
while(true)
{
//for(int n=1;n<11;n++)
{
//int n=1;
for(int i=0;i<35;i++)
{
if(bConnected)
{
b.ledOn(1,0,i,0);
b.ledOn(11,0,i,0);
}
else
{
b.ledOn(1,i,0,0);
b.ledOn(11,i,0,0);
}
delay(25);
}
for(int i=35;i>0;i--)
{
if(bConnected)
{
b.ledOn(1,0,i,0);
b.ledOn(11,0,i,0);
}
else
{
b.ledOn(1,i,0,0);
b.ledOn(11,i,0,0);
}
delay(25);
}
}
}
}
int setBtn1(String sCommand)
{
return button1.deviceID = sCommand.toInt();
}
int setBtn2(String sCommand)
{
return button2.deviceID = sCommand.toInt();;
}