-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.cpp
249 lines (220 loc) · 6.44 KB
/
global.cpp
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
//
//
//
#define DBG_OUTPUT_PORT Serial
#include "global.h"
#include "Config.h"
#include <ESP8266WiFi.h>
#include "DynamicData.h"
#include <ArduinoOTA.h>
int AdminTimeOutCounter = 0;
Ticker secondTk;
boolean secondFlag = false;
ntpClient* ntp;
//boolean wifiIsConnected = false;
long wifiDisconnectedSince = 0;
wifiStatus currentWifiStatus = FIRST_RUN;
//WebSocketsServer wsServer = WebSocketsServer(81);
extern strConfig config;
void ConfigureWifi()
{
WiFi.mode(WIFI_STA);
currentWifiStatus = WIFI_STA_DISCONNECTED;
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.printf("Connecting to %s\n", config.ssid.c_str());
#endif // DEBUG_GLOBALH
WiFi.begin(config.ssid.c_str(), config.password.c_str());
if (!config.dhcp)
{
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.println("NO DHCP");
#endif // DEBUG_GLOBALH
WiFi.config(
IPAddress(config.IP[0], config.IP[1], config.IP[2], config.IP[3]),
IPAddress(config.Gateway[0], config.Gateway[1], config.Gateway[2], config.Gateway[3]),
IPAddress(config.Netmask[0], config.Netmask[1], config.Netmask[2], config.Netmask[3]),
IPAddress(config.DNS[0], config.DNS[1], config.DNS[2], config.DNS[3])
);
}
delay(2000);
delay(5000); // Wait for WiFi
while (!WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
if (WiFi.isConnected()) {
currentWifiStatus = WIFI_STA_CONNECTED;
}
DBG_OUTPUT_PORT.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.printf("Gateway: %s\n", WiFi.gatewayIP().toString().c_str());
DBG_OUTPUT_PORT.printf("DNS: %s\n", WiFi.dnsIP().toString().c_str());
Serial.println(__PRETTY_FUNCTION__);
#endif // DEBUG_GLOBALH
}
void ConfigureWifiAP() {
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.println(__PRETTY_FUNCTION__);
#endif // DEBUG_GLOBALH
//WiFi.disconnect();
WiFi.mode(WIFI_AP);
String APname = apConfig.APssid + (String)ESP.getChipId();
//APname += (String)ESP.getChipId();
//WiFi.softAP(APname.c_str(), apConfig.APpassword.c_str());
if (httpAuth.wwwPassword != "")
WiFi.softAP(APname.c_str(), httpAuth.wwwPassword.c_str());
else
WiFi.softAP(APname.c_str());
if (CONNECTION_LED >= 0) {
flashLED(CONNECTION_LED, 3, 250);
}
}
void secondTick()
{
secondFlag = true;
}
void secondTask() {
#ifdef DEBUG_GLOBALH
//DBG_OUTPUT_PORT.println(ntp->getTimeString());
#endif // DEBUG_GLOBALH
sendTimeData();
}
String urldecode(String input) // (based on https://code.google.com/p/avr-netino/)
{
char c;
String ret = "";
for (byte t = 0; t<input.length(); t++)
{
c = input[t];
if (c == '+') c = ' ';
if (c == '%') {
t++;
c = input[t];
t++;
c = (h2int(c) << 4) | h2int(input[t]);
}
ret.concat(c);
}
return ret;
}
//
// Check the Values is between 0-255
//
boolean checkRange(String Value)
{
if (Value.toInt() < 0 || Value.toInt() > 255)
{
return false;
}
else
{
return true;
}
}
void WiFiEvent(WiFiEvent_t event) {
/*String eventStr;
switch (event) {
case WIFI_EVENT_STAMODE_CONNECTED:
eventStr = "STAMODE_CONNECTED"; break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
eventStr = "STAMODE_DISCONNECTED"; break;
case WIFI_EVENT_STAMODE_AUTHMODE_CHANGE:
eventStr = "STAMODE_AUTHMODE_CHANGE"; break;
case WIFI_EVENT_STAMODE_GOT_IP:
eventStr = "STAMODE_GOT_IP"; break;
case WIFI_EVENT_STAMODE_DHCP_TIMEOUT:
eventStr = "STAMODE_DHCP_TIMEOUT"; break;
case WIFI_EVENT_SOFTAPMODE_STACONNECTED:
eventStr = "SOFTAPMODE_STACONNECTED"; break;
case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED:
eventStr = "SOFTAPMODE_STADISCONNECTED"; break;
case WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED:
eventStr = "SOFTAPMODE_PROBEREQRECVED"; break;
case WIFI_EVENT_MAX:
eventStr = "MAX_EVENTS"; break;
}
DBG_OUTPUT_PORT.printf("%s: %s\n",__PRETTY_FUNCTION__,eventStr.c_str());
DBG_OUTPUT_PORT.printf("Current WiFi status: %d\n", currentWifiStatus);*/
switch (event) {
case WIFI_EVENT_STAMODE_GOT_IP:
//DBG_OUTPUT_PORT.println(event);
if (CONNECTION_LED >= 0) {
digitalWrite(CONNECTION_LED, LOW); // Turn LED on
}
//DBG_OUTPUT_PORT.printf("Led %s on\n", CONNECTION_LED);
//turnLedOn();
wifiDisconnectedSince = 0;
currentWifiStatus = WIFI_STA_CONNECTED;
break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.println("case STA_DISCONNECTED");
#endif // DEBUG_GLOBALH
if (CONNECTION_LED >= 0) {
digitalWrite(CONNECTION_LED, HIGH); // Turn LED off
}
//DBG_OUTPUT_PORT.printf("Led %s off\n", CONNECTION_LED);
//flashLED(config.connectionLed, 2, 100);
if (currentWifiStatus == WIFI_STA_CONNECTED) {
currentWifiStatus == WIFI_STA_DISCONNECTED;
wifiDisconnectedSince = millis();
}
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.printf("Disconnected for %d seconds\n", (int)((millis() - wifiDisconnectedSince) / 1000));
#endif // DEBUG_GLOBALH
}
}
void flashLED(int pin, int times, int delayTime) {
int oldState = digitalRead(pin);
for (int i = 0; i < times; i++) {
digitalWrite(pin, LOW); // Turn on LED
delay(delayTime);
digitalWrite(pin, HIGH); // Turn on LED
delay(delayTime);
}
digitalWrite(pin, oldState); // Turn on LED
}
void dimLEDon(int pin, int range) {
analogWriteRange(range);
for (int i = range; i > 0; i--) {
if (CONNECTION_LED >= 0) {
analogWrite(CONNECTION_LED, i);
}
delay(10);
}
}
//
void ConfigureOTA(String password) {
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname(config.DeviceName.c_str());
// No authentication by default
if (password!="") {
ArduinoOTA.setPassword(password.c_str());
#ifdef DEBUG_GLOBALH
DBG_OUTPUT_PORT.printf("OTA password set %s\n", password.c_str());
#endif // DEBUG_GLOBALH
}
#ifdef DEBUG_GLOBALH
ArduinoOTA.onStart([]() {
DBG_OUTPUT_PORT.println("StartOTA \n");
});
ArduinoOTA.onEnd([]() {
DBG_OUTPUT_PORT.println("\nEnd OTA\n");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
DBG_OUTPUT_PORT.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
DBG_OUTPUT_PORT.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) DBG_OUTPUT_PORT.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) DBG_OUTPUT_PORT.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) DBG_OUTPUT_PORT.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) DBG_OUTPUT_PORT.println("Receive Failed");
else if (error == OTA_END_ERROR) DBG_OUTPUT_PORT.println("End Failed");
});
DBG_OUTPUT_PORT.println("\nOTA Ready");
#endif // DEBUG_GLOBALH
ArduinoOTA.begin();
}