-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_8266_wifi.ino
354 lines (285 loc) · 8.01 KB
/
esp_8266_wifi.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
String data = String("");
String data_out = String("");
// MESSAGE PARTS
const char TRUE = '1';
const char FALSE = '0';
const char SEPARATOR = ' ';
const char GET = 'G';
const char POST = 'P';
// COMMANDS
const char WIFI_CONNECT = 'a';
const char IS_CONNECTED = 'c';
const char WIFI_DISCONNECT = 'd';
const char GET_NETWORKS = 'g';
const char READ_NETWORKS = 'r';
const char CONFIGURE_REQUEST = 'u';
const char GET_CONFIGURATION = 'v';
const char SEND_REQUEST = 's';
const char GET_RESPONSE = 't';
const char GET_RESPONSE_CODE = 'o';
const char ECHO = 'z';
// FOR TIMER
const int TIMER_MULTIPLIER = 50;
const int SERIAL_WAIT = 1e4; // micros
const int TIMER_DELAY = 1e3; // micros
unsigned int last_serial = 0;
void ICACHE_RAM_ATTR onTimerISR() { timerEvent(); }
// DATA
String wifi_connect_data = String();
int wifi_connect_needed = 0;
int read_networks_needed = 0;
String read_networks_result = String();
char request_type = 0;
String request_url = String();
String request_body = String();
int request_needed = 0;
int response_code = 0;
String response = String();
void setup() {
// LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Serial
Serial.begin(9600);
delay(10);
WiFi.begin();
delay(10);
// Timer
timer1_attachInterrupt(onTimerISR);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
timer1_write(TIMER_DELAY * TIMER_MULTIPLIER);
}
void loop() {
wifi_connect();
read_networks();
send_request();
}
void timerEvent() {
if (Serial.available()) {
digitalWrite(LED_BUILTIN, LOW);
char data_byte = Serial.read();
data.concat(data_byte);
last_serial = micros();
}
unsigned int time = micros();
if (last_serial && time - last_serial > SERIAL_WAIT) {
digitalWrite(LED_BUILTIN, HIGH);
if (data.length() > 0) {
process_serial();
data = String();
}
}
timer1_write(TIMER_DELAY * TIMER_MULTIPLIER);
}
void process_serial() {
char com = data.charAt(0);
data_out = String();
switch(com) {
case WIFI_CONNECT: serial_wifi_connect(); break;
case IS_CONNECTED: serial_is_connected(); break;
case WIFI_DISCONNECT: serial_wifi_disconnect(); break;
case GET_NETWORKS: serial_get_networks(); break;
case READ_NETWORKS: serial_read_networks(); break;
case CONFIGURE_REQUEST: serial_configure_request(); break;
case GET_CONFIGURATION: serial_get_configuration(); break;
case SEND_REQUEST: serial_send_request(); break;
case GET_RESPONSE: serial_get_response(); break;
case GET_RESPONSE_CODE: serial_get_response_code(); break;
case ECHO: serial_echo(); break;
}
if (data_out.length() > 0) {
Serial.print(data_out);
}
}
// FUNCTIONS FOR SERIAL RESPONSE
void serial_is_connected() {
// Output [IS_CONNECTED] [TRUE/FALSE]
int connected = WiFi.status() == WL_CONNECTED;
data_out.concat(IS_CONNECTED);
data_out.concat(connected ? TRUE : FALSE);
}
void serial_get_networks() {
// Output [GET_NETWORKS] [number] [[TRUE/FALSE - with password] [NETWORK_NAME] [SEPARATOR]]
int ready = !read_networks_needed && read_networks_result.length() > 0;
if (ready) {
data_out = read_networks_result;
}
else {
data_out.concat(GET_NETWORKS);
data_out.concat((byte) 0);
}
}
void serial_read_networks() {
// Output [READ_NETWORKS] [TRUE/FALSE read finised]
int ready = !read_networks_needed && read_networks_result.length() > 0;
data_out.concat(READ_NETWORKS);
data_out.concat(ready ? TRUE : FALSE);
read_networks_needed = true;
}
void serial_wifi_connect() {
// Input [ssid] [SEPARATOR] [password] [SEPARATOR]
// Output [WIFI_CONNECT]
wifi_connect_data = data;
wifi_connect_needed = 1;
data_out.concat(WIFI_CONNECT);
}
void serial_wifi_disconnect() {
// Output [WIFI_DISCONNECT]
WiFi.persistent(false);
WiFi.disconnect(true);
data_out.concat(WIFI_DISCONNECT);
}
void serial_configure_request() {
// Input [CONFIGURE_REQUEST] [POST / GET] [url]
// Output [CONFIGURE_REQUEST] [TRUE/FALSE - success]
data_out.concat(CONFIGURE_REQUEST);
if (request_needed) {
data_out.concat(FALSE); return;
}
char type = 0;
if (data.length() > 1)
type = data.charAt(1);
if (type != GET && type != POST) {
data_out.concat(FALSE); return;
} else {
request_type = type;
}
request_url = String();
for (int i = 2; i < data.length(); i++) {
request_url.concat(data.charAt(i));
}
data_out.concat(TRUE);
}
void serial_get_configuration() {
// Output [GET_CONFIGURATION] [POST / GET] [url]
data_out.concat(GET_CONFIGURATION);
data_out.concat((char) request_type);
for (int i = 0; i < request_url.length(); i++) {
data_out.concat(request_url.charAt(i));
}
}
void serial_send_request() {
// Input [SEND_REQUEST] [body]
// Output [SEND_REQUEST] [TRUE/FALSE success]
data_out.concat(SEND_REQUEST);
// if a request is already being sent
if (request_needed) {
data_out.concat(FALSE);
return;
}
request_body = String();
for (int i = 1; i < data.length(); i++) {
request_body.concat(data.charAt(i));
}
request_needed = 1;
response = String();
response_code = 0;
data_out.concat(TRUE);
}
void serial_get_response() {
// Output: [GET_REPONSE] [TRUE/FALSE response present] [response]
data_out.concat(GET_RESPONSE);
data_out.concat(response_code == 0 ? FALSE : TRUE);
data_out.concat(response);
}
void serial_get_response_code() {
// Output: [CODE as characters, 0 - if none]
data_out.concat(GET_RESPONSE_CODE);
data_out.concat(response_code);
}
void serial_echo() {
// Output: [ECHO]
data_out.concat(ECHO);
}
// FUNCTIONS FOR EXECUTION INSIDE LOOP
void wifi_connect() {
if (!wifi_connect_needed) return;
String ssid = String("");
String password = String("");
int i; // skip first char
for (i = 1; i < wifi_connect_data.length(); ++i) {
if (wifi_connect_data.charAt(i) == SEPARATOR) { ++i; break; }
ssid.concat(wifi_connect_data.charAt(i));
}
for (i = i; i < wifi_connect_data.length(); ++i) {
if (wifi_connect_data.charAt(i) == SEPARATOR) { ++i; break; }
password.concat(wifi_connect_data.charAt(i));
}
const char* c_ssid = ssid.c_str();
const char* c_password = password.c_str();
wifi_disconnect();
// no password
if (password.length() == 0) {
WiFi.begin(c_ssid);
} else {
WiFi.begin(c_ssid, c_password);
}
delay(10);
wifi_connect_needed = 0;
}
void read_networks() {
if (!read_networks_needed) return;
String result = String("");
wifi_disconnect();
int n = WiFi.scanNetworks();
result.concat(GET_NETWORKS);
result.concat((byte) n);
for (int i = 0; i < n; i++) {
// has password or not
result.concat(WiFi.encryptionType(i) == ENC_TYPE_NONE ? FALSE : TRUE);
// hotspot name
String ssid = WiFi.SSID(i);
result.concat(ssid);
result.concat(SEPARATOR);
}
// reconnect back
WiFi.begin();
read_networks_result = result;
read_networks_needed = 0;
}
void send_request() {
if (!request_needed) return;
if (request_url.length() < 1 || (request_type != GET && request_type != POST)) {
request_needed = 0;
return;
}
const char* url = request_url.c_str();
HTTPClient http;
http.begin(url);
if (request_type == GET) {
response_code = http.GET();
}
else if (request_type == POST) {
const char* body = request_body.c_str();
response_code = http.POST(body);
}
response = http.getString();
http.end();
request_needed = 0;
}
// HELPER FUNCIONS FOR SERIAL AND WIFI CONNECTION
void wifi_disconnect() {
WiFi.persistent(false);
WiFi.disconnect(true);
}
void wifi_send_data() {
const char* url = "http://kangaroosinantarctica.pythonanywhere.com/aquarium/data";
HTTPClient http;
char message[1000];
int i;
http.begin(url); //HTTP
int httpCode = http.POST(message);
http.end();
}
void serial_get_data() {
for (int i = 0; i < 1e4 && Serial.available() == 0; ++i) {}
delay(100);
data = String("");
int data_size = Serial.available();
for (int i = 0; i < data_size; i++) {
byte new_byte = Serial.read();
data.concat((char) new_byte);
}
}