-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathESP8266_HueWiFiClientSecure.ino
88 lines (77 loc) · 2.55 KB
/
ESP8266_HueWiFiClientSecure.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
// https://developers.meethue.com/develop/hue-api/
// $ curl -vX PUT https://HUE_BRIDGE_HOST/api/newdeveloper/lights/2/state -d '{"on":false}'
#include <ESP8266WiFi.h>
const int buttonPin = 5; // Grove adapter I2C_1 or _2 used as D6
const int ledPin = 0; // Built-in LED
const char *ssid = "MY_SSID"; // TODO
const char *password = "MY_PASSWORD"; // TODO
const char *host = "HUE_BRIDGE_HOST"; // TODO
const int port = 443;
int buttonState = 0;
void sendHueStateWebRequest(int lightId, char *content) {
const char *verb = "PUT";
const char *pathPrefix = "/api/newdeveloper/lights/";
const char *pathPostfix = "/state";
// connect to remote host
BearSSL::WiFiClientSecure client; // use TLS
client.setInsecure(); // no cert validation
if (client.connect(host, port)) {
// send HTTP request
client.print(verb);
client.print(" ");
client.print(pathPrefix);
client.print(lightId);
client.print(pathPostfix);
client.print(" HTTP/1.1\r\n");
client.print("Host: ");
client.print(host);
client.print("\r\n");
client.print("Content-Length: ");
client.print(strlen(content));
client.print("\r\n");
client.print("Content-Type: application/json\r\n");
client.print("Connection: close\r\n\r\n");
client.print(content);
// read HTTP response
while (client.connected() || client.available()) {
int ch = client.read();
while (ch >= 0) {
Serial.print((char) ch);
ch = client.read();
}
}
Serial.print("\n");
}
}
void setup() {
Serial.begin(115200);
//Serial.setDebugOutput(true);
Serial.print("\nConnecting to network ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100); // keeps watchdog happy
}
Serial.print("Connected to network, local IP = ");
Serial.println(WiFi.localIP());
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // built-in LED is inverted
}
void loop() {
int buttonValue = digitalRead(buttonPin);
if (buttonState == 0 && buttonValue == HIGH) {
buttonState = 1; // on
digitalWrite(ledPin, LOW); // built-in LED is inverted
sendHueStateWebRequest(2, "{\"on\": true}");
} else if (buttonState == 1 && buttonValue == LOW) {
buttonState = 2;
} else if (buttonState == 2 && buttonValue == HIGH) {
buttonState = 3; // off
digitalWrite(ledPin, HIGH); // built-in LED is inverted
sendHueStateWebRequest(2, "{\"on\": false}");
} else if (buttonState == 3 && buttonValue == LOW) {
buttonState = 0;
}
}