forked from jasoncoon/esp8266-fastled-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266-fastled-webserver.ino
237 lines (187 loc) · 6.87 KB
/
esp8266-fastled-webserver.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
/*
ESP8266 FastLED WebServer: https://github.com/jasoncoon/esp8266-fastled-webserver
Copyright (C) 2015-2018 Jason Coon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#define FASTLED_ALLOW_INTERRUPTS 1
//#define INTERRUPT_THRESHOLD 1
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
extern "C" {
#include "user_interface.h"
}
#include <FS.h>
#include <EEPROM.h>
#include "LED_Functions.h"
#include "Field.h"
#include "Fields.h"
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include "Webserver_Functions.h"
// ===== Settings =====
// uncomment to enable Access Point mode
// #define AP_MODE
// name of the device in the local network
const char * mDNS_Name = "lights";
#include "Secrets.h" // this file is intentionally not included in the sketch, so nobody accidentally commits their secret information.
// create a Secrets.h file with the following:
// AP mode password
// const char WiFiAPPSK[] = "your-password";
// Wi-Fi network to connect to (if not in AP mode)
// char* ssid = "your-ssid";
// char* password = "your-password";
void setup() {
WiFi.setSleepMode(WIFI_NONE_SLEEP);
LEDSetup();
Serial.begin(115200);
delay(100);
Serial.setDebugOutput(true);
EEPROM.begin(512);
loadSettings();
Serial.println();
Serial.print(F("Heap: ")); Serial.println(system_get_free_heap_size());
Serial.print(F("Boot Vers: ")); Serial.println(system_get_boot_version());
Serial.print(F("CPU: ")); Serial.println(system_get_cpu_freq());
Serial.print(F("SDK: ")); Serial.println(system_get_sdk_version());
Serial.print(F("Chip ID: ")); Serial.println(system_get_chip_id());
Serial.print(F("Flash ID: ")); Serial.println(spi_flash_get_id());
Serial.print(F("Flash Size: ")); Serial.println(ESP.getFlashChipRealSize());
Serial.print(F("Vcc: ")); Serial.println(ESP.getVcc());
Serial.println();
SPIFFS.begin();
{
Serial.println("SPIFFS contents:");
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.printf("FS File: %s, size: %s\n", fileName.c_str(), String(fileSize).c_str());
}
Serial.printf("\n");
}
//disabled due to https://github.com/jasoncoon/esp8266-fastled-webserver/issues/62
//initializeWiFi();
#ifdef AP_MODE
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ESP8266 Thing " + macID;
int apNameSize = AP_NameString.length() + 1;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i = 0; i < AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
Serial.printf("Connect to Wi-Fi access point: %s\n", AP_NameChar);
Serial.println("and open http://192.168.4.1 in your browser");
#else
WiFi.mode(WIFI_STA);
Serial.printf("Connecting to %s\n", ssid);
if (String(WiFi.SSID()) != String(ssid)) {
WiFi.begin(ssid, password);
}
#endif // AP_MODE
// webSocketsServer.begin();
// webSocketsServer.onEvent(webSocketEvent);
// Serial.println("Web socket server started");
if (!MDNS.begin(mDNS_Name)) { // Start the mDNS responder for esp8266.local
Serial.println("Error setting up MDNS responder!");
}
autoPlayTimeout = millis() + (autoplayDuration * 1000);
setupWebServer();
}
void loop() {
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy(random(65535));
// dnsServer.processNextRequest();
// webSocketsServer.loop();
webServer.handleClient();
//yield();
// handleIrInput();
if (power == 0) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
//yield();
// FastLED.delay(15);
return;
}
static bool hasConnected = false;
EVERY_N_SECONDS(1) {
if (WiFi.status() != WL_CONNECTED) {
// Serial.printf("Connecting to %s\n", ssid);
hasConnected = false;
}
else if (!hasConnected) {
WiFi.hostname(mDNS_Name);
hasConnected = true;
Serial.print("Connected! Open http://");
Serial.print(mDNS_Name);
Serial.print(" or http://");
Serial.print(WiFi.localIP());
Serial.println(" in your browser");
}
}
// EVERY_N_SECONDS(10) {
// Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size());
// }
// change to a new cpt-city gradient palette
EVERY_N_SECONDS(secondsPerPalette) {
gCurrentPaletteNumber = addmod8(gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[gCurrentPaletteNumber];
}
EVERY_N_MILLISECONDS(40) {
// slowly blend the current palette to the next
nblendPaletteTowardPalette(gCurrentPalette, gTargetPalette, 8);
gHue++; // slowly cycle the "base color" through the rainbow
}
if (autoplay && (millis() > autoPlayTimeout)) {
adjustPattern(true);
autoPlayTimeout = millis() + (autoplayDuration * 1000);
}
// Call the current pattern function once, updating the 'leds' array
patterns[currentPatternIndex].pattern();
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
void loadSettings()
{
brightness = EEPROM.read(0);
currentPatternIndex = EEPROM.read(1);
if (currentPatternIndex < 0)
currentPatternIndex = 0;
else if (currentPatternIndex >= patternCount)
currentPatternIndex = patternCount - 1;
byte r = EEPROM.read(2);
byte g = EEPROM.read(3);
byte b = EEPROM.read(4);
if (r == 0 && g == 0 && b == 0)
{
}
else
{
solidColor = CRGB(r, g, b);
}
power = EEPROM.read(5);
autoplay = EEPROM.read(6);
autoplayDuration = EEPROM.read(7);
currentPaletteIndex = EEPROM.read(8);
if (currentPaletteIndex < 0)
currentPaletteIndex = 0;
else if (currentPaletteIndex >= paletteCount)
currentPaletteIndex = paletteCount - 1;
}