Repository navigation
Expand file tree
/
Copy pathrgb_light.ino
More file actions
304 lines (262 loc) · 7.16 KB
/
Copy pathrgb_light.ino
File metadata and controls
304 lines (262 loc) · 7.16 KB
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
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <stdio.h>
#include "FS.h"
String split(String data, char separator, int index);
void loadConf();
void saveConf();
void setColor(int tr, int tg, int tb);
/*----------------------*/
/* Config */
/* ---------------------*/
// Networking
char ssid[] = "SSID";
char pass[] = "password";
unsigned int discovery_port = 1900;
IPAddress ipMulti(239, 255, 255, 250);
IPAddress ip;
ESP8266WebServer server(80);
WiFiUDP udp;
// Hardware
int ledRed = 12;
int ledGreen = 14;
int ledBlue = 13;
// Default color
int r = 128;
int g = 128;
int b = 128;
// Whitebalace
float wbr = 1;
float wbg = 1;
float wbb = 1;
bool onState = 0;
String name = "light";
int brightness = 100;
/*----------------------*/
/* SETUP */
/* ---------------------*/
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Starting HW init...");
analogWriteRange(255);
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
}
delay(100);
loadConf();
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
Serial.println("Starting WiFi...");
// Connect to wifi and wait for connection
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
ip = WiFi.localIP();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(ip);
Serial.println("Starting UDP listen...");
// Start UDP
udp.beginMulticast(WiFi.localIP(), ipMulti, discovery_port);
Serial.println("Starting web server...");
// Start the server
server.on("/", pageIndex);
server.on("/status", pageStatus);
server.onNotFound(page404);
server.begin();
}
/*----------------------*/
/* LOOP */
/* ---------------------*/
void loop() {
// UPNP Discovery - vury cut down version
int packetSize = udp.parsePacket();
if (packetSize) {
// read the packet into packetBufffer
char temp[20];
udp.read(temp, 20);
String packetBuffer = temp;
if (packetBuffer.indexOf("M-SEARCH * HTTP/1.1") != -1) {
static const char responseTemplate[] = "HTTP/1.1 200 OK\n"
"EXT:\n"
"LOCATION: http://%u.%u.%u.%u:80/\n" // WiFi.localIP()
"SERVER: Arduino/ESP8266, UPnP/1.0, ZIP-Light/0.1\n"
"ST:upnp:rootdevice\n"
"USN: uuid:22b2bd50-00e3-11e7-93ae-92361f002671::upnp:rootdevice\n"
"\n";
char buffer[1460];
snprintf(buffer, sizeof(buffer), responseTemplate, ip[0], ip[1], ip[2], ip[3]);
// Send UPNP response
udp.beginPacket(udp.remoteIP(), discovery_port);
udp.write(buffer);
udp.endPacket();
}
}
// Web server
server.handleClient();
}
/*----------------------*/
/* WEB PAGES */
/* ---------------------*/
void pageIndex() {
bool save = 0;
// Get new name from POST
if (server.hasArg("name")) {
String temp = server.arg("name");
if (temp != name) {
name = temp;
save = 1;
}
}
// Get ON state from POST
if (server.hasArg("set")) {
if (server.hasArg("on")) {
if (!onState) {
setColor(r, g, b);
onState = 1;
}
}
else {
if (onState) {
setColor(0, 0, 0);
onState = 0;
}
}
}
// Get brightness from POST
if (server.hasArg("brightness")) {
brightness = server.arg("brightness").toInt();
setColor(r, g, b);
}
// Get white balance from POST
if (server.hasArg("wb")) {
String wb = server.arg("wb");
float twb = split(wb, 0).toFloat();
if (wbr != twb) {
wbr = twb;
save = 1;
}
twb = split(wb, 1).toFloat();
if (wbg != twb) {
wbg = twb;
save = 1;
}
twb = split(wb, 2).toFloat();
if (wbb != twb) {
wbb = twb;
save = 1;
}
}
// Get color from POST
if (server.hasArg("color") & onState) {
String color = server.arg("color");
r = split(color, 0).toInt();
g = split(color, 1).toInt();
b = split(color, 2).toInt();
setColor(r, g, b);
}
// Should the config be saved?
if (save) {
saveConf();
}
// Check API in POST
if (server.hasArg("api")) {
server.send (200, "text/plain", "OK");
}
else {
String color = String(r) + "," + String(g) + "," + String(b);
String checked = "";
if (onState) {
checked = "checked";
}
String temp = (String)""
"<html><head><title>Light controls</title></title><body>"
"<form method='post'>"
" IP: " + ip[0] + "." + ip[1] + "." + ip[2] + "." + ip[3]+ "<br>"
" NAME: <input type='text' name='name' value='" + name + "'><br>"
" STATE: <input type='checkbox' name='on' value='on' " + checked + "><br>"
" COLOR: <input name='color' type='text' value='" + color +"'><br>"
" BRIGHTNESS: <input type='text' name='brightness' value='" + brightness + "'><br>"
" WB: <input type='text' name='wb' value='" + wbr + "," + wbg + "," + wbb + "'><br>"
" API: <input type='checkbox' name='api' value='api'><br>"
" <input type='submit' value='Set' name='set'>"
"</form>"
"</body></html>";
server.send (200, "text/html", temp);
}
}
void pageStatus() {
String color = String(r) + "," + String(g) + "," + String(b);
String temp = ""
"\nNAME:" + name +
"\nCOLOR:" + color +
"\nSTATE:" + onState +
"\nWB:" + wbr + "," + wbg + "," + wbb +
"\nBRIGHTNESS:" + brightness +
"";
server.send(200, "text/plain", temp);
}
void page404() {
server.send (404, "text/plain", "404 - Not Found");
}
/*----------------------*/
/* Functions */
/* ---------------------*/
// Read device name from config file on FS
void loadConf() {
File configFile = SPIFFS.open("name.cfg", "r");
if (!configFile) {
Serial.println("Can't load config");
}
else {
String temp = configFile.readString();
configFile.close();
name = split(temp, 0);
wbr = split(temp, 1).toFloat();
wbg = split(temp, 2).toFloat();
wbb = split(temp, 3).toFloat();
}
}
// Write device name into config file on FS and set it to global variable
void saveConf() {
String temp = name + "," + (String)wbr + "," + (String)wbg + "," + (String)wbb;
File configFile = SPIFFS.open("name.cfg", "w");
if (!configFile) {
Serial.println("Can't save config");
}
else {
configFile.print(temp);
configFile.close();
}
}
// Sets LED color
void setColor(int tr, int tg, int tb) {
tr = int((tr * wbr) * ((float)brightness/100));
tg = int((tg * wbg) * ((float)brightness/100));
tb = int((tb * wbb) * ((float)brightness/100));
if (tr > 1023 || tr < 0) { tr = 1023; }
if (tg > 1023 || tg < 0) { tg = 1023; }
if (tb > 1023 || tg < 0) { tb = 1023; }
analogWrite(ledRed, tr);
analogWrite(ledGreen, tg);
analogWrite(ledBlue, tb);
}
String split(String data, int index) {
char separator = 44; // 44 -> ","
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}