-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkController.cpp
313 lines (280 loc) · 8.68 KB
/
NetworkController.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
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
#include "Arduino.h"
#include "NetworkController.h"
#include "system.h"
//#include "WiFi.h"
#include <ESPmDNS.h>
#include <HTTPClient.h>
NetworkController::NetworkController()
{
this->connected = false;
}
NetworkController::NetworkController(char* ssid, char* password)
{
this->ssid = ssid;
this->password = password;
this->connected = false;
this->connected_mqtt = false;
}
NetworkController::NetworkController(char* ssid, char* password, char* endpoint)
{
this->ssid = ssid;
this->password = password;
this->endpoint = endpoint;
this->connected = false;
this->connected_mqtt = false;
}
bool NetworkController::connect() {
if (!this->connected) {
int count = 0;
int ip_count = 0;
// it wil set the static IP address to 192, 168, 1, 1
IPAddress null_IP(0, 0, 0, 0);
IPAddress ip = null_IP;
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
// waitForConnectResult() != WL_CONNECTED;
while (WiFi.waitForConnectResult() != WL_CONNECTED && ip == null_IP && count < CONNECTION_TRY_LIMIT) { //Check for the connection
switch (WiFi.status()) {
case WL_CONNECTED:
Serial.println("Connection Success");
break;
case WL_IDLE_STATUS:
Serial.println("Idle Waiting Connection");
break;
case WL_NO_SSID_AVAIL:
Serial.println("No SSID Available");
break;
case WL_SCAN_COMPLETED:
Serial.println("Scan Completed");
break;
case WL_CONNECT_FAILED:
Serial.println("Connection Failed");
break;
case WL_CONNECTION_LOST:
Serial.println("Connection Lost");
break;
case WL_DISCONNECTED:
Serial.println("Connetion Disconnected");
count = 4; // trigger wifi reset
break;
}
++count;
ip = WiFi.localIP(); // this is used to update network
if(count == 1) {
Serial.print("Waiting for Connection...");
}
else {
// Serial.print(".");
delay(300);
}
if(count % 5 == 0) {
Serial.print("Reseting WiFi...");
WiFi.disconnect();
delay(2000);
WiFi.begin(ssid, password);
delay(2000);
ip = WiFi.localIP(); // used to update network
}
}
// Check if WiFi was successful
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to the WiFi network");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
this->connected = true;
return true;
}
else {
Serial.println("Failed to Connect to WiFi!");
return false;
}
}
return true;
}
bool NetworkController::disconnect() {
WiFi.disconnect(); //Flush WiFi
delay(300);
Serial.println(WiFi.status());
// this->client.disconnect(); //Disconnect mqtt
return true;
}
bool NetworkController::setup() {
WiFi.disconnect(); //Flush WiFi
return true;
}
// bool NetworkController::setup_mqtt(char* server) {
// WiFiClient wifi_client;
// this->mqtt_server = server;
// this->client.setClient(wifi_client);
// this->client.setServer(server, 1883);
// return true;
// }
bool NetworkController::send_http(String data) {
Serial.println("Sending readings to server...");
// Block until we are able to connect to the WiFi access point
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(this->endpoint);
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
int send_attempts = 0;
// Try to post the data up to three times
int httpResponseCode = 0;
bool sent_successfully = false;
while(send_attempts < 3 && httpResponseCode != 200) {
httpResponseCode = http.POST(data);
send_attempts++;
if(httpResponseCode>0) {
String response = http.getString();
Serial.print("Response: ");
Serial.println(httpResponseCode);
Serial.println(response);
if (httpResponseCode == 200) {
sent_successfully = true;
}
}
else {
Serial.printf("Error occurred while sending HTTP POST: %s\n", http.errorToString(httpResponseCode).c_str());
delay(1500);
}
}
// Add failed request data to array
if(!sent_successfully) {
Serial.println("Failed to Send to Database!");
http.end();
return false;
}
http.end();
return true;
}
else {
this->connected = false;
}
}
// bool NetworkController::send_http(DynamicJsonDocument data) {
// Serial.println("Sending readings to server...");
// // Block until we are able to connect to the WiFi access point
// if(WiFi.status() == WL_CONNECTED){
// HTTPClient http;
// http.begin(this->endpoint);
// http.addHeader("Content-Type", "application/json");
// http.addHeader("Accept", "application/json");
// //PACKAGE DATA
// String requestBody;
// serializeJson(data, requestBody);
// int send_attempts = 0;
// // Try to post the data up to three times
// int httpResponseCode = 0;
// bool sent_successfully = false;
// while(send_attempts < 3 && httpResponseCode != 200) {
// httpResponseCode = http.POST(requestBody);
// send_attempts++;
// if(httpResponseCode>0) {
// String response = http.getString();
// Serial.print("Response: ");
// Serial.println(httpResponseCode);
// Serial.println(response);
// if (httpResponseCode == 200) {
// sent_successfully = true;
// }
// }
// else {
// Serial.printf("Error occurred while sending HTTP POST: %s\n", http.errorToString(httpResponseCode).c_str());
// delay(1500);
// }
// }
// // Add failed request data to array
// if(!sent_successfully) {
// Serial.println("Failed to Send to Database!");
// http.end();
// return false;
// }
// http.end();
// return true;
// }
// else {
// this->connected = false;
// }
// }
// bool NetworkController::connect_mqtt() {
// if (!this->connected_mqtt) {
// int attempts = 0;
// while (!this->client.connected() && attempts <= 10) {
// ++attempts;
// Serial.print("Connecting MQTT...");
// // Attempt to connect
// const char* client_name = "MudPi_" + DEVICE_ID;
// if (this->client.connect(client_name)) {
// Serial.println("Success");
// // Subscribe
// // this->client.subscribe(this->topic);
// this->connected_mqtt = true;
// } else {
// Serial.print("Failed, rc=");
// Serial.println(this->client.state());
// delay(500);
// this->connected_mqtt = false;
// }
// }
// if(!this->connected_mqtt) {
// Serial.println('MQTT Failed to Connect');
// return false;
// }
// }
// return true;
// }
// bool NetworkController::send_mqtt(char* topic, String data) {
// Serial.print("Sending readings to MQTT server...");
// if(this->connected_mqtt) {
// int send_attempts = 0;
// // Try to post the data up to three times
// bool sent_successfully = false;
// while(send_attempts < 3 && ! sent_successfully) {
// sent_successfully = this->client.publish(topic, data.c_str());
// send_attempts++;
// if(sent_successfully) {
// Serial.println("Success! ");
// }
// else {
// Serial.println("Failed");
// delay(1000);
// }
// }
// // Add failed request data to array
// if(!sent_successfully) {
// Serial.println("Failed to Send to MQTT Server!");
// return false;
// }
// return true;
// }
// return false;
//}
// bool NetworkController::send_mqtt(char* topic, DynamicJsonDocument data) {
// Serial.print("Sending readings to MQTT server...");
// if(this->connected_mqtt) {
// int send_attempts = 0;
// // Try to post the data up to three times
// bool sent_successfully = false;
// //PACKAGE DATA
// char* jsonString;
// serializeJson(data, jsonString);
// while(send_attempts < 3 && ! sent_successfully) {
// sent_successfully = this->client.publish(topic, jsonString);
// send_attempts++;
// if(sent_successfully) {
// Serial.println("Success! ");
// }
// else {
// Serial.println("Failed");
// delay(1000);
// }
// }
// // Add failed request data to array
// if(!sent_successfully) {
// Serial.println("Failed to Send to MQTT Server!");
// return false;
// }
// return true;
// }
// return false;
// }