-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWifiClock_USB.ino
306 lines (252 loc) · 8.73 KB
/
WifiClock_USB.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
/*
Elliott Bradley
Last Edited 9/10/23
Used code from:
https://www.instructables.com/Desktop-Ring-Clock/
https://randomnerdtutorials.com/esp32-async-web-server-espasyncwebserver-library/
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <FastLED.h>
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define LED_PIN 16
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
CRGB HourColor = CRGB(145, 0, 0); //RED //FastLED config
CRGB MinuteColor = CRGB(0, 118, 138); //GREEN
CRGB SecondColor = CRGB(144, 0, 112); //BLUE
const CRGB lightcolor(8, 5, 1);
uint8_t BRIGHTNESS = 255;
CRGBPalette16 currentPalette;
const int wifiTimeout = 8000; //8 seconds
bool wifiCast;
String defaultSSID = "mnrbradley"; // WiFi name
String defaultPassword = "Kr1st1n@"; // WiFi password
const char* ssidAP = "Ring_Clock"; // Soft AP
const char* passwordAP = "123456789";
// IP Address details
IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1); //soft AP data
IPAddress subnet(255, 255, 255, 0);
// Set web server port number to 80
AsyncWebServer server(80);
// Variable to store the HTTP request
String header;
String tempStr;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "north-america.pool.ntp.org", -18000); // -5 hours offset, Chicago Time
const int modeCount = 4;
int lightMode = 1;
int hour = 10;
int minute = 30;
int second = 0;
/*
int touchButton;
int touchButtonLast;
int touchThreshold = 20;
*/
byte temp_second = 0;
TaskHandle_t Main;
TaskHandle_t Server;
TaskHandle_t AP;
void lightSpin(int8_t blinkCount, CRGB color, int speed = 20); //need declaration for default values
void showDigit(String digits, int counter = 1500, CRGB color = CRGB::White);
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println(F("An Error has occurred while mounting SPIFFS"));
return;
}
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
tempStr = readFile(SPIFFS, "/inputBrightness.txt"); //read brightness stored in file
uint8_t readBrightness = tempStr.toInt(); //convert to int
FastLED.setBrightness(readBrightness);
Serial.print(F("FastLed Setup done on core "));
Serial.println(xPortGetCoreID());
vTaskDelay(100 / portTICK_PERIOD_MS);
WiFi.mode(WIFI_STA);
//WiFi.begin(ssid, password);
WiFi.begin(readFile(SPIFFS, "/wifiSSID.txt").c_str(), readFile(SPIFFS, "/wifiPSK.txt").c_str());
int wifiCount = millis();
int wifiPrev = wifiCount;
while (WiFi.status() != WL_CONNECTED) {
Serial.print("|");
//softtwinkles();
pride();
FastLED.show();
wifiCount = millis();
if (wifiCount - wifiPrev >= wifiTimeout) {
Serial.println(F("|"));
Serial.println(F("WiFi not available, switching to default SSID & password"));
WiFi.begin(defaultSSID, defaultPassword);
wifiCount = millis();
wifiPrev = wifiCount;
lightSpin(1, CRGB::Yellow, 10);
break;
//if successful, save default ssid to spiffs to speed up connection?
}
}
while (WiFi.status() != WL_CONNECTED) {
Serial.print(F("."));
//softtwinkles();
pride();
FastLED.show();
wifiCount = millis();
if (wifiCount - wifiPrev >= wifiTimeout) { //if timeout
wifiCast = true;
timeFromFile();
second = 0;
Serial.println(F("."));
Serial.println(F("WiFi not available, switching to Access Point mode"));
Serial.print(F("wifiCast "));
Serial.println(wifiCast);
WiFi.mode(WIFI_AP);
lightSpin(2, CRGB::Red, 20); //Blink leds red to indicate not connected (kinda)
goto tasks;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("!"));
Serial.println(F("WiFi connected."));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
Serial.print(F("connected with core "));
Serial.println(xPortGetCoreID());
Serial.print(F("time taken "));
Serial.println(wifiCount - wifiPrev);
//show last 3 digits of IP
showDigit(ipShort((WiFi.localIP().toString()), 3)); //I did it this way so in the settings there can be an option to show the entire IP, just in case
}
tasks:
//serverSetup();
if (WiFi.status() != WL_CONNECTED) //if not connected, host offline server
{
xTaskCreatePinnedToCore(APcode, "AP", 4096, NULL, 1, &AP, 0); //runs the async server on core 1
}
xTaskCreatePinnedToCore(serverSetup, "Server", 4096, NULL, 0, &Server, 0); //execute serverSetup on core 0 with priority 0 MAYBE TRY RUNNING ON CORE 1
//xTaskCreatePinnedToCore(Maincode, "Main", 50000, NULL, 1, &Main, 1); //MOVED TO END OF "Server" FUNCTION to not crash(using wifi and fastLED on the same core can crash)
}
void loop() {
delay(1); //to hopefully reduce processing times if the loop is considered
}
void Maincode(void* parameter) {
for (;;) //loop forever
{
if (wifiCast == false) {
if (temp_second != timeClient.getSeconds()) { //update the temp_second only when the time client second changes
temp_second = timeClient.getSeconds();
timeClient.update(); //this might be redundant, but im not sure if the timeClient.get commands update the client or not (pretty sure it does)
Serial.print(F("24 hr time "));
Serial.println(timeClient.getFormattedTime());
Serial.print(F("looped on "));
Serial.println(xPortGetCoreID());
hour = timeClient.getHours();
minute = timeClient.getMinutes();
second = timeClient.getSeconds();
//EEPROM.write so it saves current internet time for if the esp reboots offline?
}
}
if (wifiCast == true) {
EVERY_N_SECONDS(1) { //this should replace my timer code to make it look pretty
second += 1;
Serial.print(F("Offline time "));
Serial.print(hour);
Serial.print(F(":"));
Serial.print(minute);
Serial.print(F("."));
Serial.println(second);
if (second >= 59) {
second = 0;
minute += 1;
}
if (minute >= 60) {
minute = 0;
hour += 1;
}
if (hour >= 24) {
hour = 0;
}
}
EVERY_N_SECONDS(60) {
String timeStore;
String modeStore;
timeStore = String(int(hour) + ":" + int(minute + 1)); //use String to prevent corruption
modeStore = String(lightMode);
writeFileString(SPIFFS, "/inputTime.txt", timeStore); //save current time to spiffs
writeFile(SPIFFS, "/lightMode.txt", modeStore.c_str());
//WORK ON CONVERTING SAVING SINGLE VALUES TO NVS INSTEAD OF SPIFFS
//https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/Prefs2Struct/Prefs2Struct.ino
}
}
//buttonRead(); //not implemented or used currently
switch (lightMode) {
case 1:
timeDisplay1(hour, minute, second);
break;
case 2:
timeDisplay2(hour, minute, second);
break;
case 3:
timeDisplay3(hour, minute, second);
break;
case 4:
timeDisplay4(hour, minute, second);
break;
/*
case 5:
timeDisplay5(hour, minute, second); //mode is broken
break;
*/
default:
Serial.print(F("timeDisplay Error!!!!!"));
lightMode = modeCount;
break;
}
ledOffset();
FastLED.show();
}
}
String ipShort(String ip, int8_t digitsFromEnd) {
String cutIP;
cutIP = ip.substring(ip.length() - digitsFromEnd);
Serial.println(cutIP);
return (cutIP);
}
/*
void buttonRead() {
int touch;
touch = touchRead(T0);
if (touch < touchThreshold) {
touchButton = 1;
} else {
touchButton = 0;
}
if (touchButton == 0 && touchButton != touchButtonLast) //senses falloff, if last cycle button was pressed and it's no longer pressed, increment
{
lightMode++;
if (lightMode > modeCount) {
lightMode = 1;
}
String modeStore = String(lightMode);
writeFile(SPIFFS, "/lightMode.txt", modeStore.c_str());
}
touchButtonLast = touchButton;
if (touchButton == 1) {
Serial.print(F("touch value "));
Serial.println(touch);
Serial.print(F("button value "));
Serial.println(touchButton);
Serial.print(F("light mode "));
Serial.println(lightMode + 1);
Serial.print(F("last button state "));
Serial.println(touchButtonLast);
}
}
*/