Skip to content

Commit 2c7bf2e

Browse files
working WIFI mode
now you can enter via webPage Wifi's SSID and password
1 parent 3615c2b commit 2c7bf2e

4 files changed

Lines changed: 84 additions & 29 deletions

File tree

Clock.ino

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
void setup() {
44
Serial.begin(9600);
55
EEPROM.begin(512);
6-
//EEPROM.write(50,0);EEPROM.commit();
7-
pinMode(BTN_PIN, INPUT_PULLUP);
86
strip.begin();
97
strip.show();
108
strip.setBrightness(brightness_pv);
11-
clockMode = CONNECTING;
9+
if (getWifiMode() == CREATE_NETWORK) {
10+
clockMode = NO_WIFI;
11+
} else {
12+
clockMode = INITIALIZING;
13+
}
1214

13-
server.on("/", handleMain);
14-
server.on("/strip", handleStrip);
15-
server.on("/timer", handleTimer);
16-
server.on("/alarm", handleAlarm);
17-
server.on("/alarmList", handleAlarmList);
18-
server.on("/delete", handleDelete);
19-
server.begin();
20-
//if(getWifiMode()!=CONNETC_TO_NETWORK)
21-
//putEEPROMData();
15+
pinMode(BTN_PIN, INPUT_PULLUP);
2216
}
2317

2418
void loop() {
@@ -67,6 +61,7 @@ void loop() {
6761

6862
if (!clockRinging) checkForAlarms();
6963

64+
7065
//CLOCK_MODE MANAGER
7166
switch (clockMode * (stripMode == MAN_ON || stripMode == AUTO_ON)) {
7267
case DARK:
@@ -79,11 +74,10 @@ void loop() {
7974
getRealTime();
8075
drawTime();
8176
break;
82-
case CONNECTING:
77+
case INITIALIZING:
8378
drawPointer(0, 1, strip.Color(255, 255, 0));
8479
strip.show();
8580
getEEPROMData();
86-
Serial.printf("ssid:%s, psw:%s", ssid, password);
8781
connectToWifi();
8882
fetchTime();
8983
clockMode = NORMAL;
@@ -95,8 +89,17 @@ void loop() {
9589
case RINGING:
9690
strip.show();
9791
break;
92+
case NO_WIFI:
93+
if (!APConfigured) {
94+
drawPointer(0, 1, strip.Color(255, 0, 0));
95+
strip.show();
96+
createWifiNetwork();
97+
APConfigured = 1;
98+
}
99+
break;
98100
}
99-
server.handleClient(); /*
101+
server.handleClient();
102+
Serial.println("loop"); /*
100103
for (byte i = 0; i < existingAlarms; i++) {
101104
Serial.printf("Alarm n. %d --- dayCode:%d%d%d%d%d%d%d,%d, AlreadyRinged:%d, %d:%d\n", i, alarms[i].weekDay[0], alarms[i].weekDay[1], alarms[i].weekDay[2], alarms[i].weekDay[3], alarms[i].weekDay[4], alarms[i].weekDay[5], alarms[i].weekDay[6], alarms[i].oneTime,alarms[i].alreadyRinged, alarms[i].hour, alarms[i].min);
102105
}*/

Connection.ino

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,64 @@
1+
void createWifiNetwork() {
2+
Serial.println("creating network");
3+
WiFi.softAP(ssid, password);
4+
IPAddress myIP = WiFi.softAPIP();
5+
Serial.print("AP IP address: ");
6+
Serial.println(myIP);
7+
server.on("/", handleAPPage);
8+
server.on("/WIFIData", handleWIFIData);
9+
server.begin();
10+
Serial.println("HTTP server started");
11+
}
12+
113
void connectToWifi() {
14+
byte attempt = 0;
215
Serial.print("Connecting to ");
316
Serial.println(ssid);
417
WiFi.mode(WIFI_STA);
518
WiFi.begin(ssid, password);
619
while (WiFi.status() != WL_CONNECTED) {
720
delay(500);
821
Serial.print(".");
22+
attempt++;
23+
if (attempt >= 20) {
24+
wifiMode = CREATE_NETWORK;
25+
putEEPROMData();
26+
ESP.restart();
27+
}
928
}
1029
Serial.print("IP address: ");
1130
Serial.println(WiFi.localIP());
31+
32+
server.on("/", handleMain);
33+
server.on("/strip", handleStrip);
34+
server.on("/timer", handleTimer);
35+
server.on("/alarm", handleAlarm);
36+
server.on("/alarmList", handleAlarmList);
37+
server.on("/delete", handleDelete);
38+
server.begin();
1239
}
1340

14-
void handleMain(){
41+
void handleWIFIData() {
42+
drawPointer(0, 1, strip.Color(0, 255, 0));
43+
strip.show();
44+
ssid = server.arg("SSID");
45+
password = server.arg("Password");
46+
wifiMode = CONNETC_TO_NETWORK;
47+
putEEPROMData();
48+
server.send(200, "text/plain", "This device is going to reboot...");
49+
delay(500);
50+
ESP.restart();
51+
}
52+
53+
void handleAPPage() {
54+
drawPointer(0, 1, strip.Color(255, 255, 0));
55+
strip.show();
56+
server.send(200, "text/html", getAPPage());
57+
}
58+
59+
void handleMain() {
1560
server.sendHeader("Access-Control-Allow-Origin", "*");
1661
server.send(200, "text/html", getMainPage());
17-
1862
}
1963

2064
void handleStrip() {

Support.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct timeUnit timeFetch;
1010
unsigned long int timeLastFetch;
1111

1212

13+
1314
struct alarm {
1415
int hour;
1516
int min;
@@ -32,12 +33,12 @@ bool alarmsForToday = 1;
3233

3334
ESP8266WebServer server(80);
3435

35-
String ssid = "";
36-
String password = "";
37-
36+
String ssid = "Clock";
37+
String password = "12345678";
3838
bool wifiMode;
3939

4040

41+
4142
// NTP things
4243
#include <NTPClient.h>
4344
#include <WiFiUdp.h>
@@ -63,9 +64,10 @@ Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
6364
enum clockMode {
6465
DARK,
6566
NORMAL,
66-
CONNECTING,
67+
INITIALIZING,
6768
TIMER,
68-
RINGING
69+
RINGING,
70+
NO_WIFI
6971
};
7072
enum stripMode {
7173
MAN_OFF,
@@ -78,7 +80,7 @@ enum ringType {
7880
TIMER_RING,
7981
ALARM_RING
8082
};
81-
byte clockMode = CONNECTING, stripMode = AUTO_ON;
83+
byte clockMode, stripMode = AUTO_ON;
8284
byte fadeStepDuration = 5;
8385
bool inFade;
8486
int lastTimeBTNPressed, lastFadeStep;
@@ -109,8 +111,10 @@ enum ALARM_OFFEST {
109111
};
110112

111113
enum wifiMode {
112-
CONNETC_TO_NETWORK,
113-
CREATE_NETWORK
114+
CREATE_NETWORK,
115+
CONNETC_TO_NETWORK
114116
};
115117

116-
#define TIME_CORRECTION_CONSTANT 200
118+
#define TIME_CORRECTION_CONSTANT 200
119+
120+
bool APConfigured;

webPageSource.ino

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
String getMainPage(){
2-
String html="<!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' /> <meta name='viewport' content='width=device-width, initial-scale=1' /> <title>Clock - Manager</title> <style> @import url(https://fonts.googleapis.com/css?family=Poppins); * { box-sizing: border-box; } body { width: 100%; height: 100%; margin: 0; display: block; background-color: #fff; font-family: poppins; user-select: none; } nav { background-color: #fff; padding: 20px; padding-bottom: 0; width: 100%; text-align: center; font-family: poppins; color: #424242; font-size: 100px; } .button-body { margin: 30px; padding: 20px; align-items: center; border: solid; border-radius: 32px; border-color: #424242; color: #424242; text-align: center; font-size: 40px; } .modal { visibility: hidden; height: 0; font-size: 20px; text-align: left; display: block; } .modal-visible { visibility: visible; height: auto; } .button { display: block; } .checkbox-container { margin-top: 10px; } .checkbox-item { text-align: left; font-size: 25px; margin-bottom: 10px; } .modal-section { margin: auto; max-width: 400px; margin-bottom: 20px; } .submit-button { margin-left: 70%; width: 30%; max-width: 200px; height: 50px; font-size: 20px; border-radius: 8px; font-family: Poppins; color: #424242; border-color: #424242; } .time-picker { height: 40px; font-size: 20px; border-radius: 8px; font-family: Poppins; color: #424242; border-color: #424242; } .simple-button:active { background-color: #eee; } label{ font-size: 20px; } </style> <script> const Http = new XMLHttpRequest(); const url = ''; function showModal() { document.getElementById('alarmModal').classList.toggle('modal-visible'); } </script> </head> <body> <nav> Clock </nav> <div class='button'> <div class='button-body simple-button' onclick='Http.open(\"GET\", url+\"/strip\");Http.send();'> Toggle Strip </div> </div> <div class='button'> <div class='button-body'> <div onclick='showModal();'> New Alarm </div> <div class='modal' id='alarmModal'> <form id='alarmForm' method='get' action='/alarm'> <div class='modal-section'> When will it ring? <div class='checkbox-container'> <div class='checkbox-item'> <div class='checkbox-item'> <label for='time'>Select a time:</label> <input type='time' id='time' name='time'> </div> </div> <div class='checkbox-item'> <input type='checkbox' id='mon' name='mon'> <label for='mon'>Monday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='tue' name='tue'> <label for='tue'>Tuesday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='wed' name='wed'> <label for='wed'>Wednesday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='thu' name='thu'> <label for='thu'>Thursday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='fri' name='fri'> <label for='fri'>Friday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='sat' name='sat'> <label for='sat'>Saturday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='sun' name='sun'> <label for='sun'>Sunday</label> </div> </div> </div> <div class='modal-section'> Repeat only one time <div class='checkbox-container'> <div class='checkbox-item'> <input type='checkbox' id='oneTime' name='oneTime'> <label for='oneTime'>Onetime</label> </div> </div> </div> <input type='submit' class='submit-button' form='alarmForm' value='Set'> </div> </form> </div> </div> </body> </html>";
3-
return html;
1+
String getMainPage() {
2+
String html = "<!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' /> <meta name='viewport' content='width=device-width, initial-scale=1' /> <title>Clock - Manager</title> <style> @import url(https://fonts.googleapis.com/css?family=Poppins); * { box-sizing: border-box; } body { width: 100%; height: 100%; margin: 0; display: block; background-color: #fff; font-family: poppins; user-select: none; } nav { background-color: #fff; padding: 20px; padding-bottom: 0; width: 100%; text-align: center; font-family: poppins; color: #424242; font-size: 100px; } .button-body { margin: 30px; padding: 20px; align-items: center; border: solid; border-radius: 32px; border-color: #424242; color: #424242; text-align: center; font-size: 40px; } .modal { visibility: hidden; height: 0; font-size: 20px; text-align: left; display: block; } .modal-visible { visibility: visible; height: auto; } .button { display: block; } .checkbox-container { margin-top: 10px; } .checkbox-item { text-align: left; font-size: 25px; margin-bottom: 10px; } .modal-section { margin: auto; max-width: 400px; margin-bottom: 20px; } .submit-button { margin-left: 70%; width: 30%; max-width: 200px; height: 50px; font-size: 20px; border-radius: 8px; font-family: Poppins; color: #424242; border-color: #424242; } .time-picker { height: 40px; font-size: 20px; border-radius: 8px; font-family: Poppins; color: #424242; border-color: #424242; } .simple-button:active { background-color: #eee; } label{ font-size: 20px; } </style> <script> const Http = new XMLHttpRequest(); const url = ''; function showModal() { document.getElementById('alarmModal').classList.toggle('modal-visible'); } </script> </head> <body> <nav> Clock </nav> <div class='button'> <div class='button-body simple-button' onclick='Http.open(\"GET\", url+\"/strip\");Http.send();'> Toggle Strip </div> </div> <div class='button'> <div class='button-body'> <div onclick='showModal();'> New Alarm </div> <div class='modal' id='alarmModal'> <form id='alarmForm' method='get' action='/alarm'> <div class='modal-section'> When will it ring? <div class='checkbox-container'> <div class='checkbox-item'> <div class='checkbox-item'> <label for='time'>Select a time:</label> <input type='time' id='time' name='time'> </div> </div> <div class='checkbox-item'> <input type='checkbox' id='mon' name='mon'> <label for='mon'>Monday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='tue' name='tue'> <label for='tue'>Tuesday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='wed' name='wed'> <label for='wed'>Wednesday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='thu' name='thu'> <label for='thu'>Thursday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='fri' name='fri'> <label for='fri'>Friday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='sat' name='sat'> <label for='sat'>Saturday</label> </div> <div class='checkbox-item'> <input type='checkbox' id='sun' name='sun'> <label for='sun'>Sunday</label> </div> </div> </div> <div class='modal-section'> Repeat only one time <div class='checkbox-container'> <div class='checkbox-item'> <input type='checkbox' id='oneTime' name='oneTime'> <label for='oneTime'>Onetime</label> </div> </div> </div> <input type='submit' class='submit-button' form='alarmForm' value='Set'> </div> </form> </div> </div> </body> </html>";
3+
return html;
4+
}
5+
String getAPPage() {
6+
String html = "<!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Clock - WIFI Settings</title> </head> <body> <form action='/WIFIData'> <div> <label for='username'>SSID:</label><br> <input type='text' id='SSID' name='SSID'> </div> <br> <div> <label for='Pass'>Password:</label><br> <input type='password' id='Pass' name='Password'> </div> <br> <input type='submit' value='Enter'> </form> </body> </html>";
7+
return html;
48
}

0 commit comments

Comments
 (0)