-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWiFiSwitch.ino
More file actions
220 lines (201 loc) · 5.45 KB
/
WiFiSwitch.ino
File metadata and controls
220 lines (201 loc) · 5.45 KB
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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#define RUN_TASKS 32
#include <Run.h>
#include "settings.h"
ADC_MODE(ADC_VCC);
const char* VERSION = "0.8.3";
// Pin to activete WiFiManager configuration routine
#define RESET_PIN D8
// Current query interval (mS)
#define A0_DELAY 1000
#define WIFI_SETUP_AP "SocketWIFI_AP_"
#define WIFI_CHECK_DELAY 1000
#define WIFI_CHECK_COUNT 150
struct events {
uint16_t wifiConnected = 0;
uint16_t resetLongPress = 0;
uint16_t resetShortPress = 0;
uint16_t turnOffAllSockets= 0;
uint16_t keyPressed = 0;
uint16_t keyReleased = 0;
uint16_t saveParams = 0;
};
struct statuses {
bool wifiConnected = false;
bool ntpSync = false;
bool rtcPresent = false;
bool rtcValid = false;
bool keyPressed = false;
};
events event;
statuses status;
#define DATA9K 9000
char data[DATA9K]; // sprintf buffer
#define PUMP_NONE "100"
#define PUMP_SINGLE "110"
#define PUMP_DOUBLE_PULSE "121"
#define PUMP_DOUBLE_ALT "122"
#define PUMP_DOUBLE_SER "123"
#define PUMP_DOUBLE_RND "124"
#define PUMP_QUAD_PULSE "141"
#define PUMP_QUAD_ALT "142"
#define PUMP_QUAD_SER "143"
#define PUMP_QUAD_RND "144"
#define GROUP_HTML_BASE 11
String ntp1 = "pool.ntp.org";
String ntp2 = "time.nist.gov";
String ntp3 = "time.apple.com";
String tz = "5";
String admin = "admin";
String pass = "password";
String sysName = "socket";
uint32_t wifiStart();
#include "ACemon.h"
#include "clock.h"
#include "control.h"
#include "config.h"
#include "web.h"
#include "discovery.h"
#include "update.h"
#include "ping.h"
#include "alexa.h"
uint8_t waitWF;
uint32_t wifiStart() {
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin();
waitWF = 1;
taskAddWithDelay(wifiWait, WIFI_CHECK_DELAY);
#ifdef WFS_DEBUG
String ssid = WiFi.SSID();
WDEBUG("Connecting to %s\n", ssid.c_str());
#endif
return RUN_DELETE;
}
uint32_t wifiWait() {
if(WiFi.status() != WL_CONNECTED) {
if (waitWF <= WIFI_CHECK_COUNT) {
WDEBUG(".");
waitWF++;
return WIFI_CHECK_DELAY;
} else {
waitWF = 1;
//WiFi.mode(WIFI_OFF);
taskAddWithDelay(wifiStart, WIFI_CHECK_DELAY);
return RUN_DELETE;
}
} else {
#ifdef WFS_DEBUG
String ip = WiFi.localIP().toString();
WDEBUG("IP Address: %s\n", ip.c_str());
#endif
event.wifiConnected++;
randomSeed(millis());
taskAdd(initPing);
}
return RUN_DELETE;
}
void cbSaveParams() {
event.saveParams++;
}
void cbConnected(WiFiManager *wfm) {
}
uint32_t restartESP() {
timer1_disable();
ESP.restart();
return RUN_DELETE;
}
uint32_t stopPing();
uint32_t wifiManager() {
stopPing();
server.stop();
WiFiManager wifiManager;
WiFi.mode(WIFI_OFF);
delay(1000);
wifiManager.setSaveConfigCallback(cbSaveParams);
//wifiManager.resetSettings();
wifiManager.setBreakAfterConfig(true);
wifiManager.setTimeout(120);
wifiManager.addParameter(&pNameT);
wifiManager.addParameter(&pName);
char apname[sizeof(WIFI_SETUP_AP)+5];
byte mac[6];
WiFi.macAddress(mac);
sprintf_P(apname, PSTR("%s%02X%02X"), WIFI_SETUP_AP, mac[4], mac[5]);
wifiManager.startConfigPortal(apname);
WDEBUG("Connected to %s\n", WiFi.SSID().c_str());
sysName = pName.getValue();
saveConfig();
delay(1000);
ESP.restart();
return RUN_DELETE;
}
// Query Reset Key ststus change and flag events
uint32_t checkKey() {
if (digitalRead(RESET_PIN) == HIGH && !status.keyPressed) {
event.keyPressed++;
status.keyPressed = true;
}
if (digitalRead(RESET_PIN) == LOW && status.keyPressed) {
event.keyReleased++;
status.keyPressed = false;
}
return 100;
}
#define KEY_LONG_TIME 3000
// Called on Key Pressed Event
uint32_t keyPressed() {
taskAddWithDelay(keyLongPressed, KEY_LONG_TIME);
return RUN_NEVER;
}
// Called on Key Release Event
uint32_t keyReleased() {
taskDel(keyLongPressed);
ESP.reset();
return RUN_NEVER;
}
// Called if Key Pressed for KEY_LONG_TIME mS
uint32_t keyLongPressed() {
taskDel(keyReleased);
turnOffAllSockets();
wifiManager();
return RUN_DELETE;
}
void setup() {
#ifdef WFS_DEBUG
Serial.begin(74880); //For debug
#endif
SPIFFS.begin();
//xmlo.init((uint8_t *)buffer, sizeof(buffer), &XML_callback);
readConfig();
//readState();
taskAdd(wifiStart); // Add task with Wi-Fi initialization code
taskAdd(initRTC); // Add task with RTC init
taskAdd(initSockets); // Add task to initilize Sockets control
taskAddWithDelay(initA0, 5000); // Add task to initialize ADC query
taskAddWithDelay(initNTP, 5000); // Run initNTP()
//taskAddWithSemaphore(initNTP, &event.wifiConnected); // Run initNTP() on Wi-Fi connection
taskAddWithSemaphore(initWeb, &event.wifiConnected); // Run initWeb() on Wi-Fi connection
taskAddWithSemaphore(discovery, &event.wifiConnected);
taskAddWithSemaphore(initUpdate, &event.wifiConnected);
taskAddWithSemaphore(initAlexa, &event.wifiConnected);
//taskAdd(printTime); //For debug
taskAdd(checkKey); // Key query
taskAddWithSemaphore(keyPressed, &event.keyPressed); // Run keyPressed() on keyPressed event
taskAddWithSemaphore(keyReleased, &event.keyReleased);// Run keyReleased() on keyRelease event
//taskAddWithSemaphore(saveConfig, &event.saveParams); // Save config on WiFiManager request
taskAdd(readState);
taskAdd(queryA0);
//taskAdd(initPing);
}
void loop(void) {
//wdt_enable(0);
taskExec();
yield();
wdt_reset();
}