-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHueBridge-example.ino
249 lines (176 loc) · 5.92 KB
/
HueBridge-example.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
/*------------------------------------------------------------------------------
Example Sketch for Phillips Hue
This lib requires you to have the neopixelbus lib in your arduino libs folder,
for the colour management header files <RgbColor.h> & <HslColor.h>.
https://github.com/Makuna/NeoPixelBus/tree/UartDriven
*** Needs to be UARTDRIVEN branch, or Animator Branch ***
------------------------------------------------------------------------------*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ESP8266SSDP.h>
#include <NeoPixelBus.h>
#include <ArduinoJson.h>
#include "HueBridge.h"
#include <NeoPixelBus.h> // NeoPixelAnimator branch
const char* host = "Hue-Bridge";
const char* ssid = "SKY";
const char* pass = "wellcometrust";
const uint16_t aport = 8266;
WiFiServer TelnetServer(aport);
WiFiClient Telnet;
WiFiUDP OTA;
ESP8266WebServer HTTP(80);
//HueBridge* Hue = NULL; // used for dynamic memory allocation...
#define pixelCount 250 // Strip has 30 NeoPixels
#define pixelPin 2 // Strip is attached to GPIO2 on ESP-01
#define HUEgroups 5
#define HUElights 10
HueBridge Hue(&HTTP, HUElights, HUEgroups, &HandleHue);
NeoPixelBus strip = NeoPixelBus(pixelCount, pixelPin);
NeoPixelAnimator animator(&strip); // NeoPixel animation management object
void HandleHue(uint8_t Lightno, struct RgbColor rgb, HueLight* lightdata) {
// Hue callback....
// Serial.printf( "\n | Light = %u, Name = %s (%u,%u,%u) | ", Lightno, lightdata->Name, lightdata->Hue, lightdata->Sat, lightdata->Bri);
//temporarily holds data from vals
char x[10];
char y[10];
//4 is mininum width, 3 is precision; float value is copied onto buff
dtostrf(lightdata->xy.x, 5, 4, x);
dtostrf(lightdata->xy.y, 5, 4, y);
Serial.printf( " | light = %3u, %15s , RGB(%3u,%3u,%3u), HSB(%5u,%3u,%3u), XY(%s,%s),mode=%s | \n",
Lightno, lightdata->Name, rgb.R, rgb.G, rgb.B, lightdata->hsb.H, lightdata->hsb.S, lightdata->hsb.B, x, y, lightdata->Colormode);
Lightno--;
RgbColor original = strip.GetPixelColor(Lightno);
AnimUpdateCallback animUpdate = [=](float progress)
{
RgbColor updatedColor;
if (lightdata->State) {
updatedColor = RgbColor::LinearBlend(original, rgb, progress);
} else {
updatedColor = RgbColor::LinearBlend(original, RgbColor(0,0,0), progress);
}
strip.SetPixelColor(Lightno, updatedColor);
};
animator.StartAnimation(Lightno, 1000, animUpdate);
}
void setup() {
strip.Begin();
strip.Show();
Serial.begin(115200);
delay(500);
Serial.println("");
Serial.println("Arduino HueBridge Test");
Serial.printf("Sketch size: %u\n", ESP.getSketchSize());
Serial.printf("Free size: %u\n", ESP.getFreeSketchSpace());
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED ) {
delay(500);
i++;
Serial.print(".");
if (i == 20) {
Serial.print("Failed");
ESP.reset();
break;
} ;
}
if(WiFi.waitForConnectResult() == WL_CONNECTED){
MDNS.begin(host);
MDNS.addService("arduino", "tcp", aport);
OTA.begin(aport);
TelnetServer.begin();
TelnetServer.setNoDelay(true);
delay(10);
Serial.print("\nIP address: ");
String IP = String(WiFi.localIP());
//Serial.println(IP);
Serial.println(WiFi.localIP());
Hue.Begin();
HTTP.begin();
} else {
Serial.println("CONFIG FAILED... rebooting");
ESP.reset();
}
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
}
void loop() {
HTTP.handleClient();
yield();
OTA_handle();
static unsigned long update_strip_time = 0; // keeps track of pixel refresh rate... limits updates to 33 Hz
if (millis() - update_strip_time > 30)
{
if ( animator.IsAnimating() ) animator.UpdateAnimations(100);
strip.Show();
update_strip_time = millis();
}
}
void OTA_handle(){
if (OTA.parsePacket()) {
IPAddress remote = OTA.remoteIP();
int cmd = OTA.parseInt();
int port = OTA.parseInt();
int size = OTA.parseInt();
Serial.print("Update Start: ip:");
Serial.print(remote);
Serial.printf(", port:%d, size:%d\n", port, size);
uint32_t startTime = millis();
WiFiUDP::stopAll();
if(!Update.begin(size)){
Serial.println("Update Begin Error");
return;
}
WiFiClient client;
bool connected = false;
delay(2000);
//do {
connected = client.connect(remote, port);
//} while (!connected || millis() - startTime < 20000);
if (connected) {
uint32_t written;
while(!Update.isFinished()){
written = Update.write(client);
if(written > 0) client.print(written, DEC);
}
Serial.setDebugOutput(false);
if(Update.end()){
client.println("OK");
Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
ESP.restart();
} else {
Update.printError(client);
Update.printError(Serial);
}
} else {
Serial.printf("Connect Failed: %u\n", millis() - startTime);
ESP.restart();
}
}
//IDE Monitor (connected to Serial)
if (TelnetServer.hasClient()){
if (!Telnet || !Telnet.connected()){
if(Telnet) Telnet.stop();
Telnet = TelnetServer.available();
} else {
WiFiClient toKill = TelnetServer.available();
toKill.stop();
}
}
if (Telnet && Telnet.connected() && Telnet.available()){
while(Telnet.available())
Serial.write(Telnet.read());
}
if(Serial.available()){
size_t len = Serial.available();
uint8_t * sbuf = (uint8_t *)malloc(len);
Serial.readBytes(sbuf, len);
if (Telnet && Telnet.connected()){
Telnet.write((uint8_t *)sbuf, len);
yield();
}
free(sbuf);
}
//delay(1);
}