-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD_Display.ino
527 lines (489 loc) · 14.5 KB
/
LCD_Display.ino
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
Simple HTTP get webclient test
*/
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
#include "Memory_Helper.h"
#include "LCD_Helper.h"
//********************** DECLARE Wifi ******************************/
char ssid[32] ;
char password[32];
String host = "boonygroup.com";
char* host_char = "boonygroup.com";
const int httpPort = 80;
String Macaddress;
boolean IsConnected = false;
boolean Found_Network = false;
const int port = 9876; // and this port
WiFiServer server(port);
WiFiClient client;
boolean Develop_Mode = false;
boolean Default_Loaded = false;
/*********************** DECLARE SERVER **************************/
int Success_Message = 0;
String Server_Messsage;
String Data_Message;
String Last_Data_Message;
String Default_Data;
//##########################DECLARE DEVELOP MODE ########################///
bool Debug_Mode_Live = false;
bool force_develop_mode = false;
String develop_version = "1.1";
int CONNECTION_TIMES_FAILED = 0;
int setting_config = 1;
const char* develop_ssid = "BoonyDeveloper";
const char* develop_password = "vanphong@2017";
//set command config -- not change
char cmd[100]; // stores the command chars received from RoboRemo
int cmdIndex;
unsigned long lastCmdTime = 60000;
unsigned long aliveSentTime = 0;
//load library
LCD_Helper lcd_helper = LCD_Helper();
Memory_Helper memory_helper = Memory_Helper();
void setup() {
Serial.begin(115200);
//############################ STEP 1 SCAN NETWORK #############################//
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
//WiFi.disconnect();
delay(100);
Serial.println("Scan network Nearby");
delay(5000);
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
{
Serial.println("no networks found");
}
else
{
Serial.print(n);
Serial.println(" networks found");
if (force_develop_mode == false)
{
for (int i = 0; i < n; ++i)
{
if (WiFi.SSID(i) == develop_ssid)
{
Develop_Mode = true;
Found_Network = true;
break;
}
delay(10);
}
}
else
{
Develop_Mode = true;
Found_Network = true;
}
}
Serial.println("");
byte mac[6];
WiFi.macAddress(mac);
Macaddress = macToStr(mac);
lcd_helper.Begin_LCD();
delay(100);
// END STEP 1 SCAN NETWORK //
}
void ConnectWifi()
{
String str_ssid = String(ssid);
String str_develop_ssid = String(develop_ssid);
if (Develop_Mode == true )
{
if (force_develop_mode == true)
{
char* _force_develop_ssid = "Boony";
char* _develop_password= "#boonygroup";
str_develop_ssid = String(_force_develop_ssid);
lcd_helper.PrintLCD("FORCE DEVELOPER MODE<br>Connecting to Wifi<br>" + str_develop_ssid+ +"<br>Version: "+develop_version);
WiFi.begin(_force_develop_ssid, _develop_password);
delay (8000);
}
else
{
if (CONNECTION_TIMES_FAILED < 3)
{
lcd_helper.PrintLCD(" DEVELOPER MODE <br>Connecting to Wifi<br>" + str_develop_ssid);
WiFi.begin(develop_ssid, develop_password);
delay (8000);
}
else
{
Develop_Mode = false;
CONNECTION_TIMES_FAILED = 0;
lcd_helper.PrintLCD("Swiching To<br>Normal Mode<br>Connecting to Wifi<br>" + str_ssid);
}
}
}
else // Develop_Mode = false;
{
// ########################### READ WIFI CONFIGURATION FROM MEMORY ##################################
if (Default_Loaded == false)
{
EEPROM.begin(512);
String esid= memory_helper.Read_Wifi_UserName();
String epass = memory_helper.Read_Wifi_Password();
Default_Data = memory_helper.Read_Lastest_Data();
String temp_host= memory_helper.Read_Host_Configuration();
Last_Data_Message = Default_Data;
EEPROM.commit();
Serial.println("Memory wifi setup " );
if (temp_host.length() > 2)
{
host = temp_host;
// Length (with one extra character for the null terminator)
int str_len = host.length() + 1;
// Prepare the character array (the buffer)
host_char =new char[str_len];
// Copy it over
host.toCharArray(host_char, str_len);
}
if (esid.length() > 2)
{
Serial.println("Had Wifi memory " + esid);
Serial.println("Default data" + Default_Data);
if (Debug_Mode_Live == true)
{
Serial.println("Wifi SSID " + esid);
Serial.println("Wifi PASS " + epass);
}
}
else
{
esid = "sony";
epass = "sony@2017";
}
esid.toCharArray(ssid, esid.length()+1);
epass.toCharArray(password, epass.length()+1);
Default_Loaded = true;
str_ssid = String(ssid);
}// END READ WIFI CONFIGURATION FROM MEMORY
if (Found_Network == false)
{
int n = WiFi.scanNetworks();
if (n == 0)
{
print_in_debug("no networks found");
}
else
{
for (int i = 0; i < n; ++i)
{
if (WiFi.SSID(i) == ssid)
{
lcd_helper.PrintLCD("Connecting to Wifi<br>" + str_ssid);
WiFi.begin(ssid, password);
Found_Network = true;
delay (5000);
break;
}
delay(10);
}
}
}//End Searching network
}//End read data from ROM
if (Found_Network == false || (String)WiFi.localIP() == "0") // Connect Failed
{
CONNECTION_TIMES_FAILED ++;
if (Develop_Mode == true)
{
lcd_helper.PrintLCD("Developer Failed<br>" + Macaddress + "<br>" + "Trying... " + String (CONNECTION_TIMES_FAILED) + " Time(s)" );
}
else
{
Load_Default_Data();
delay (2000);
if (Found_Network == false)
{
print_in_debug("NOT FOUND NETWORK:<br>" + str_ssid);
}
else
{
if (CONNECTION_TIMES_FAILED >= 3)
{
print_in_debug("Can't connect Wifi<br>" + str_ssid);
}
else
{
String connect_times_point = ".";
for (int i=0; i < 20; i++){
connect_times_point = connect_times_point + ".";
}
if (Debug_Mode_Live == true)
{
print_in_debug("Connect fail to Wifi<br>" + str_ssid + "<br>Trying again " + CONNECTION_TIMES_FAILED + " times");
}
}
}
}
}
else
{
IsConnected = true;
String ipaddress = WiFi.localIP().toString();
if (Develop_Mode == true)
{
server.begin(); // start TCP server
lcd_helper.PrintLCD(" DEVELOPER MODE <br>IP: " + ipaddress + "<br>" + Macaddress+"<br>Version: "+develop_version);
}
else
{
lcd_helper.PrintLCD("Test connect to: <br>" + String(host) + "<br>" + Macaddress);
if (!client.connect(host_char, httpPort)) {
//######################## LOAD DEFAULT DATA##################################
print_in_debug("Test connection Failed<br>Load default data");
Load_Default_Data();
return;
}
else
{
lcd_helper.PrintLCD("LOADING . . . .<br>Please wait..." + (String)char(5) + (String)char(6)); //Display arrow up and down
}
}
}
}
void loop() {
if (IsConnected == false)
{
ConnectWifi();
}
else // End if (IsConnected == false)
{
if (Develop_Mode == true)
{
String ipaddress = WiFi.localIP().toString();
lcd_helper.PrintLCD(" DEVELOPER MODE <br>IP: " + ipaddress + "<br>" + Macaddress+"<br>Version: "+develop_version);
if (!client.connected())
{
// listen for incoming clients
client = server.available();
return;
}
if (client.available())
{
//Serial.println("Client connected");
char c = (char)client.read(); // read char from client (RoboRemo app)
if (c == '\n') { // if it is command ending
Serial.println("Raw data " + (String)cmd + " Length " + (String)cmdIndex);
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else
{
cmd[cmdIndex] = c; // add to the cmd buffer
if (cmdIndex < 99) cmdIndex++;
}
}
if (millis() - aliveSentTime > 500) { // every 500ms
client.write("Connected. 1\n");
aliveSentTime = millis();
}
}
else //IsConnected && Develop_Mode == false
{
print_in_debug("Downloading Data...<br>Connnected : " + (String)IsConnected + "<br>Found Network : " + (String)Found_Network); // Debug only
Load_Data();
delay(10000);
}
}
}
void Load_Data()
{
//########################### LAY DU LIEU #######################################################
if (!client.connect(host_char, httpPort)) {
print_in_debug("Can't Connect To Server<br>Please Check Server...");
Load_Default_Data();
IsConnected = false;
Found_Network = false;
return;
}
print_in_debug("Get Data on server ");
// LAY THÔNG TIN TRÊN SERVER
String device_status = "MACAddress=" + Macaddress + "&local_ip=" + WiFi.localIP().toString()+"&ssid=" + ssid;
GetPostData("/VideoAdvertising/Simplepages/GetVideo", device_status);
String msgString(Server_Messsage);
if (Success_Message == 1)
{
if (msgString == "Register")//Nếu chưa đăng ký
{
//Register
GetPostData("/VideoAdvertising/Simplepages/VerifyAddEdit_device", "MACAddress=" + Macaddress);
}
else
{
// GET AND SAVE DATA
print_in_debug("Saved Data Debug " + Data_Message);
lcd_helper.PrintLCD(Data_Message);
} // END LOAD DATA TO LCD
}// END LOAD DATA TO SCREEN
else // Không thể kết nối Server
{
print_in_debug("SERVER MAINTAINING.<br>Load DataMessage From Memeroy");
Load_Default_Data();
}
}// END LAY DU LIEU
void GetPostData(String url, String PostData)
{
// Func sumbit POST get respond tu Server
// This will send the request to the server
client.println("POST " + url + " HTTP/1.1");
client.println("Host: " + host);
client.println("Cache-Control: no-cache");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
delay(500);
//Serial.println (host+ url);
String section = "header";
// Read all the lines of the reply from server and print them to Serial
int json_size;
while (client.available()) {
String line = client.readStringUntil('\r');
int header_json;
if (section == "header") {
for (header_json = 0; header_json < 6; header_json++) {
if (line.substring(header_json, header_json + 1) == "{")
{
section = "json";
line = line.substring(header_json, line.length());
break;
}
}
//Serial.println ("section " + section + " Line " + line);
}
if (section == "json") { // print the good stuff
section = "ignore";
json_size = line.length() + 1;// Tuy vao json
Serial.println(line); // Hex
int size = json_size;
char json[size];
line.toCharArray(json, size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_parsed = jsonBuffer.parseObject(const_cast<char*>(json));
if (!json_parsed.success())
{
Serial.println("parseObject() failed");
Serial.println("Data : " + line);
break;
}
Server_Messsage = json_parsed["Message"].as<String>();;
Success_Message = json_parsed["Success"];
print_in_debug("Data Status :" + Server_Messsage);
if (sizeof(json_parsed["Content"]) > 0)
{
Data_Message = json_parsed["Content"].as<String>();
if (Data_Message != Last_Data_Message)
{
Last_Data_Message = Data_Message;
Default_Data = Data_Message;
memory_helper.Write_Lastest_Data(Last_Data_Message);
print_in_debug("Write Default Data");
}
}
}
}
}// END Func sumbit POST get respond tu Server
void Load_Default_Data()
{
if(Data_Message.length() == 0)
{
print_in_debug("Don't have default data");
}
lcd_helper.PrintLCD(Default_Data);
}
boolean cmdStartsWith(const char *st) { // checks if cmd starts with st
for (int i = 0; ; i++) {
if (st[i] == 0) return true;
if (cmd[i] == 0) return false;
if (cmd[i] != st[i]) return false;;
}
return false;
}
void exeCmd() { // executes the command from cmd
lastCmdTime = millis();
if ( cmdStartsWith("swf") ) {
String data;
data = String(cmd);
memory_helper.Write_Wifi_Configuration(data);
for( int i = 0; i < sizeof(data); ++i )
{
cmd[i] = (char)0;
}
lcd_helper.PrintLCD("Write...<br>Wifi Configuration<br>Successful!");
print_in_debug("Saved: " + data);
delay(2000);
}
if ( cmdStartsWith("sdt") ) {
int ch = cmd[2] - '0';
int i;
String data;
for (i = 4; i <= cmdIndex; i ++)
{
//data = String(cmd[i], cmdIndex);
}
data = String(cmd);
data = data.substring(3, data.length());
memory_helper.Write_Lastest_Data(data);
for( int i = 0; i < sizeof(data); ++i )
{
cmd[i] = (char)0;
}
lcd_helper.PrintLCD("Write...<br>Default Data<br>Successful!");
print_in_debug("Saved: " + data);
delay(2000);
}
if ( cmdStartsWith("ssv") ) {
int ch = cmd[2] - '0';
int i;
String data;
for (i = 4; i <= cmdIndex; i ++)
{
//data = String(cmd[i], cmdIndex);
}
data = String(cmd);
data = data.substring(3, data.length());
memory_helper.Write_Host_Configuration(data);
for( int i = 0; i < sizeof(data); ++i )
{
cmd[i] = (char)0;
}
lcd_helper.PrintLCD("Write...<br>Server Configuration<br>Successful!");
print_in_debug("Saved: " + data);
delay(2000);
}
}
String macToStr(const uint8_t* mac)
{
String result;
String atom;
for (int i = 0; i < 6; ++i) {
atom = String(mac[i], 16);
if ( atom.length() < 2)
{
result += String ("0") + atom;
}
else
{
result += atom;
}
if (i < 5)
result += ':';
}
return result;
}
void print_in_debug(String msg)
{
if (Debug_Mode_Live == true)
{
lcd_helper.PrintLCD(msg);
delay(2000);
Serial.println(msg);
}
}