-
Notifications
You must be signed in to change notification settings - Fork 4
/
global.h
278 lines (220 loc) · 7.95 KB
/
global.h
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
#ifndef GLOBAL_H
#define GLOBAL_H
ESP8266WebServer server(80); // The Webserver
boolean firstStart = true; // On firststart = true, NTP will try to get a valid time
int AdminTimeOutCounter = 0; // Counter for Disabling the AdminMode
WiFiUDP UDPNTPClient; // NTP Client
volatile unsigned long UnixTimestamp = 0; // GLOBALTIME ( Will be set by NTP)
int cNTP_Update = 0; // Counter for Updating the time via NTP
Ticker tkSecond; // Second - Timer for Updating Datetime Structure
//custom declarations
long absoluteActualTime, actualTime;
long customWatchdog; // WatchDog to detect main loop blocking. There is a builtin WatchDog to the chip firmare not related to this one
struct strConfig {
boolean dhcp; // 1 Byte - EEPROM 16
boolean isDayLightSaving; // 1 Byte - EEPROM 17
long Update_Time_Via_NTP_Every; // 4 Byte - EEPROM 18
long timeZone; // 4 Byte - EEPROM 22
byte IP[4]; // 4 Byte - EEPROM 32
byte Netmask[4]; // 4 Byte - EEPROM 36
byte Gateway[4]; // 4 Byte - EEPROM 40
String ssid; // up to 32 Byte - EEPROM 64
String password; // up to 32 Byte - EEPROM 96
String ntpServerName; // up to 32 Byte - EEPROM 128
String DeviceName; // up to 32 Byte - EEPROM 160
// Application Settings here... from EEPROM 192 up to 511 (0 - 511)
} config;
// Auxiliar function to handle EEPROM
void EEPROMWritelong(int address, long value){
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long EEPROMReadlong(long address){
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
// Check the Values is between 0-255
boolean checkRange(String Value){
if (Value.toInt() < 0 || Value.toInt() > 255)
{
return false;
}
else
{
return true;
}
}
void WriteStringToEEPROM(int beginaddress, String string){
char charBuf[string.length() + 1];
string.toCharArray(charBuf, string.length() + 1);
for (int t = 0; t < sizeof(charBuf); t++)
{
EEPROM.write(beginaddress + t, charBuf[t]);
}
}
String ReadStringFromEEPROM(int beginaddress){
volatile byte counter = 0;
char rChar;
String retString = "";
while (1)
{
rChar = EEPROM.read(beginaddress + counter);
if (rChar == 0) break;
if (counter > 31) break;
counter++;
retString.concat(rChar);
}
return retString;
}
/*
**
** CONFIGURATION HANDLING
**
*/
void ConfigureWifi(){
Serial.println("Configuring Wifi");
WiFi.begin (config.ssid.c_str(), config.password.c_str());
while (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected");
delay(500);
}
if (!config.dhcp)
{
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] ));
}
}
void WriteConfig(){
Serial.println("Writing Config");
EEPROM.write(0, 'C');
EEPROM.write(1, 'F');
EEPROM.write(2, 'G');
EEPROM.write(16, config.dhcp);
EEPROM.write(17, config.isDayLightSaving);
EEPROMWritelong(18, config.Update_Time_Via_NTP_Every); // 4 Byte
EEPROMWritelong(22, config.timeZone); // 4 Byte
EEPROM.write(32, config.IP[0]);
EEPROM.write(33, config.IP[1]);
EEPROM.write(34, config.IP[2]);
EEPROM.write(35, config.IP[3]);
EEPROM.write(36, config.Netmask[0]);
EEPROM.write(37, config.Netmask[1]);
EEPROM.write(38, config.Netmask[2]);
EEPROM.write(39, config.Netmask[3]);
EEPROM.write(40, config.Gateway[0]);
EEPROM.write(41, config.Gateway[1]);
EEPROM.write(42, config.Gateway[2]);
EEPROM.write(43, config.Gateway[3]);
WriteStringToEEPROM(64, config.ssid);
WriteStringToEEPROM(96, config.password);
WriteStringToEEPROM(128, config.ntpServerName);
WriteStringToEEPROM(160, config.DeviceName);
// Application Settings here... from EEPROM 192 up to 511 (0 - 511)
EEPROM.commit();
}
boolean ReadConfig(){
Serial.println("Reading Configuration");
if (EEPROM.read(0) == 'C' && EEPROM.read(1) == 'F' && EEPROM.read(2) == 'G' )
{
Serial.println("Configurarion Found!");
config.dhcp = EEPROM.read(16);
config.isDayLightSaving = EEPROM.read(17);
config.Update_Time_Via_NTP_Every = EEPROMReadlong(18); // 4 Byte
config.timeZone = EEPROMReadlong(22); // 4 Byte
config.IP[0] = EEPROM.read(32);
config.IP[1] = EEPROM.read(33);
config.IP[2] = EEPROM.read(34);
config.IP[3] = EEPROM.read(35);
config.Netmask[0] = EEPROM.read(36);
config.Netmask[1] = EEPROM.read(37);
config.Netmask[2] = EEPROM.read(38);
config.Netmask[3] = EEPROM.read(39);
config.Gateway[0] = EEPROM.read(40);
config.Gateway[1] = EEPROM.read(41);
config.Gateway[2] = EEPROM.read(42);
config.Gateway[3] = EEPROM.read(43);
config.ssid = ReadStringFromEEPROM(64);
config.password = ReadStringFromEEPROM(96);
config.ntpServerName = ReadStringFromEEPROM(128);
config.DeviceName = ReadStringFromEEPROM(160);
// Application parameters here ... from EEPROM 192 to 511
return true;
}
else
{
Serial.println("Configurarion NOT FOUND!!!!");
return false;
}
}
void printConfig(){
Serial.println("Printing Config");
Serial.printf("DHCP:%d\n", config.dhcp);
Serial.printf("DayLight:%d\n", config.isDayLightSaving);
Serial.printf("NTP update every %ld sec\n", config.Update_Time_Via_NTP_Every); // 4 Byte
Serial.printf("Timezone %ld\n", config.timeZone); // 4 Byte
Serial.printf("IP:%d.%d.%d.%d\n", config.IP[0],config.IP[1],config.IP[2],config.IP[3]);
Serial.printf("Mask:%d.%d.%d.%d\n", config.Netmask[0],config.Netmask[1],config.Netmask[2],config.Netmask[3]);
Serial.printf("Gateway:%d.%d.%d.%d\n", config.Gateway[0],config.Gateway[1],config.Gateway[2],config.Gateway[3]);
Serial.printf("SSID:%s\n", config.ssid.c_str());
Serial.printf("PWD:%s\n", config.password.c_str());
Serial.printf("ntp ServerName:%s\n", config.ntpServerName.c_str());
Serial.printf("Device Name:%s\n", config.DeviceName.c_str());
// Application Settings here... from EEPROM 192 up to 511 (0 - 511)
}
String GetMacAddress(){
uint8_t mac[6];
char macStr[18] = {0};
WiFi.macAddress(mac);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
String GetAPMacAddress(){
uint8_t mac[6];
char macStr[18] = {0};
WiFi.softAPmacAddress(mac);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
// convert a single hex digit character to its integer value (from https://code.google.com/p/avr-netino/)
unsigned char h2int(char c){
if (c >= '0' && c <= '9') {
return ((unsigned char)c - '0');
}
if (c >= 'a' && c <= 'f') {
return ((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <= 'F') {
return ((unsigned char)c - 'A' + 10);
}
return (0);
}
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;
}
#endif