-
Notifications
You must be signed in to change notification settings - Fork 4
/
ANTCLK-alpha.ino
523 lines (477 loc) · 14.2 KB
/
ANTCLK-alpha.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include <Arduino.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SH1106Wire.h"
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <DNSServer.h>
#include <ESPUI.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>
//Settings
#define SLOW_BOOT 0
#define HOSTNAME "ANTCLKWIFISETUI"
#define FORCE_USE_HOTSPOT 0
#define PIN 0 //GPIO0接口
#define NUMPIXELS 4 //彩灯个数
#define BTN_Pin D7
#define SWT_Pin D0
#define NTD1 294
#define NTD2 330
#define NTD3 350
int tune1[]=
{
NTD1,NTD3
};
int tune2[]=
{
NTD3,NTD1
};
float durt[]=
{
0.25,0.25,0.25
};
int songLength=2;
int tonepin=15;
Adafruit_NeoPixel led(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 4, 1);
DNSServer dnsServer;
#if defined(ESP32)
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
WiFiClient client;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com",60*60*8, 30*60*1000);
String weekday[8] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",""};
//const char* ssid = "ESPUI";
//const char* password = "espui";
//const char* hostname = "espui";
uint16_t button1;
uint16_t switchOne;
uint16_t switchTwo;
uint16_t status;
uint16_t r_slid;
uint16_t g_slid;
uint16_t b_slid;
uint16_t l_slid;
uint16_t text_time;
uint16_t deftemp;
uint16_t defhumi;
//UI handles
uint16_t wifi_ssid_text, wifi_pass_text;
int wifiIsok = 0;
SH1106Wire display(0x3c, SDA, SCL); // ADDRESS, SDA, SCL
int bellFlag = 0;//clock on/off
int led_r=0;
int led_g=0;
int led_b=0;
int led_l=0; //lightness ws2812
int led_s=0; //light on/off
String settime;
int clockbellflag = 0;
void Bell(int tt)
{
int x;
if(tt)
{
for(x=0;x<songLength;x++)
{
tone(tonepin,tune1[x]);
delay(400*durt[x]);
delay(100*durt[x]);
noTone(tonepin);
}
}else{
for(x=0;x<songLength;x++)
{
tone(tonepin,tune2[x]);
delay(400*durt[x]);
delay(100*durt[x]);
noTone(tonepin);
}
}
}
void readStringFromEEPROM(String& buf, int baseaddress, int size) {
buf.reserve(size);
for (int i = baseaddress; i < baseaddress+size; i++) {
char c = EEPROM.read(i);
buf += c;
if(!c) break;
}
}
void connectWifi() {
int connect_timeout;
#if defined(ESP32)
WiFi.setHostname(HOSTNAME);
#else
WiFi.hostname(HOSTNAME);
#endif
Serial.println("Begin wifi...");
//Load credentials from EEPROM
if(!(FORCE_USE_HOTSPOT)) {
yield();
EEPROM.begin(100);
String stored_ssid, stored_pass;
readStringFromEEPROM(stored_ssid, 0, 32);
readStringFromEEPROM(stored_pass, 32, 96);
EEPROM.end();
//Try to connect with stored credentials, fire up an access point if they don't work.
#if defined(ESP32)
WiFi.begin(stored_ssid.c_str(), stored_pass.c_str());
#else
WiFi.begin(stored_ssid, stored_pass);
#endif
connect_timeout = 28; //7 seconds
while (WiFi.status() != WL_CONNECTED && connect_timeout > 0) {
delay(250);
Serial.print(".");
connect_timeout--;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
Serial.println("Wifi started");
wifiIsok = 1;
if (!MDNS.begin(HOSTNAME)) {
Serial.println("Error setting up MDNS responder!");
}
timeClient.begin();
timeClient.setTimeOffset(28800);
} else {
Serial.println("\nCreating access point...");
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));
WiFi.softAP(HOSTNAME);
connect_timeout = 20;
do {
delay(250);
Serial.print(",");
connect_timeout--;
} while(connect_timeout);
}
}
void enterWifiDetailsCallback(Control *sender, int type) {
if(type == B_UP) {
Serial.println("Saving credentials to EPROM...");
Serial.println(ESPUI.getControl(wifi_ssid_text)->value);
Serial.println(ESPUI.getControl(wifi_pass_text)->value);
unsigned int i;
EEPROM.begin(100);
for(i = 0; i < ESPUI.getControl(wifi_ssid_text)->value.length(); i++) {
EEPROM.write(i, ESPUI.getControl(wifi_ssid_text)->value.charAt(i));
if(i==30) break; //Even though we provided a max length, user input should never be trusted
}
EEPROM.write(i, '\0');
for(i = 0; i < ESPUI.getControl(wifi_pass_text)->value.length(); i++) {
EEPROM.write(i + 32, ESPUI.getControl(wifi_pass_text)->value.charAt(i));
if(i==94) break; //Even though we provided a max length, user input should never be trusted
}
EEPROM.write(i + 32, '\0');
EEPROM.end();
wifiIsok = 2;
}
}
void getUIdata()
{
/* uint16_t switchOne;
uint16_t switchTwo;
uint16_t status;
uint16_t r_slid;
uint16_t g_slid;
uint16_t b_slid;
uint16_t l_slid;
uint16_t text_time;*/
int i;
static int obellFlag = 0;//clock on/off
static int oled_r=0;
static int oled_g=0;
static int oled_b=0;
static int oled_l=0; //lightness ws2812
static int oled_s=0; //light on/off
Serial.print("switchOne: ");
Serial.println(ESPUI.getControl(switchOne)->value);
Serial.print("switchTwo: ");
Serial.println(ESPUI.getControl(switchTwo)->value);
bellFlag = ESPUI.getControl(switchOne)->value.toInt();
led_s = ESPUI.getControl(switchTwo)->value.toInt();
settime = ESPUI.getControl(text_time)->value;
Serial.println(settime);
if(oled_s != led_s)
{
digitalWrite(SWT_Pin, led_s);
Bell(led_s);
}
led_r = ESPUI.getControl(r_slid)->value.toInt();
led_g = ESPUI.getControl(g_slid)->value.toInt();
led_b = ESPUI.getControl(b_slid)->value.toInt();
led_l = ESPUI.getControl(l_slid)->value.toInt();
if(led_r!=oled_r||led_g!=oled_g||led_b!=oled_b)
{
for(i=0;i<NUMPIXELS;i++)
{
led.setPixelColor(i,led.Color(led_r, led_g, led_b)); //
led.show();
delay(100);
}
Serial.print("set led ");
Serial.print(led_r);
Serial.print(led_g);
Serial.println(led_b);
}
if(led_l!=oled_l)
{
led.setBrightness(led_l); //设置亮度 (0~255)
led.show();
Serial.print("set light ");
Serial.println(led_l);
}
oled_s = led_s;
obellFlag = bellFlag;
oled_r = led_r;
oled_g = led_g;
oled_b = led_b;
oled_l = led_l;
}
void oledDisplay() {
char s2Uno[128];
char clktime[128];
String str1;
int i;
int shour;
int smin;
int ssec;
int rtemp=22;
int rhumi=40;
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
sprintf(s2Uno,"temp:%02d*C",rtemp);
display.drawString(0, 8, s2Uno);
sprintf(s2Uno,"humi:%02d%%",rhumi);
display.drawString(72, 8, s2Uno);
timeClient.update();
shour = timeClient.getHours();
smin = timeClient.getMinutes();
ssec = timeClient.getSeconds();
sprintf(s2Uno,"%02d : %02d",shour,smin);
display.setFont(ArialMT_Plain_24);
display.drawString(28, 20, s2Uno);
display.setFont(ArialMT_Plain_10);
i = timeClient.getDay();
display.drawString(0, 48, weekday[i]);
display.drawString(64, 48,settime);
if(bellFlag)
{
sprintf(clktime,"%s",settime);
Serial.println(clktime);
Serial.println(atoi(clktime));
Serial.println(atoi(clktime+3));
if(shour==atoi(clktime)&&smin==atoi(clktime+3)&&ssec<30)
{
clockbellflag=1;
Serial.println("bell is trigger!");
}
display.drawString(94, 48, "ON ");
}
else
{
display.drawString(94, 48, "OFF");
}
display.display();
//sprintf(s2Uno,"[%s][%s][%s]",userData->city,userData->weather,userData->temp);
//Serial.println(s2Uno);
}
void textCallback(Control *sender, int type) {
//This callback is needed to handle the changed values, even though it doesn't do anything itself.
}
void randomString(char *buf, int len) {
for(auto i = 0; i < len-1; i++)
buf[i] = random(0, 26) + 'A';
buf[len-1] = '\0';
}
void numberCall(Control* sender, int type)
{
Serial.println(sender->value);
}
void textCall(Control* sender, int type)
{
Serial.print("Text: ID: ");
Serial.print(sender->id);
Serial.print(", Value: ");
Serial.println(sender->value);
}
void slider(Control* sender, int type)
{
Serial.print("Slider: ID: ");
Serial.print(sender->id);
Serial.print(", Value: ");
Serial.println(sender->value);
}
void switchExample(Control* sender, int value)
{
switch (value)
{
case S_ACTIVE:
Serial.print("Active:");
break;
case S_INACTIVE:
Serial.print("Inactive");
break;
}
Serial.print(" ");
Serial.println(sender->id);
}
void otherSwitchExample(Control* sender, int value)
{
switch (value)
{
case S_ACTIVE:
Serial.print("Active:");
break;
case S_INACTIVE:
Serial.print("Inactive");
break;
}
Serial.print(" ");
Serial.println(sender->id);
}
void SetUILink()
{
auto wifitab = ESPUI.addControl(Tab, "", "WiFi Credentials");
wifi_ssid_text = ESPUI.addControl(Text, "SSID", "", Alizarin, wifitab, textCallback);
//Note that adding a "Max" control to a text control sets the max length
ESPUI.addControl(Max, "", "32", None, wifi_ssid_text);
wifi_pass_text = ESPUI.addControl(Text, "Password", "", Alizarin, wifitab, textCallback);
ESPUI.addControl(Max, "", "64", None, wifi_pass_text);
ESPUI.addControl(Button, "Save", "Save", Peterriver, wifitab, enterWifiDetailsCallback);
//Finally, start up the UI.
//This should only be called once we are connected to WiFi.
ESPUI.begin(HOSTNAME);
}
void SetUIUser()
{
uint16_t tab1 = ESPUI.addControl(ControlType::Tab, "Red", "Red");
uint16_t tab2 = ESPUI.addControl(ControlType::Tab, "Green", "Green");
uint16_t tab3 = ESPUI.addControl(ControlType::Tab, "Blue", "Blue");
uint16_t tab4 = ESPUI.addControl(ControlType::Tab, "Light", "Light");
// shown above all tabs
deftemp = ESPUI.addControl(ControlType::Button, "Temp&Humi", "22*C", ControlColor::Turquoise,Control::noParent);
defhumi= ESPUI.addControl(ControlType::Button, "", " 40% ", ControlColor::Emerald, deftemp);
ESPUI.setPanelStyle(deftemp, "background: linear-gradient(90deg, rgba(131,58,180,1) 0%, rgba(253,29,29,1) 50%, rgba(252,176,69,1) 100%); border-bottom: #555;");
ESPUI.setElementStyle(deftemp, "border-radius: 2em; border: 3px solid black; width: 30%; background-color: #8df;");
ESPUI.setPanelStyle(defhumi, "background: linear-gradient(90deg, rgba(131,58,180,1) 0%, rgba(253,29,29,1) 50%, rgba(252,176,69,1) 100%); border-bottom: #555;");
ESPUI.setElementStyle(defhumi, "border-radius: 2em; border: 3px solid black; width: 30%; background-color: #8df;");
//ESPUI.setPanelWide(status, true);
r_slid = ESPUI.addControl(ControlType::Slider, "Red", "100", ControlColor::Alizarin, tab1, &slider);
g_slid = ESPUI.addControl(ControlType::Slider, "Green", "100", ControlColor::Emerald, tab2, &slider);
b_slid = ESPUI.addControl(ControlType::Slider, "Blue", "100", ControlColor::Peterriver, tab3, &slider);
l_slid = ESPUI.addControl(ControlType::Slider, "Light", "100", ControlColor::Sunflower, tab4, &slider);
ESPUI.addControl(Min, "", "0", None, r_slid);
ESPUI.addControl(Max, "", "255", None, r_slid);
ESPUI.addControl(Min, "", "0", None, g_slid);
ESPUI.addControl(Max, "", "255", None, g_slid);
ESPUI.addControl(Min, "", "0", None, b_slid);
ESPUI.addControl(Max, "", "255", None, b_slid);
ESPUI.addControl(Min, "", "0", None, l_slid);
ESPUI.addControl(Max, "", "255", None, l_slid);
ESPUI.sliderContinuous = true;
text_time = ESPUI.text("Set Clock Time", &textCall, ControlColor::Carrot, "06:00");
ESPUI.setInputType(text_time, "time");
ESPUI.addControl(ControlType::Max, "", "32", ControlColor::None, text_time,&textCall);
switchOne = ESPUI.addControl(ControlType::Switcher, "", "0", ControlColor::None,text_time, &switchExample);
switchTwo = ESPUI.addControl(ControlType::Switcher, "Light ON/OFF", "0", ControlColor::Dark, Control::noParent,&switchExample);
ESPUI.begin("ESPUI Control");
}
void setup(void)
{
pinMode(SWT_Pin, OUTPUT);
pinMode(tonepin,OUTPUT);
pinMode(BTN_Pin, INPUT);
Serial.begin(115200);
delay(2000);
display.init();
display.flipScreenVertically();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(28, 32, "Hello ANT_Clock");
display.display();
connectWifi();
digitalWrite(SWT_Pin, 1);
delay(500);
led.begin();
//led.clear();
led.setBrightness(100); //设置亮度 (0~255)
delay(1000);
if(wifiIsok)
{
SetUIUser();
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(8, 0, "now connected to");
display.drawString(8, 16, WiFi.SSID());
display.drawString(8, 32, WiFi.localIP().toString().c_str());
display.drawString(8, 48, "open this ip , enjoy it");
display.display();
}else{
SetUILink();
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(8, 0, "setup your wifi...");
display.drawString(8,16, "please find ssid:");
display.drawString(8,32, "ANTCLKWIFISETUI");
display.drawString(8,48, "and connect this ssid");
display.display();
//digitalWrite(SWT_Pin, 0);
}
}
void loop(void)
{
static int i=0;
dnsServer.processNextRequest();
Serial.print("wifiisok = ");
Serial.println(wifiIsok);
if(i==3)
{
i=0;
if(wifiIsok==2)
{
ESP.restart();
}else if(wifiIsok==1)
{
oledDisplay();
getUIdata();
ESPUI.updateButton(deftemp,"22*C");
ESPUI.updateButton(defhumi,"40%");
}
}
if(digitalRead(BTN_Pin)==0)//设置按钮按下
{
delay(200);
if(digitalRead(BTN_Pin)==0)
{
clockbellflag =0;
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(8, 0, "now connected to");
display.drawString(8, 16, WiFi.SSID());
display.drawString(8, 32, WiFi.localIP().toString().c_str());
display.drawString(8, 48, "open this ip , enjoy it");
display.display();
delay(5000);
}
}
if(clockbellflag)
{
Bell(0);
delay(200);
Bell(1);
}
delay(1000);
i++;
}