-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_hue.ino
397 lines (354 loc) · 10.7 KB
/
arduino_hue.ino
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <Ethernet.h>
#include <SPI.h>
#include <PubSubClient.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*
* Created 6 Dec 2018
* by Sofia Larsson and Maximilan Falkenström
*/
//HUE
const char hueUsername[] = "1c4738123d9e1f373321fc2e1a934d63";
const char server[] = "192.168.20.129"; //Hue-lights
int groupID;
String status = "";
//Ethernet shield
const byte mac[] = { 0x2A, 0xF8, 0xB7, 0x5B, 0x8F, 0x97 }; //Arduino Ethernet Shield MAC-address
IPAddress ip(195, 178, 254, 88); //Arduino Shield IP-address
IPAddress myDns(192, 168, 0, 1);
EthernetClient client; //Initialize the Ethernet client library
char inputString1[80]; // general purpose input string, 80 bytes
char inputString2[80]; // general purpose input string, 80 bytes
char outputString[80]; // general output string, 80 bytes
//MQTT
const char user[] = "dlevwepg"; //MQTT username
const char password[] = "fYRxSnoN2Ll9"; //MQTT password
const char host[] = "m21.cloudmqtt.com"; //MQTT-server URL
const int port = 14873;
PubSubClient mqttClient(client);
void setup() {
Ethernet.init(10); //Configure the CS pin (pin 10 for most Arduino Shields)
Serial.begin(9600); //data rate in bits per second for data transmission
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
//Serial.println(F("Initialize Ethernet with DHCP:"));
if (Ethernet.begin(mac) == 0) {
//Serial.println(F("Failed to configure Ethernet using DHCP"));
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
//Serial.println(F("Ethernet shield was not found. Sorry, can't run without hardware. :("));
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
//Serial.println(F("Ethernet cable is not connected."));
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
//Serial.print(F(" DHCP assigned IP "));
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(2000);
//Serial.print(F("connecting to "));
//Serial.print(server);
//Serial.println(F("..."));
mqttClient.setServer(host, port);
mqttClient.setCallback(callback);
delay(1000);
setBrightness(50, 3, false);
delay(1000);
setHue(1000L, 3, false);
delay(1000);
setPower(true, 3, false);
delay(2000);
groupID = createGroup();
delay(5000);
}
void loop() {
/*String status1 = getStatus(1);
String status2 = getStatus(2);
String status3 = getStatus(3);
status = status1;
status += ";";
status += status2;
status += ";";
status += status3;
Serial.println(status);*/
const char s[status.length()+1];
status.toCharArray(s, status.length()+1);
if(!mqttClient.connected())
{
connectToMQTT();
}
//boolean mqttMessageSent = mqttClient.publish("fromHue", s, true);
/*
if(mqttMessageSent)
{
Serial.println(F("Successfully sent via MQTT"));
}
else
{
Serial.println(F("Failed to send via MQTT"));
}*/
mqttClient.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
String str = payload;
Serial.println(str);
int lampId = splitString(str, ',', 0).toInt();
String operation = splitString(str, ',', 1);
long value = atol(splitString(str, ',', 2).c_str());
boolean group = false;
Serial.println(String(value) + " " + String(lampId) + " " + operation);
if(lampId == 4) {
group = true;
lampId = groupID;
}
if(operation.equals("power")){
setPower(value, lampId, group);
/*if(value == 1) {
setPower(true, lampId, group);
} else {
setPower(false, lampId, group);
}*/
} else if(operation.equals("brightness")) {
setBrightness(value, lampId, group);
} else if(operation.equals("hue")) {
setHue(value, lampId, group);
}
/*
for (int i=0;i<length;i++) {
char receivedChar = (char)payload[i];
}*/
}
boolean connectToMQTT()
{
const char clientID[] = "Arduino MQTT"; //ID to use for MQTT
const char user[] = "dlevwepg"; //MQTT username
const char password[]= "fYRxSnoM2Ll9"; //MQTT password
while(!mqttClient.connected())
{
Serial.println(F("connecting to mqtt"));
if(mqttClient.connect(clientID, user, password))
{
mqttClient.subscribe("toHue");
}
else
{
delay(5000);
}
}
}
String splitString(String str, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = str.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (str.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? str.substring(strIndex[0], strIndex[1]) : "";
}
const int createGroup() {
int id;
if(client.connect(server, 80)) {
char body[] = "{\"lights\": [\"1\",\"2\",\"3\"],\"name\":\"testgroup\"}";
client.print("POST /api/");
client.print(hueUsername);
client.print("/groups");
client.println(" HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(server);
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(strlen(body) + 1);
client.println();
client.println(body);
client.println();
while(client.connected()) {
if(client.available()) {
client.findUntil("groups/", '\0');
char idBuffer[1];
client.readBytesUntil('\"', idBuffer, 1);
id = atoi(idBuffer);
break;
}
}
}else {
Serial.println(F("Create group: connection failed"));
}
client.flush();
client.stop();
return id;
}
String getStatus(int id) {
String res;
if(client.connect(server, 80)) {
client.print("GET /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(id);
client.println(" HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(server);
client.println("Content-Type: application/json");
client.println();
while(client.connected())
{
if(client.available())
{
client.findUntil("\"on\":", '\0');
String on = client.readStringUntil(',') == "true" ? "1" : "0";
client.findUntil("\"bri\":", '\0');
String brightness = client.readStringUntil(',');
client.findUntil("\"hue\":", '\0');
String hue = client.readStringUntil(',');
res = on;
res += ",";
res += brightness;
res += ",";
res += hue;
break;
}
}
}else {
// if you didn't get a connection to the server:
Serial.println(F("Get status: connection failed"));
}
client.flush();
client.stop();
return res;
}
void setHue(long hue, int id, bool group) {
if(client.connect(server, 80)) {
Serial.println(F("Set hue"));
client.print("PUT /api/");
client.print(hueUsername);
if(group) {
client.print("/groups/");
}
else {
client.print("/lights/");
}
client.print(id); // hueLight zero based, add 1
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(server);
client.println("Content-Length: 13");
client.println("Content-Type: text/plain;charset=UTF-8");
client.println(); // blank line before body
client.println("{\"hue\": " + String(hue) + "}"); // Hue command
client.flush();
client.stop();
} else {
// if you didn't get a connection to the server:
Serial.println(F("Set hue: connection failed"));
}
}
void setBrightness(int brightness, int id, bool group) {
if(client.connect(server, 80)) {
Serial.println(F("Set brightness"));
client.print("PUT /api/");
client.print(hueUsername);
if(group) {
client.print("/groups/");
}
else {
client.print("/lights/");
}
client.print(id); // hueLight zero based, add 1
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(server);
client.println("Content-Length: 13");
client.println("Content-Type: text/plain;charset=UTF-8");
client.println(); // blank line before body
client.println("{\"bri\": " + String(brightness) + "}"); // Hue command
client.flush();
client.stop();
}else {
// if you didn't get a connection to the server:
Serial.println(F("Set brigness: connection failed"));
}
}
void setPower(bool on, int id, bool group) {
if(client.connect(server, 80)) {
Serial.println("Set power");
client.print("PUT /api/");
client.print(hueUsername);
if(group) {
client.print("/groups/");
}
else {
client.print("/lights/");
}
client.print(id); // hueLight zero based, add 1
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(server);
client.println("Content-Length: 14");
client.println("Content-Type: text/plain;charset=UTF-8");
client.println(); // blank line before body
if(on) {
client.println("{\"on\": true}"); // Hue command
}
else {
client.println("{\"on\": false}"); // Hue command
}
client.flush();
client.stop();
} else {
// if you didn't get a connection to the server:
Serial.println(F("Set power: connection failed"));
}
}
/* setHue() is our main command function, which needs to be passed a light number and a
* properly formatted command string in JSON format (basically a Javascript style array of variables
* and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
*/
boolean setHue(int lightNum, const char *command, boolean group)
{
in commandLength = strlen(command);
if (client.connect(server, 80))
{
while (client.connected())
{
client.print("PUT /api/");
client.print(hueUsername);
if(group) {
client.print("/groups/");
}else {
client.print("/lights/");
}
client.print(lightNum); // hueLight zero based, add 1
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(server);
client.print("Content-Length: ");
client.println(commandLength);
client.println("Content-Type: text/plain;charset=UTF-8");
client.println(); // blank line before body
client.println(command); // Hue command
}
client.stop();
return true; // command executed
}
else
return false; // command failed
}