forked from jeremymlong/ESP8266-UPnP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP-UPnP.ino
191 lines (163 loc) · 3.87 KB
/
ESP-UPnP.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
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <WiFiServer.h>
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <SPI.h>
#include <APA.h>
#include <aJSON.h>
#if PRINT_BUFFER_LEN < 2048
#error "Increase PRINT_BUFFER_LEN in aJSON.h"
#endif
#include "Ssdp.h"
#include "WeMoEmulator.h"
#include "HueBridge.h"
#define TREE_PIN 2
#define GARL_PIN 12
#define LED_COUNT 72
DateTime *dateTime;
Ssdp *ssdp;
APA apa(LED_COUNT);
bool state;
uint8 h = 255, s = 255, l = 255;
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Serial Ready");
WiFi.mode(WIFI_STA);
Serial.println("Connecting to WiFi");
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.println("WiFi failed to connect.");
delay(1000);
Serial.println("Double check SSID and password. Restarting...");
delay(30000);
ESP.restart();
}
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
dateTime = new DateTime();
dateTime->setUpdateCallback([]()
{
HttpRequest request(WiFi.gatewayIP(), 80, "/");
HttpResponse response;
HttpClient().openUrl(&request, &response);
String result;
if (response.hasHeader("Date"))
{
HttpHeader* dateHeader = response.getHeader("Date");
result = dateHeader->Value;
Serial.printf("Date: %s\n", result.c_str());
}
return result;
});
dateTime->update();
dateTime->begin();
randomSeed(dateTime->getEpoch());
ssdp = new Ssdp(WiFi.localIP());
ssdp->setDateTime(dateTime);
ssdp->setUdpSendCount(1);
ssdp->addDevice(new WeMoEmulator("Christmas Tree", TREE_PIN, 8080U));
ssdp->addDevice(new WeMoEmulator("Garland", GARL_PIN, 8081U));
HueBridge *bridge = new HueBridge(8082);
HueLight *deskLamp = new HueLight(1, "Desk Lamp");
deskLamp->setBrightnessChangedHandler([](uint8 b)
{
Serial.printf("Set desk lamp brightness to: %u\n", b);
l = b;
updateDeskLamp();
});
deskLamp->setStateChangedHandler([](bool s)
{
Serial.printf("Set desk lamp state to: %s\n", s ? "on" : "off");
state = s;
updateDeskLamp();
});
HueLight *deskLampColor = new HueLight(2, "Lamp Color");
deskLampColor->setStateChangedHandler([](bool value)
{
Serial.printf("Set lamp color to: %s\n", value ? "on" : "off");
s = value ? 255 : 0;
updateDeskLamp();
});
deskLampColor->setBrightnessChangedHandler([](uint8 b)
{
Serial.printf("Set lamp color to: %u\n", b);
h = b;
updateDeskLamp();
});
bridge->addLight(deskLamp);
bridge->addLight(deskLampColor);
ssdp->addDevice(bridge);
ssdp->begin();
apa.setup();
apa.setMaxBrightness(31U);
updateDeskLamp();
Serial.println("setup complete");
}
void setHslColor(uint32 h, uint32 s, uint32 l)
{
uint32 r, g, b;
HSL_to_RGB(h, s, l, &r, &g, &b);
setRgbColor(r, g, b);
}
void setRgbColor(uint8 r, uint8 g, uint8 b)
{
apa.beginFrame();
for (int i = 0; i < LED_COUNT; i++)
{
apa.setPixelColor(r, g, b);
}
apa.endFrame();
}
void updateDeskLamp()
{
if (state)
{
setHslColor(h, s, l);
}
else
{
setRgbColor(0, 0, 0);
}
Serial.printf("Free Heap: %u\n", ESP.getFreeHeap());
}
void loop()
{
dateTime->loop();
ssdp->loop();
delay(1);
}
void HSL_to_RGB(uint32_t hue, uint32_t sat, uint32_t lum, uint32_t* r, uint32_t* g, uint32_t* b)
{
uint32_t v;
v = (lum < 128) ? (lum * (255 + sat)) >> 8 :
(((lum + sat) << 8) - lum * sat) >> 8;
if (v <= 0) {
*r = *g = *b = 0;
}
else {
uint32_t m;
uint32_t sextant;
uint32_t fract, vsf, mid1, mid2;
m = lum + lum - v;
hue *= 6;
sextant = hue >> 8;
fract = hue - (sextant << 8);
vsf = v * fract * (v - m) / v >> 8;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant) {
case 0: *r = v; *g = mid1; *b = m; break;
case 1: *r = mid2; *g = v; *b = m; break;
case 2: *r = m; *g = v; *b = mid1; break;
case 3: *r = m; *g = mid2; *b = v; break;
case 4: *r = mid1; *g = m; *b = v; break;
case 5: *r = v; *g = m; *b = mid2; break;
}
}
}