-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIoTLED.ino
381 lines (316 loc) · 11.3 KB
/
IoTLED.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
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
#include "string.h"
#include <FastLED.h>
#include <ArduinoJson.h>
FASTLED_USING_NAMESPACE
#define DATA_PIN 12
//#define CLK_PIN 4
#define LED_TYPE WS2813
#define COLOR_ORDER GRB
#define NUM_LEDS 300
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 10 // Default Brightness on ESP startup
#define FRAMES_PER_SECOND 300
// Global Effect Variables
uint8_t activeEffect;
uint8_t brightness;
struct effectData {
uint32_t color1;
} d_effect1;
uint8_t gHue = 0; // rotating "base color" for rainbow effect
// Network
const char* ssid = "*";
const char* password = "*";
IPAddress local_IP(192, 168, 178, 6);
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); // optional
IPAddress secondaryDNS(8, 8, 4, 4); // optional
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String effect1State = "off";
String effect2State = "off";
// Create AsyncWebServer and WebSocket object on port 80
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
//AsyncWebSocketClient * globalClient = NULL;
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.println("Websocket client connection received");
//globalClient = client;
} else if (type == WS_EVT_DISCONNECT) {
Serial.println("Client disconnected");
Serial.println("-----------------------");
//globalClient = NULL;
} else if (type == WS_EVT_DATA) {
Serial.print("Data received: ");
String msg = "";
for (int i = 0; i < len; i++) {
msg += (char)data[i];
}
Serial.println(msg);
if (msg.startsWith("0x")) {
String temp = msg.substring(2, 4);
uint8_t tempEffect;
int type = temp.toInt();
msg.remove(0, 4);
char *eptr;
switch (type) {
case 99: // change activeEvent
temp = msg.substring(0,2);
tempEffect = (uint8_t)atoi(temp.c_str());
if (tempEffect >= 1 && tempEffect <= 3)
activeEffect = tempEffect;
break;
case 98: // brightness
temp = msg.substring(0,3);
brightness = (uint8_t)atoi(temp.c_str());
break;
case 0: // stip off
activeEffect = 0;
break;
case 1: // effect1 data
activeEffect = 1;
temp = msg.substring(1, 7); //filter out #
d_effect1.color1 = (uint32_t)strtoul(temp.c_str(), &eptr, 16);
break;
case 2: // effect2 data
activeEffect = 2;
//temp = msg.substring(1, 7); //filter out #
//d_effect1.color1 = (uint32_t)strtoul(temp.c_str(), &eptr, 16);
break;
case 3: // effect3 data
activeEffect = 3;
//temp = msg.substring(1, 7); //filter out #
//d_effect1.color1 = (uint32_t)strtoul(temp.c_str(), &eptr, 16);
break;
default:
break;
}
}
//brightness = (uint8_t)atoi(msg.c_str());
//ws.textAll("response from server!");
//globalClient->text("response from server!");
}
}
void loadSettings() {
File settings = SPIFFS.open("/settings.json", "r");
if (!settings) {
Serial.println("ERR: Failed to open settings JSON!");
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
StaticJsonDocument<200> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, settings);
if (error) {
Serial.println("ERR: Failed to deserialize JSON file!");
}
activeEffect = doc["activeEffect"].as<uint8_t>();
brightness = doc["brightness"].as<uint8_t>();
char *eptr; // does nothing
String temp;
temp = doc["effect1Data"]["color1"].as<String>();
temp.remove(0, 1);
d_effect1.color1 = (uint32_t)strtoul(temp.c_str(), &eptr, 16); // wtf
//Serial.println(d_effect1.color1);
//Serial.print("Loaded brightness: ");
//Serial.println(brightness);
settings.close();
}
void saveSettings() {
SPIFFS.remove("/settings.json");
File settings = SPIFFS.open("/settings.json", "w");
if (!settings) {
Serial.println("ERR: Failed to open settings file for writing!");
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
StaticJsonDocument<200> doc;
doc["activeEffect"] = activeEffect;
doc["brightness"] = brightness;
String temp = String(d_effect1.color1, HEX);
doc["effect1Data"]["color1"] = "#" + temp;
Serial.println("Saved Color: #" + temp);
// Serialize JSON to file
if (serializeJson(doc, settings) == 0) {
Serial.println("ERR: Failed to write to settings JSON!");
}
// Close the file
settings.close();
}
/* // Replaces HTML placeholder with state values
String processor(const String& var) {
if (var == "EFFECT1STATE") {
return effect1State;
} else if (var == "EFFECT2STATE") {
return effect2State;
}
return "!!!PLACEHOLDER ERROR!!!"; // replaces not (yet) defined placeholder with this
} */
void setup() {
Serial.begin(115200);
// ----------------------------- SPIFFS -----------------------------
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
/*
saveSettings();
Serial.println("Settings saved");
*/
loadSettings();
Serial.println("Settings loaded");
// ----------------------------- LEDS -----------------------------
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
// ----------------------------- WIFI -----------------------------
// Configure WiFi for Static IP
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Wait 5 seconds for WiFi to connect or else reboot
uint8_t wifitimeout = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (++wifitimeout > 10) {
WiFi.disconnect(true);
ESP.restart();
}
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// ----------------------------- SERVER -----------------------------
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/index.html", "text/html");
});
// Route for .js
server.on("/IoTLED.js", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/IoTLED.js", "application/javascript");
});
// Route for settings JSON
server.on("/settings.json", HTTP_GET, [](AsyncWebServerRequest * request) {
saveSettings();
request->send(SPIFFS, "/settings.json", "application/json");
});
// Route for icons
server.on("/android-chrome-192x192.png", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/android-chrome-192x192.png", "image/png");
});
server.on("/android-chrome-512x512.png", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/android-chrome-512x512.png", "image/png");
});
server.on("/apple-touch-icon.png", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/apple-touch-icon.png", "image/png");
});
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/favicon.ico", "image/x-icon");
});
server.on("/favicon-16x16.png", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/favicon-16x16.png", "image/png");
});
server.on("/favicon-32x32.png", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/favicon-32x32.png", "image/png");
});
server.on("/mstile-150x150.png", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/mstile-150x150.png", "image/png");
});
server.on("/safari-pinned-tab.svg", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/safari-pinned-tab.svg", "image/svg");
});
server.on("/browserconfig.xml", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/browserconfig.xml", "application/xml");
});
server.on("/site.webmanifest", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/site.webmanifest", "text/webmanifest");
});
/* // Route to load style.css file
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/style.css", "text/css");
}); */
ws.onEvent(onWsEvent);
server.addHandler(&ws);
server.begin();
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[NUM_LEDS];
int cooldown;
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
if(cooldown>heat[i]) {
heat[i]=0;
} else {
heat[i]=heat[i]-cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if( random(255) < Sparking ) {
int y = random(7);
heat[y] = heat[y] + random(160,255);
//heat[y] = random(160,255);
}
// Step 4. Convert heat to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
delay(SpeedDelay);
}
void setPixelHeatColor (int Pixel, byte temperature) {
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature/255.0)*191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
// figure out which third of the spectrum we're in:
if( t192 > 0x80) { // hottest
setPixel(Pixel, 255, 255, heatramp);
} else if( t192 > 0x40 ) { // middle
setPixel(Pixel, 255, heatramp, 0);
} else { // coolest
setPixel(Pixel, heatramp, 0, 0);
}
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
}
void loop() {
switch (activeEffect) {
case 1:
fill_solid(leds, NUM_LEDS, d_effect1.color1);
break;
case 2:
fill_solid(leds, NUM_LEDS, CRGB::Blue);
break;
case 3:
Fire(55,120,15);
break;
case 0:
default:
fill_solid(leds, NUM_LEDS, CRGB::Black);
break;
}
FastLED.setBrightness(brightness);
FastLED.show();
FastLED.delay(1000 / FRAMES_PER_SECOND);
//EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
}