-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhilipsHueApi.ino
248 lines (211 loc) · 6.11 KB
/
PhilipsHueApi.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
//----------------------------------------------------------------------------------------
//File: PhilipsHueApi.ino
//
//Autor: Tobias Rothlin
//Versions:
// 1.0 Inital Version
//----------------------------------------------------------------------------------------
//Include Arduino Library
#include <SPI.h>
#include <Ethernet.h>
#include <String.h>
//Request buffer size for incomming .json getHueState()
#define REQ_BUF_SZ 500
//Hue API parameters specific to Hue Bridge
const char hueHubIP[] = "192.168.1.14"; //Hue Bridge IP
const char hueUsername[] = "HdFwM9X9b1tOQHTkZ4sEe8B8JWNUPFjD6FyxZIbl"; //Hue Bridge username
const int hueHubPort = 80; //Always port 80
//External pins
int pinSwitchIn = 5;
int pinStatusLed = 6;
//System variables
int periodicCheckInterval = 10000;
int numberOfChecks = 0;
int durationLongpress = 500;
//Hue status variables
boolean hueLampIsOn;
int hueLampBrightness;
long hueLampHueValue;
//Network settings Arduino shield
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x7C, 0x0F }; //MAC address
IPAddress ip(192, 168, 1, 23); // Arduino IP
EthernetClient client;
//Initial Setup
void setup()
{
//Set pin modes
pinMode(10, OUTPUT); //Set pin 10 to High used by the ethernet shield and SPI.h lib
digitalWrite(10, HIGH);
pinMode(pinSwitchIn, INPUT);
pinMode(pinStatusLed, OUTPUT);
//Setup Serial Output @ Bauderate 9600
Serial.begin(9600);
//Setup the Ethernet
Ethernet.begin(mac, ip);
//Get the current state of the second Hue lamp
getHueState(2);
}
void loop()
{
//Delay to slow down polling frequency
delay(1);
//Indicates Arduino is in polling Mode -> Idel light (not nessesary)
digitalWrite(pinStatusLed, HIGH);
//Polling checks if the switch is presses
if (digitalRead(pinSwitchIn) == 1)
{
//Turns off idel light
digitalWrite(pinStatusLed, LOW);
//Gets current state of the second Hue lamp
getHueState(2);
//Measures the duration of the button press
int pressduration = 0;
while (digitalRead(pinSwitchIn) == 1 && pressduration <= 100 )
{
delay(1);
pressduration++;
}
//if the Hue lamp is turend on -> turn off Hue lamp
if (hueLampIsOn == 1)
{
HueTurnOff(2);
}
//else -> turn on Hue lamp
else
{
//if the button press is longer than 100ms -> the lamp will dim (Night light mode)
if (pressduration > 100)
{
HueTurnOnDim(2,50);
}
//else -> turn on Hue lamp
else
{
HueTurnOn(2);
}
}
//Waits until the button is released
while (digitalRead(pinSwitchIn) == 1 )
{
delay(1);
}
}
}
//Turns the Hue lamp on at max brightness
void HueTurnOn(int lightNum)
{
String command = "{\"on\": true , \"bri\": 254}";
setHue(lightNum, command);
}
//Turns the Hue lamp off
void HueTurnOff(int lightNum)
{
String command = "{\"on\": false}";
setHue(lightNum, command);
}
//Slowly increses the the brightness to the value bri
void HueTurnOnDim(int lightNum, int bri)
{
String command = "{\"on\": true, \"bri\": " + String(bri) + ",\"transitiontime\": 200}";
setHue(lightNum, command);
}
//The generic function to send commands to the Hue bridge
void setHue(int lightNum, String command) //int lightNum -> The lightNumber on the Hue bridge
//String command -> The .json with the light parameters.
// The exact form is shown in the API documantaion
// or seen in the functions above(HueTurnOn/HueTurnOnDim)
{
if (client.connect(hueHubIP, hueHubPort))
{
//Used to terminate the connection after 100 loops
int NumberOfLoops = 0;
while (client.connected())
{
//send the data to the network shield ontop the Arduino
client.print("PUT /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(lightNum);
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(hueHubIP);
client.print("Content-Length: ");
client.println(command.length());
client.println("Content-Type: text/plain;charset=UTF-8");
client.println();
client.println(command);
NumberOfLoops++;
if (NumberOfLoops == 100) {
break;//Terminates the connection after 100 loops can depend on the speed of the network connection
}
}
client.stop();
}
}
//Get Hue state updates the variables
boolean getHueState(int lightNum)
{
String Jason_req;
String getJason;
char Buf[500] = {""};
int MaxLenght = 43;
int currentLenght = 0;
if (client.connect(hueHubIP, hueHubPort)) //Sends the request to the hue bridge
{
client.print("GET /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(lightNum);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(hueHubIP);
client.println("Content-type: application/json");
client.println("keep-alive");
client.println();
//Saves the response .json in a String
while (client.connected())
{
if (client.available())
{
char c = client.read();
Jason_req.toCharArray(Buf, Jason_req.length());
if (strstr(Buf, "state") > 1) {
currentLenght++;
if (currentLenght < MaxLenght) {
getJason = getJason + c;
}
}
else {
Jason_req = Jason_req + c;
}
}
}
String State = "";
String bri = "";
//Extracts the state of the Hue lamp
int startPos = getJason.indexOf("on");
int endPos = getJason.indexOf(",");
State = getJason.substring(startPos + 4, endPos );
getJason = getJason.substring(endPos + 1);
//Sets the state of the Hue lamp
if (State == "true") {
hueLampIsOn = true;
}
else {
hueLampIsOn = false;
}
//Extracts the brightness of the Hue lamp
startPos = getJason.indexOf("bri");
endPos = getJason.indexOf(",");
bri = getJason.substring(startPos + 5, endPos );
getJason = getJason.substring(endPos + 1);
hueLampBrightness = bri.toInt();
//Terminates the connection;
client.stop();
Jason_req = "";
return true;
}
else
return false;
}