-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiManager.ino
169 lines (151 loc) · 3.75 KB
/
wifiManager.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
boolean connectToWifiNetwork(){
digitalWrite(SS_ETHERNET, LOW); //Enable WIFI
if (WiFi.status() == WL_NO_SHIELD) {
return false;
}
while (status != WL_CONNECTED) {
status = WiFi.begin((char*)ssid, pass);
delay(10000);
}
return true;
}
boolean isWifiClientAvailable(){
if(socketTimer == 0){
return true;
}
return false;
}
void forceSocketClose(){
socketTimer = 0;
client.flush();
client.stop();
}
void updateSocketTimer(){
if((millis() - socketTimer) > SOCKET_TIMER && socketTimer > 0){
Serial.println(F("Socket TimeOut"));
socketTimer = 0;
client.flush();
client.stop();
}
}
void createClientRequest(String query){
client.print("GET "+WEBSERVICE_URL);
client.print(query);
client.print(HTTP_REQUEST);
}
boolean configRequestOverWifi(String mn){
if(isWifiClientAvailable()){
if(!client.connected()){
if (client.connect(server, 80)) {
socketTimer = millis();
String q= "?mn="+mn+"&cfg";
createClientRequest(q);
return checkServerResponse();
}
}
}
return false;
}
boolean selectLEDOverWifi(String mn, unsigned short id){
if(isWifiClientAvailable()){
if(!client.connected()){
if (client.connect(server, 80)) {
socketTimer = millis();
String q= "?mn="+mn+"&led="+String(id);
createClientRequest(q);
return checkServerResponse();;
}
}
}
return false;
}
boolean executeServiceOverWifi(String mn,unsigned long id){
if(isWifiClientAvailable()){
if(!client.connected()){
if (client.connect(server, 80)) {
socketTimer = millis();
String q ="?mn="+mn+"&srv="+String(id);
createClientRequest(q);
return checkServerResponse();
}
}
}
return false;
}
boolean checkServerResponse(){
int connectLoop = 0;
boolean dataTr = false;
//while(connectLoop <= 10000 && dataTr==false){
while(client.connected() && dataTr==false){
while(client.available()){
dataTr = true;
char c = client.read();
connectLoop = 0;
//Body part of HTTP response start
#if defined(WITH_SERIAL)
Serial.print(c);
#endif
if(body == 1 && c !='\r' && c!='\n'){
//Check if it is JSON
if(c == '{'){
jsonStarted = true;
}
if (jsonStarted==true){
buf[strInd] = c;
strInd++;
//Ajouter le controle de l'overflow
if(strInd >= RCV_BUFF_SIZE){
#if defined(WITH_SERIAL)
Serial.println(F("Buf Overflow"));
#endif
jsonStarted = false;
stringComplete = false;
strInd = 0;
body = 0;
forceSocketClose();
return false;
}
}
}
//Flag telling we enter body part of HTTP response
if(t=='\n' && c=='\r'){
body = 1;
}
t = c;
}
connectLoop++;
if(connectLoop > 10000)
{
// then close the connection from this end.
#if defined(WITH_SERIAL)
Serial.println(F("Server Timeout"));
#endif
strInd = 0;
jsonStarted = false;
stringComplete = false;
body = 0;
forceSocketClose();
return false;
}
//delay(1);
}
//Last '\n' is end of JSON
if (jsonStarted==true){
buf[strInd] = '\0';
strInd = 0;
stringComplete = true;
jsonStarted = false;
body = 0;
forceSocketClose();
return true;
}
#if defined(WITH_SERIAL)
Serial.println(F("Server Disconnected"));
#endif
jsonStarted = false;
stringComplete = false;
strInd = 0;
body = 0;
forceSocketClose();
return false;
}