44#include < ESP8266WebServer.h>
55#include < ESP8266WiFi.h>
66#include < ESP8266mDNS.h>
7- // #include <RemoteDebug.h>
7+ #define MAX_TIME_INACTIVE -1 // Disable timeout feature
8+ #include < RemoteDebug.h>
89#include < WiFiUdp.h>
910// #define FASTLED_INTERRUPT_RETRY_COUNT 1
1011// #define FASTLED_ALLOW_INTERRUPTS 0
1112#include < FastLED.h>
13+
14+ #include < TimeLib.h>
15+ #include < NTPClientLib.h>
16+
1217#include " secrets.h"
1318
1419#define LED_PIN 2
@@ -24,19 +29,21 @@ const char *password = PASSWORD;
2429CRGBPalette16 currentPalette;
2530TBlendType currentBlending;
2631
27- byte red;
28- byte green;
29- byte blue;
30- byte brightness;
31- bool rainbow = false ;
32- bool epilepsy = false ;
33- bool breath = false ;
34- bool toUpdate = true ;
35-
3632#include " html.html" // htmlTemplate
3733
3834ESP8266WebServer server (80 );
39- // RemoteDebug Debug;
35+ RemoteDebug Debug;
36+
37+ struct configuration {
38+ uint8_t brightness;
39+ uint8_t red, green, blue;
40+ bool rainbow, epilepsy, breath;
41+ };
42+
43+ bool toUpdate = true ;
44+
45+ struct configuration savedConf;
46+ struct configuration loopConf;
4047
4148void redirect303 (const String &url) {
4249 server.sendHeader (" Location" , url);
@@ -54,49 +61,45 @@ void handleIndexGet() {
5461 memset (bufferHtml, 0 , sizeof (htmlTemplate) + 42 );
5562 memset (bufferCss, 0 , sizeof (htmlTemplateCSS));
5663 strcpy_P (bufferCss, htmlTemplateCSS);
57- sprintf_P (bufferHtml, htmlTemplate, bufferCss, red, green, blue, brightness);
64+ sprintf_P (bufferHtml, htmlTemplate, bufferCss, savedConf. red , savedConf. green , savedConf. blue , savedConf. brightness );
5865 server.send (200 , " text/html" , bufferHtml);
5966 free (bufferHtml);
6067 free (bufferCss);
6168}
6269
6370void handleIndexPost () {
6471 if (server.arg (" red" ) != " same" ) {
65- red = server.arg (" red" ).toInt ();
66- EEPROM.write (0 , red);
72+ savedConf.red = server.arg (" red" ).toInt ();
6773 }
6874 if (server.arg (" green" ) != " same" ) {
69- green = server.arg (" green" ).toInt ();
70- EEPROM.write (1 , green);
75+ savedConf.green = server.arg (" green" ).toInt ();
7176 }
7277 if (server.arg (" blue" ) != " same" ) {
73- blue = server.arg (" blue" ).toInt ();
74- EEPROM.write (2 , blue);
78+ savedConf.blue = server.arg (" blue" ).toInt ();
7579 }
7680 if (server.arg (" brightness" ) != " same" ) {
77- brightness = server.arg (" brightness" ).toInt ();
78- EEPROM.write (3 , brightness);
81+ savedConf.brightness = server.arg (" brightness" ).toInt ();
7982 }
8083
8184 if (server.arg (" rainbow" ) != " " ) {
82- rainbow = true ;
83- epilepsy = false ;
84- breath = false ;
85+ savedConf. rainbow = true ;
86+ savedConf. epilepsy = false ;
87+ savedConf. breath = false ;
8588 } else if (server.arg (" epilepsy" ) != " " ) {
86- epilepsy = true ;
87- rainbow = false ;
88- breath = false ;
89+ savedConf. epilepsy = true ;
90+ savedConf. rainbow = false ;
91+ savedConf. breath = false ;
8992 } else if (server.arg (" breath" ) != " " ) {
90- breath = true ;
91- rainbow = false ;
92- epilepsy = false ;
93+ savedConf. breath = true ;
94+ savedConf. rainbow = false ;
95+ savedConf. epilepsy = false ;
9396 } else {
94- epilepsy = false ;
95- rainbow = false ;
96- breath = false ;
97+ savedConf. epilepsy = false ;
98+ savedConf. rainbow = false ;
99+ savedConf. breath = false ;
97100 }
98- EEPROM. write ( 4 , rainbow);
99- EEPROM.write ( 5 , breath );
101+
102+ EEPROM.put ( 0 , savedConf );
100103
101104 EEPROM.commit ();
102105 toUpdate = true ;
@@ -107,7 +110,7 @@ void handleIndexPost() {
107110void setup () {
108111 Serial.begin (115200 );
109112 Serial.println (" Booting" );
110- EEPROM.begin (6 );
113+ EEPROM.begin (sizeof ( struct configuration ) );
111114 WiFi.mode (WIFI_STA);
112115 WiFi.begin (ssid, password);
113116 while (WiFi.waitForConnectResult () != WL_CONNECTED) {
@@ -116,12 +119,7 @@ void setup() {
116119 ESP.restart ();
117120 }
118121
119- red = EEPROM.read (0 );
120- green = EEPROM.read (1 );
121- blue = EEPROM.read (2 );
122- brightness = EEPROM.read (3 ); // Load from EEPROM here
123- rainbow = EEPROM.read (4 );
124- breath = EEPROM.read (5 );
122+ EEPROM.get (0 , savedConf);
125123
126124 ArduinoOTA.setHostname (" Leds" );
127125
@@ -157,64 +155,103 @@ void setup() {
157155 Serial.print (" IP address: " );
158156 Serial.println (WiFi.localIP ());
159157
160- // Debug.begin("Telnet_HostName");
158+ Debug.begin (" Telnet_HostName" );
159+ NTP.begin (" europe.pool.ntp.org" , 1 , true , 0 );
161160
162161 server.onNotFound (onMissing);
163162 server.on (" /" , HTTP_GET, handleIndexGet);
164163 server.on (" /" , HTTP_POST, handleIndexPost);
165164 server.begin ();
166165
167166 FastLED.addLeds <LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
168- FastLED.setBrightness (brightness);
167+ FastLED.setBrightness (savedConf. brightness );
169168
170169 currentPalette = RainbowColors_p;
171170 currentBlending = LINEARBLEND;
172171}
173172
174173void FillLEDsFromPaletteColors (uint8_t colorIndex) {
175174 for (int i = 0 ; i < NUM_LEDS; i++) {
176- leds[i] = ColorFromPalette (currentPalette, colorIndex, brightness, currentBlending);
175+ leds[i] = ColorFromPalette (currentPalette, colorIndex, loopConf. brightness , currentBlending);
177176 colorIndex += 3 ;
178177 }
179178}
180179
181- unsigned long previousMillis = 0 ;
180+ void rgb (struct configuration *conf, int red, int green, int blue) {
181+ conf->red = red;
182+ conf->green = green;
183+ conf->blue = blue;
184+ }
185+
186+ bool toUpdateNextLoop = false ;
187+
188+ // long lastMillis = 0;
189+ // long loops = 0;
182190
183191void loop () {
192+ // long currentMillis = millis();
193+ // loops++;
194+ // if (currentMillis - lastMillis > 1000){
195+ // Debug.print("Loops last second: ");
196+ // Debug.println(loops);
197+
198+ // lastMillis = currentMillis;
199+ // loops = 0;
200+ // }
184201 static uint8_t startIndex = 0 ;
185202 startIndex += 1 ; /* motion speed */
186203
187- /* // Hardcoded mode
188- toUpdate = true;
189- breath = false;
190- red = 255;
191- green = 0;
192- blue = 255;
193- rainbow = false;
194- epilepsy = false;
195- brightness = 100; */
196-
197- if (rainbow) {
204+ loopConf = savedConf;
205+
206+ if (toUpdateNextLoop) {
207+ toUpdate = true ;
208+ toUpdateNextLoop = false ;
209+ }
210+
211+ time_t t = now ();
212+ if (hour (t) == 0 && minute (t) == 0 && second (t) == 0 ) { // midnight, set as full red, warning
213+ rgb (&loopConf, 255 , 0 , 0 );
214+ loopConf.brightness = 255 ;
215+ toUpdate = true ;
216+ } else if (hour (t) == 0 && minute (t) == 0 && second (t) == 2 ) { // two seconds later, dim the brightness. Set sensible colours
217+ savedConf.brightness = 50 ;
218+ toUpdate = true ;
219+ } else if (hour (t) == 7 && minute (t) == 0 && second (t) == 0 ) { // 7 am, lights off
220+ savedConf.brightness = 0 ;
221+ } else if (minute (t) == 0 && second (t) <= 5 ) {
222+ loopConf.rainbow = true ;
223+ loopConf.breath = false ;
224+ loopConf.epilepsy = false ;
225+ loopConf.brightness = 255 ;
226+ toUpdateNextLoop = true ;
227+ }
228+
229+ if (loopConf.rainbow ) {
198230 FillLEDsFromPaletteColors (startIndex);
199- } if (epilepsy) {
231+ } else if (loopConf. epilepsy ) {
200232 FastLED.delay (20 );
201233 FastLED.setBrightness (0 );
202234 FastLED.show ();
203235 FastLED.delay (20 );
204236 startIndex += 1 ;
205- } if (breath) {
237+ } else if (loopConf. breath ) {
206238 double breathValue, modifiedBrightness;
207239 breathValue = (exp (sin (millis ()/2000.0 *PI))-0.36787944 )*108.0 ;
208- modifiedBrightness = map (breathValue, 0 , 255 , 0 , brightness);
240+ modifiedBrightness = map (breathValue, 0 , 255 , 0 , loopConf. brightness );
209241 FastLED.setBrightness (modifiedBrightness);
210- } if (toUpdate) {
211- fill_solid (leds, NUM_LEDS, CRGB (red, green, blue));
212- toUpdate = false ;
213242 }
214- if (!breath) {
215- FastLED.setBrightness (brightness);
243+
244+ if (toUpdate && !loopConf.rainbow ) {
245+ fill_solid (leds, NUM_LEDS, CRGB (loopConf.red , loopConf.green , loopConf.blue ));
216246 }
247+
248+ if (!loopConf.breath ) {
249+ FastLED.setBrightness (loopConf.brightness );
250+ }
251+
252+ toUpdate = false ; // DO NOT PUT AFTER HTTP HANDLING
217253 FastLED.show ();
218254 ArduinoOTA.handle ();
219255 server.handleClient ();
256+ Debug.handle ();
220257}
0 commit comments