-
Notifications
You must be signed in to change notification settings - Fork 1
/
MagicLantern.ino
262 lines (216 loc) · 6.97 KB
/
MagicLantern.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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WS2812FX.h>
#include <Restfully.h>
#include "configuration.h"
int handleEcho(RestRequest& request);
int handlePowerOn(RestRequest& request);
int handlePowerOff(RestRequest& request);
int handleModeCycle(RestRequest& request);
int handleModeCyclePause(RestRequest& request);
int handleModeFire(RestRequest& request);
int handleModeStatic(RestRequest& request);
int handleModePulse(RestRequest& request);
int handleModeOff(RestRequest& request);
int handleBrightnessPOST(RestRequest& request);
int handleBrightnessGET(RestRequest& request);
void srv_handle_not_found();
// With gratitude to kitesurfer's webserver example - https://github.com/kitesurfer1404/WS2812FX/tree/master/examples
// See https://github.com/esp8266/Arduino/issues/263
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define LED_PIN 14
#define LED_COUNT 24
#define WIFI_TIMEOUT 30000
#define HTTP_PORT 80
#define DEFAULT_COLOR 0xFF5900
#define DEFAULT_BRIGHTNESS 255
#define DEFAULT_SPEED 1000
#define DEFAULT_MODE FX_MODE_STATIC
unsigned long cycle_last_change_time = 0;
unsigned long last_wifi_check_time = 0;
boolean cycle_mode_on = false;
WS2812FX neopixel_ring = WS2812FX(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
ESP8266WebServer server(HTTP_PORT);
RestRequestHandler restHandler;
void sendHeaders() {
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
server.sendHeader("Access-Control-Allow-Methods", "GET");
}
class OptionsRequestHandler : public RequestHandler {
virtual bool canHandle(HTTPMethod method, String uri) {
return method == HTTP_OPTIONS;
}
virtual bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) {
sendHeaders();
server.send(200, "application/json; charset=utf-8", "");
return true;
}
} optionsRequestHandler;
void setup_wifi() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.mode(WIFI_STA);
#ifdef STATIC_IP
WiFi.config(ip, gateway, subnet);
#endif
unsigned long connect_start = millis();
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(millis() - connect_start > WIFI_TIMEOUT) {
Serial.println();
Serial.print("Tried ");
Serial.print(WIFI_TIMEOUT);
Serial.print("ms. Resetting ESP now.");
ESP.reset();
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
}
void setup_neopixel_ring() {
neopixel_ring.init();
neopixel_ring.setMode(DEFAULT_MODE);
neopixel_ring.setColor(DEFAULT_COLOR);
neopixel_ring.setSpeed(DEFAULT_SPEED);
neopixel_ring.setBrightness(DEFAULT_BRIGHTNESS);
neopixel_ring.stop();
}
void setup_http_server() {
server.addHandler(&optionsRequestHandler);
server.addHandler(&restHandler);
server.onNotFound(srv_handle_not_found);
restHandler.on("/api/brightness/:brightness(integer)", POST(handleBrightnessPOST));
restHandler.on("/api/brightness", GET(handleBrightnessGET));
restHandler.on("/api/power/on", POST(handlePowerOn));
restHandler.on("/api/power/off", POST(handlePowerOff));
restHandler.on("/api/mode/cycle", POST(handleModeCycle));
restHandler.on("/api/mode/cycle/pause", POST(handleModeCyclePause));
restHandler.on("/api/mode/fire", POST(handleModeFire));
restHandler.on("/api/mode/static", POST(handleModeStatic));
restHandler.on("/api/mode/pulse", POST(handleModePulse));
restHandler.on("/api/mode/off", POST(handleModeOff));
restHandler.on("/api/echo/:msg(string|integer)", GET(handleEcho) );
}
int handleEcho(RestRequest& request) {
String s("Hello ");
auto msg = request["msg"];
if(msg.isString())
s += msg.toString();
else {
s += '#';
s += (long)msg;
}
request.response["reply"] = s;
return 200;
}
/** Webserver Functions **/
int handlePowerOn(RestRequest& request) {
neopixel_ring.start();
request.response["power"] = "on";
return 200;
}
int handlePowerOff(RestRequest& request) {
neopixel_ring.stop();
request.response["power"] = "off";
return 200;
}
int handleModeCycle(RestRequest& request) {
cycle_mode_on = true;
request.response["cycle"] = "on";
return 200;
}
int handleModeCyclePause(RestRequest& request) {
cycle_mode_on = false;
request.response["cycle"] = "paused";
return 200;
}
int handleModeFire(RestRequest& request) {
cycle_mode_on = false;
neopixel_ring.setMode(FX_MODE_FIRE_FLICKER_INTENSE);
request.response["mode"] = "fire";
return 200;
}
int handleModePulse(RestRequest& request) {
cycle_mode_on = false;
neopixel_ring.setMode(FX_MODE_BREATH);
request.response["mode"] = "pulse";
return 200;
}
int handleModeStatic(RestRequest& request) {
cycle_mode_on = false;
neopixel_ring.setMode(FX_MODE_STATIC);
request.response["mode"] = "static";
return 200;
}
int handleModeOff(RestRequest& request) {
cycle_mode_on = false;
neopixel_ring.stop();
request.response["mode"] = "off";
return 200;
}
int handleBrightnessPOST(RestRequest& request) {
auto brightness = request["brightness"];
Serial.print("Lantern set to ");
Serial.println((long)brightness);
neopixel_ring.setBrightness((long)brightness);
request.response["brightness"] = (long)brightness;
return 200;
}
int handleBrightnessGET(RestRequest& request) {
Serial.println("brightness get");
request.response["brightness"] = neopixel_ring.getBrightness();
return 200;
}
void srv_handle_not_found() {
String path = server.uri();
Serial.print("URI not found: ");
Serial.println(path);
server.send(404, "text/plain", "File Not Found");
}
void setup(){
Serial.begin(115200);
Serial.println("Starting...");
Serial.println("NeoPixel Ring initializing...");
setup_neopixel_ring();
Serial.println("Wifi initializing...");
setup_wifi();
Serial.println("HTTP server initializing...");
setup_http_server();
server.begin();
Serial.println("HTTP server started.");
if (!MDNS.begin(HOSTNAME)) {
Serial.println("Error setting up MDNS responder!");
}
Serial.print("mDNS responder started as "); Serial.print(HOSTNAME); Serial.println(".local");
Serial.println("ready!");
}
void loop() {
unsigned long now = millis();
server.handleClient();
neopixel_ring.service();
if(now - last_wifi_check_time > WIFI_TIMEOUT) {
Serial.print("Checking WiFi... ");
if(WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost. Reconnecting...");
setup_wifi();
} else {
Serial.println("OK");
}
last_wifi_check_time = now;
}
if(cycle_mode_on && (now - cycle_last_change_time > 10000)) {
uint8_t next_mode = (neopixel_ring.getMode() + 1) % neopixel_ring.getModeCount();
neopixel_ring.setMode(next_mode);
Serial.print("mode is "); Serial.println(neopixel_ring.getModeName(neopixel_ring.getMode()));
cycle_last_change_time = now;
}
}