-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp8266-NeoPixel-Clock.ino
254 lines (200 loc) · 8.34 KB
/
esp8266-NeoPixel-Clock.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
/**The MIT License (MIT)
Copyright (c) 2017 by Radim Keseg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <ESP8266mDNS.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
// Helps with connecting to internet
#include <WiFiManager.h>
#include <Adafruit_NeoPixel.h>
// check settings.h for adapting to your needs
#include "settings.h"
#include "embHTML.h"
#include "ITimer.h"
#include "Clock.h"
#include "CuckooRainbowCycle.h"
#include "CuckooAlarm.h"
// HOSTNAME for OTA update
#define HOSTNAME "WSC-ESP8266-"
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdater;
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, NEOPIXEL_DATA_IN_PIN, NEO_GRB + NEO_KHZ800);
CustomSettings cs;
Clock clock(&strip, 30);
CuckooRainbowCycle cuckoo(&strip);
CuckooAlarm buzzer(&strip);
/*prototypes*/
void updateData();
/* webserver handlers */
void handle_root()
{
String content = FPSTR(PAGE_INDEX);
content.replace("{timeoffset}", String(cs.settings.UTC_OFFSET).c_str() );
if (cs.settings.DST) content.replace("{dst}", "checked='checked'");
else content.replace("{dst}", "");
content.replace("{brightness}", String(cs.settings.brightness).c_str());
content.replace("{brightness_night}", String(cs.settings.brightness_night).c_str());
content.replace("{val-hand-hour}", cs.settings.color_hand_hour);
content.replace("{val-hand-mins}", cs.settings.color_hand_mins);
content.replace("{val-hand-secs}", cs.settings.color_hand_secs);
content.replace("{val-segm-hour}", cs.settings.color_segm_hour);
content.replace("{val-dots-hour}", cs.settings.color_dots_hour);
content.replace("{val-dots-qrtr}", cs.settings.color_dots_qrtr);
if (cs.settings.ALARM_SWITCH) content.replace("{alarm}", "checked='checked'");
else content.replace("{alarm}", "");
content.replace("{alarmHour}", String(cs.settings.alarmHour).c_str());
content.replace("{alarmMins}", String(cs.settings.alarmMins).c_str());
server.send(200, "text/html", content);
}
static bool forceUpdateData = false;
void handle_store_settings(){
if(server.arg("_dst")==NULL && server.arg("_timeoffset")==NULL ){
Serial.println("setting page refreshed only, no params");
}else{
Serial.println("settings changed");
cs.settings.UTC_OFFSET = atof(server.arg("_timeoffset").c_str());
cs.settings.DST = server.arg("_dst").length()>0;
cs.settings.brightness = atoi(server.arg("_brightness").c_str());
cs.settings.brightness_night = atoi(server.arg("_brightness_night").c_str());
strncpy(cs.settings.color_hand_hour, server.arg("_input-hand-hour").c_str(), 8);
strncpy(cs.settings.color_hand_mins, server.arg("_input-hand-mins").c_str(), 8);
strncpy(cs.settings.color_hand_secs, server.arg("_input-hand-secs").c_str(), 8);
strncpy(cs.settings.color_segm_hour, server.arg("_input-segm-hour").c_str(), 8);
strncpy(cs.settings.color_dots_hour, server.arg("_input-dots-hour").c_str(), 8);
strncpy(cs.settings.color_dots_qrtr, server.arg("_input-dots-qrtr").c_str(), 8);
cs.settings.ALARM_SWITCH = server.arg("_alarm").length()>0;
cs.settings.alarmHour = atoi(server.arg("_alarmHour").c_str());
cs.settings.alarmMins = atoi(server.arg("_alarmMins").c_str());
cs.print();
cs.write();
updateData();
forceUpdateData = true;
}
clock.SetTimeOffset(cs.settings.UTC_OFFSET+cs.settings.DST);
server.send(200, "text/html", "OK");
}
/**/
// Called if WiFi has not been configured yet
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Wifi Manager");
Serial.println("Please connect to AP");
Serial.println(myWiFiManager->getConfigPortalSSID());
Serial.println("To setup Wifi Configuration");
}
void setup() {
//pinMode(NEOPIXEL_DATA_IN_PIN, OUTPUT);
Serial.begin(9600);
// Uncomment for testing wifi manager
//wifiManager.resetSettings();
wifiManager.setAPCallback(configModeCallback);
//or use this for auto generated name ESP + ChipID
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(180);
String hostname = String("Neopixel-Cuckoo") + String("-") + String(ESP.getChipId(), HEX);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if(!wifiManager.autoConnect( hostname.c_str() ) ){
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//Manual Wifi
//WiFi.begin(WIFI_SSID, WIFI_PWD);
WiFi.hostname(hostname);
cs.init();
cs.read();
cs.print();
clock.SetTimeOffset(cs.settings.UTC_OFFSET+cs.settings.DST);
MDNS.begin( (hostname+"-webupdate").c_str() );
httpUpdater.setup(&server, "/firmware", update_username, update_password );
//user setting handling
server.on("/", handle_root);
server.on("/offset", handle_store_settings);
server.begin();
Serial.println("HTTP server started");
MDNS.addService("http", "tcp", 80);
strip.begin();
strip.clear();
strip.show();
updateData();
}
long lastDrew = 0;
long lastUpdate = 0;
long stamp = 0;
boolean clear = true;
void loop() {
// Handle web server
server.handleClient();
// Handle OTA update requests
// ArduinoOTA.handle();
clear = true;
//effect by alarm
if(cs.settings.ALARM_SWITCH)
if(clock.getHourInt() == cs.settings.alarmHour && clock.getMinsInt() == cs.settings.alarmMins){
buzzer.Show();
clear = false;
}
// cuckoo effect by the full hour
if(clock.getMinsInt() == 0 && clock.getSecsInt()<10){
cuckoo.Show();
clear = false;
}
// show clock
stamp = millis();
if (stamp - lastDrew > 500 || stamp < lastDrew || !clear) {
//adjust brightness
if(clock.getHourInt()>=22 || clock.getHourInt()<6) strip.setBrightness(cs.settings.brightness_night);
else strip.setBrightness(cs.settings.brightness);
//show clock hands
clock.Show(clear, clear); delay(10);
lastDrew = stamp;
}
if(stamp - lastUpdate > UPDATE_INTERVAL_SECS * 1000 || stamp < lastUpdate || forceUpdateData){
updateData();
lastUpdate = stamp;
}
}
void updateData(){
clock.SetTimeOffset(cs.settings.UTC_OFFSET+cs.settings.DST);
clock.SetUp(ITimer::hex2rgb(
cs.settings.color_hand_hour), ITimer::hex2rgb(cs.settings.color_hand_mins), ITimer::hex2rgb(cs.settings.color_hand_secs), ITimer::hex2rgb(cs.settings.color_segm_hour),
ITimer::hex2rgb(cs.settings.color_dots_hour), ITimer::hex2rgb(cs.settings.color_dots_qrtr)
);
if(forceUpdateData) forceUpdateData=false;
}