-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHD_Utilities.h
322 lines (269 loc) · 10.3 KB
/
HD_Utilities.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
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
#include <EEPROM.h>
// Time / NTP
#define TZ 0 // (utc+) TZ in hours
#define DST_MN 60 // use 60mn for summer time in some countries
#define TZ_MN ((TZ)*60)
#define TZ_SEC ((TZ)*3600)
#define DST_SEC ((DST_MN)*60)
timeval tv;
time_t now;
// --------------------------- Config -----------------------------
byte cfg_version_major = 0;
byte cfg_version_minor = 0;
byte cfg_API_Write = 0;
byte cfg_VerboseTrace = 0;
#define WIFI_SSID 0
#define WIFI_PASS 1
#define DHCP_ENAB 2
#define FIXED_IP 3
#define FIXED_SUB 4
#define FIXED_GW 5
#define FIXED_DNS 6
#define SMS_NR 7
#define WEB_ENABLED 8
#define WEB_PASS 9
#define WEB_PORT 10
#define MQTT_ENAB 11
#define MQTT_SRVR 12
#define MQTT_PORT 13
#define MQTT_USER 14
#define MQTT_PASS 15
#define MQTT_SUBS 16
#define ENGAGED 17
const String setname[] = {"WIFI_SSID","WIFI_PASS","DHCP_ENAB","FIXED_IP","FIXED_SUB","FIXED_GW","FIXED_DNS","SMS_NR","WEB_ENABLED","WEB_PASS","WEB_PORT","MQTT_ENAB","MQTT_SRVR","MQTT_PORT","MQTT_USER","MQTT_PASS","MQTT_SUBS","ENGAGED","eof"};
String setting[] = {"none", "none", "1", "0.0.0.0","255.255.255.0","0.0.0.0", "1.1.1.1", "0", "1", "1234", "80", "0", "none", "1883", "homeassistant", "none", "0", "0"}; // Defaults
//-----------------------------------------------------------------------------------------------
void trace (String text, String func) ;
void blinkLed(){
if (Led) {digitalWrite(LED_BUILTIN, LOW);Led=0;}
else
{digitalWrite(LED_BUILTIN, HIGH);Led=1;}
}
//-----------------------------------------------------------------------------------------------
void init_eeprom() {
EEPROM.begin(512); // Max 4096
}
void write_eeprom_not_used(){
byte i;
EEPROM.write(510, 123);EEPROM.write(511, 221); // Init är gjord
EEPROM.commit();
}
//-----------------------------------------------------------------------------------------------
void settings_rw(bool read_settings){ //Läs och skriv inställningar till SPIFFS fil
byte i;
String s, key, val;
byte mid, end;
bool Hit=0;
cfg_version_major = 0;
cfg_version_minor = 2;
if (!read_settings)
{
// Ladda config från SPIFFS fil.
// Öppna fil
File f = SPIFFS.open("/config2.txt", "r");
if (!f) {trace("Config file failed!","SER LF LOG");
return;
}
// Parsa json string till variabler
while (f.available()){ // Så länge ej EOF och H1 svarar med ACK
s = f.readStringUntil(','); // Läs en rad
s.trim();
//Serial.println(s);
mid = s.indexOf(':');
end = s.length();
key = s.substring(1,mid-1);
val = s.substring(mid+2,end-1);
// Serial.println (key + " : " + val);
i=0;Hit=0;
while (setname[i] != "eof" && !Hit)
{
if (key == setname[i])
{setting[i] = val;
//Serial.println("set " + String(i));
Hit=1;
}
i++;
}
}
}
else
{
// STORE SETTINGS
File f = SPIFFS.open("/config2.txt", "w");
if (!f) { trace("Config file write failed!","SER LF LOG");
return;
}
i=0;
while (setname[i] != "eof") // Skriv till json format
{f.println("'" + setname[i] + "':'" + setting[i] + "',");
i++;
}
f.close();
}
}
//-----------------------------------------------------------------------------------------------
void rssi_update()
{
int i = WiFi.RSSI();
char s[7] = "Strong";
if (i< -60) sprintf(s,"Good");
if (i< -70) sprintf(s,"Okay");
if (i< -80) sprintf(s,"Weak");
if (i< -90) sprintf(s,"Bad");
sprintf(g_wifi_rssi,"%s %ddBm", s, i); // Uppdatera signalstyrka
}
//-----------------------------------------------------------------------------------------------
String tid(char val){
String t;
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
if (val==0 || val==1 || val==3) // {return String(hr) + ":" + String(min % 60) + ":" + String(sec % 60);} // "10:00:00"
//else if (val==1) // "2018-12-01 10:00:00"
{
now = time(nullptr);
char buffer[80];
if (val==1)
strftime (buffer,80,"%Y-%m-%d %H:%M:%S",localtime(&now));
else if (val==3)
strftime (buffer,80,"%H%M%S",localtime(&now));
else
strftime (buffer,80,"%H:%M:%S",localtime(&now));
t = buffer;
return t;
//Serial.println (buffer);
}
else if (val==2) // Connect to NTP and report
{
char buffer[10];
buffer[0]=1; // 1970
char i=0;
while (buffer[0] != '2' && i++<5)
{
now = time(nullptr);
delay(100);
strftime (buffer,10,"%Y",localtime(&now));
}
if (buffer[0] != '2') trace("Cannot get NTP updates!","SER LOG LF"); else trace("NTP server connected successfully!","SER LOG LF");
}
/*
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time representation for your locale
%d Day of month as a decimal number (01-31)
%H Hour in 24-hour format (00-23)
%I Hour in 12-hour format (01-12)
%j Day of year as decimal number (001-366)
%m Month as decimal number (01-12)
%M Minute as decimal number (00-59)
%p Current locale's A.M./P.M. indicator for 12-hour clock
%S Second as decimal number (00-59)
%U Week of year as decimal number, Sunday as first day of week (00-51)
%w Weekday as decimal number (0-6; Sunday is 0)
%W Week of year as decimal number, Monday as first day of week (00-51)
%x Date representation for current locale
%X Time representation for current locale
%y Year without century, as decimal number (00-99)
%Y Year with century, as decimal number
%z %Z Time-zone name or abbreviation, (no characters if time zone is unknown)
%% Percent sign
You can include text literals (such as spaces and colons) to make a neater display or for padding between adjoining columns.
You can suppress the display of leading zeroes by using the "#" character (%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y)
*/
}
//===============================================================================
void trace (String text, String func) // SER LOG LF
{
byte i;
if (func.indexOf("VB")!=-1) if (!cfg_VerboseTrace) return;
if (debug_log.length() > 2000) {debug_log += tid(0) + " Log sent, was full..."; debug_log="";}
if (func.indexOf("LF")!=-1) text += "\n";
//if (func.indexOf("SER")!=-1) Serial.print(debug_log.length() + " " + text);
if (func.indexOf("SER")!=-1) Serial.print(text);
if (func.indexOf("LOG")!=-1) {debug_log += tid(0) + " " + text; }
if (func.indexOf("LIST")!=-1) {
Serial.print("TRACE LOG");Serial.println(" ------------");
Serial.print(debug_log);
}
}
//===============================================================================
// behöver inte returnera nått, modifierar originalvariabeln
char *tolow (char *s)
{
char *p;
for (p = s; *p != '\0'; ++p) { *p = tolower(*p); }
return s;
}
//===============================================================================
bool config (String cmd)
{
String subcmd = "";
String val = "";
String s;
byte i, idx;
byte Hit=0;
if (cmd.length() > 35) return 0; // too long
//Serial.println (cmd);
idx = cmd.indexOf('=');
subcmd = cmd.substring(4,idx); // Läs ut kommando
subcmd.toUpperCase();
val = cmd.substring(idx+1,cmd.indexOf('\n')); // Läs ut värde att sätta
if (val.length() > 25) return 0; // too long value
i=0;Hit=0;
while (setname[i] != "eof" && !Hit)
{
if (subcmd == setname[i])
{setting[i] = val;
//Serial.println("set " + String(i));
Hit=1;
}
i++;
}
if (idx==-1 || Hit==0) { // Endast CMD utan värde = lista config
String output = "SER LF";
if (subcmd == "statuslog") output = "SER LOG LF";
i=0;
while (setname[i] != "eof")
{
trace(setname[i] + "=" + setting[i], output);
i++;
}
}
if(Hit)
{trace(subcmd + " is now set to " + val, "SER LF");
// Serial.print(subcmd);Serial.print(" is now set to ");Serial.println(val);
settings_rw(true); //Skriv ner settings
return 1;
}
else
trace("Invalid setting format: " + cmd, "SER LF LOG");
return 0; // Settting not found
}
//------------------------------------------------------------------------------------------------------------------
void esp_status(byte val)
{
char x[20];
String output = "SER LF";
if (val == 0 || val == 3)
{
if (val==3) output = "SER LF LOG";
trace("-- Status --",output);
trace("Time NTP: " + tid(1),output);
trace("Uptime: " + tid(0),output);
trace("WIFI MAC address: " + String(g_wifi_mac),output);
trace("WIFI IP address: " + String(g_wifi_ip),output);
trace("WIFI Signal: " + String(g_wifi_rssi),output);
trace("WIFI connection: " + String(state_Wifi),output);
trace("Internal Webserver status: " + String(state_HttpServer),output);
}
else if (val == 2)
{
tid(1).toCharArray(x,20); mqttpub ("STATUS/TIME", x);
tid(0).toCharArray(x,20); mqttpub ("STATUS/UPTIME", x);
mqttpub ("STATUS/MAC", g_wifi_mac);
mqttpub ("STATUS/IP", g_wifi_ip);
mqttpub ("STATUS/RSSI", g_wifi_rssi);
}
}