-
Notifications
You must be signed in to change notification settings - Fork 2
/
EspLight-firmware.ino
468 lines (422 loc) · 9.93 KB
/
EspLight-firmware.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
Author: Duality / Robert
This is Firmware for controlling ledstrips with a esp8266.
It includes a way of setting which strip is connected, (webinterface)
and how long that ledstrip is.
It also includes a Way of finding the device through
broadcastig a udp packet. (esplightcontroller app)
It also includes a way for controlling which effect is selected.
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <Ticker.h>
#include <otaupload.h>
#include "stripcontrol.h"
#include "html.h"
#include "effectParse.h"
#define AP_BUTTON 0
#define STATUS_LED_PIN 16
#define SERIALBAUD 115200
#define EFFECTPORT 1337
#define WIFI_CHANNEL 4
#define WEBSERVERPORT 80
#define EEPROMSIZE 1024
#define SERVERTEST false
#define ENABLEOTA false
// enable all Serial printing.
#define SERIALDEBUGPRINTING true
// sets Serial.setDebugOutput()
#define ENABLESERIALDEBUG true
// set initial board name and wifi settings.
String board_name = "EspLight01";
String sta_ssid = "esplight_ssid";
String sta_pass = "esplight_password";
// select an initial mode.
enum {STA_MODE, AP_MODE};
int currentMode = STA_MODE;
// set an initial pincode.
int accessPin = 1234;
String magicEepromWord = "deadbeaf";
uint8_t magicWorldLen;
// create a stripcontrol structure and clear it.
stripcontrol_t stripcontrol = {
.pincode = 0,
.effect = 0,
.brightness = 0,
.varZero = 0,
.varOne = 0,
.varTwo = 0,
.changed = false
};
String available_aps;
// select intial ledstrip
int stripselect = ANALOGSTRIP;
// select an initial length.
int striplen = 1;
// setup http server
ESP8266WebServer server(WEBSERVERPORT);
// setup ticker and status blink function.
Ticker statusBlinker;
// types that define speed intervals that indicate status on led.
typedef struct
{
int connecting;
int connected;
int apmode;
int error;
} board_state_t;
board_state_t board_state =
{
.connecting = 85,
.connected = 1000,
.apmode = 3000,
.error = -1
};
// the actuall function that sets the state of the led.
void statusBlink()
{
static uint8_t state;
digitalWrite(STATUS_LED_PIN, state);
state = !state;
}
// function to set a state.
void setStatus(int state)
{
if(state == board_state.error)
{
statusBlinker.detach();
digitalWrite(STATUS_LED_PIN, LOW);
}
else
{
statusBlinker.attach_ms(state, statusBlink);
}
}
// setup the ledstate.
void setupStatusLed()
{
pinMode(STATUS_LED_PIN, OUTPUT);
setStatus(board_state.connecting);
}
Ticker heapPrinter;
void heapPrint()
{
Serial.printf("heap: %d \n", ESP.getFreeHeap());
}
void setupHeapPrint()
{
heapPrinter.attach(10, heapPrint);
}
// stores a string into eeprom plus nullterminator.
void storeString(String string, int& addr)
{
// get a reference to the original c string.
const char *str = string.c_str();
// get the length of that string.
int str_len = strlen(str);
int i;
// loop over the length of the string.
// and store the bytes. and zero terminator.
for(i = 0; i <= str_len; i++)
{
// write the bytes into the eeprom (flash)
EEPROM.write(addr+i, str[i]);
delay(0);
}
addr += i;
}
void storeInt(int value, int& addr)
{
char fmtstr[100];
int i;
for(i = 0; i < sizeof(value); i++)
{
EEPROM.write(addr+i, (value>>(i*8)&0xff));
delay(0);
}
addr += i;
}
String loadString(int &addr)
{
String text = "";
char read = EEPROM.read(addr);
/* Basicly how you read any string in C. */
while(read != '\0')
{
text += read;
addr++;
read = EEPROM.read(addr);
delay(0);
}
addr++; //acount for zero terminator.
return text;
}
// checks for the magic string in eeprom indicating valid settings.
bool magicStrPresent(int &addr)
{
char text[] = "deadbeaf";
uint8_t magicWordLen = magicEepromWord.length();
for(int i = 0; i < magicWordLen; i++)
{
text[i] = EEPROM.read(addr);
addr++;
delay(0);
}
addr++;
//acount for zero terminator
Serial.printf("\nread: %s \n", text);
Serial.printf("should be: %s \n", magicEepromWord.c_str());
return (String(text) == magicEepromWord);
}
int loadInt(int &addr)
{
int value = 0;
int i;
for(i = 0; i < 0x04; i++)
{
char byte = EEPROM.read(addr+i);
value |= (byte << (8*i));
}
addr += i;
return value;
}
void settingsStore()
{
/*
stored are:
board_name,
sta ssid,
sta pass,
accesPin,
stripselect,
currentMode
*/
int eeAddr = 0;
Serial.println("storing: ");
storeString(magicEepromWord, eeAddr);
Serial.println("magic word:");
Serial.println(magicEepromWord);
storeString(board_name, eeAddr);
Serial.println("board name:");
Serial.println(board_name);
storeString(sta_ssid, eeAddr);
Serial.println("ssid: ");
Serial.println(sta_ssid);
storeString(sta_pass, eeAddr);
Serial.println("pass: ");
Serial.println(sta_pass);
storeInt(accessPin, eeAddr);
Serial.println("accesspin");
Serial.println(accessPin);
storeInt(stripselect, eeAddr);
Serial.println("strip select: ");
Serial.println(stripselect);
storeInt(striplen, eeAddr);
Serial.println("striplen: ");
Serial.println(striplen);
EEPROM.commit();
}
void settingsLoad()
{
int eeAddr = 0;
bool isValid = magicStrPresent(eeAddr);
// check if settings are valid;
Serial.print("settings are valid: ");
Serial.println(isValid ? "true" : "false");
if(isValid)
{
// valid settings found and load them.
board_name = loadString(eeAddr);
sta_ssid = loadString(eeAddr);
sta_pass = loadString(eeAddr);
accessPin = loadInt(eeAddr);
stripselect = loadInt(eeAddr);
striplen = loadInt(eeAddr);
}
else if(!isValid)
{
// no valid settings found.
// store valid settings.
Serial.println("invalid settings found loading defaults.");
settingsStore();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setupAP()
{
currentMode = AP_MODE;
WiFi.mode(WIFI_OFF);
delay(0);
WiFi.mode(WIFI_AP);
WiFi.softAP(board_name.c_str());
Serial.println();
IPAddress myIP = WiFi.softAPIP();
Serial.println(myIP);
setStatus(board_state.apmode);
}
void setupSTA(bool silent)
{
currentMode = STA_MODE;
WiFi.mode(WIFI_STA);
// WiFi.disconnect();
WiFi.begin(sta_ssid.c_str(), sta_pass.c_str(), WIFI_CHANNEL);
if(!silent)
Serial.println();
// timeout variable.
int i = 0;
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
if(!silent)
Serial.print(".");
// if button pressed switch mode.
wifiModeHandling();
// keep a timeout timer.
i++;
if(i == 20)
{
if(!silent)
Serial.println("Unable to connect");
return;
}
}
setStatus(board_state.connected);
if(!silent)
{
Serial.println();
printWifiStatus();
}
}
void wifiModeHandling()
{
if(!digitalRead(AP_BUTTON))
{
// switch modes as needed.
if(currentMode == STA_MODE)
{
Serial.println("mode is now AP_MODE");
setupAP();
}
/* actually never gets here. but does if we allowed it to switch back.*/
// else
// {
// Serial.println("mode is now STA_MODE");
// // list to output that we are connecting
// setupSTA(false);
// }
while(!digitalRead(AP_BUTTON)) delay(50);
}
}
void setupWifi(bool silent)
{
if(currentMode == STA_MODE)
{
Serial.println("mode is now STA_MODE");
setupSTA(silent);
}
else
{
Serial.println("mode is now AP_MODE");
setupAP();
}
}
void setupWebserver()
{
// attach directories and file handlers.
server.on("/", handleRoot);
server.on("/ledsettings", handleLedSettings);
server.on("/wifisettings", handleWiFiSettings);
server.on("/css", handleCss);
// start the server
server.begin();
Serial.println("done setting up server");
// request a hostname
WiFi.hostname(board_name);
Serial.printf("Hostname set: %s\n", board_name.c_str());
// scan for wifi once.
available_aps = getAvailableNetworks();
}
void setup() {
// setup serial com.
if(SERIALDEBUGPRINTING)
{
Serial.begin(SERIALBAUD);
Serial.setDebugOutput(ENABLESERIALDEBUG);
Serial.println();
}
// prepare eeprom for use.
EEPROM.begin(EEPROMSIZE);
// settingsStore();
// load stored settings.
settingsLoad();
// setup led status
setupStatusLed();
// setup mode switching pin
pinMode(AP_BUTTON, INPUT_PULLUP);
// setup strips for the first time (initialize some pointers and stuff.)
// and also turn off the leds
setupStrips(striplen);
// setup wifi with output.
setupWifi(false);
// enable OTA
Serial.setDebugOutput(ENABLESERIALDEBUG);
setupOta();
Serial.println("done setting up pins, and WifiMode.");
/* Setup server side things.*/
setupWebserver();
// setup the effect parser.
setupEffectParse(EFFECTPORT);
// setup heap printing
setupHeapPrint();
}
void loop() {
if((currentMode == AP_MODE) || SERVERTEST)
{
// only serve pages in Access point mode.
server.handleClient();
}
if(currentMode == STA_MODE)
{
// check if we are still connected.
if(WiFi.status() != WL_CONNECTED)
{
// don't list anything to the serial output.
// but still try to connect.
setupSTA(true);
if(WiFi.status() == WL_CONNECTED)
{
// if reconnected restart anything that uses a port.
setupOta();
setupWebserver();
setupEffectParse(EFFECTPORT);
}
}
// handle ledstrip animations.
handleStrips();
// handle control over effects.
handleEffectUpdate();
// handle switching to AP_MODE
wifiModeHandling();
}
if(ENABLEOTA)
{
// allow uploading over OTA <dev>
handleSketchUpdate();
}
}