-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathesp8266.c
758 lines (649 loc) · 17.9 KB
/
esp8266.c
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#include "esp8266.h"
#include <string.h>
#include <stdio.h>
Operation op;
Type t;
ESP8266_Status s = STATUS_NOT_WORKING;
const char ok_answer[] = "OK\r\n";
//const size_t ok_answer_length = strlen(ok_answer);
const char ready_answer[] = "ready\r\n";
//const size_t ready_answer_length = strlen(ready_answer);
const char error_answer[] = "ERROR\r\n";
//const size_t error_answer_length = strlen(error_answer);
const char at_inquiry[] = "AT\r\n";
const char at_rst[] = "AT+RST\r\n";
const char ate0[] = "ATE0\r\n";
const char ate1[] = "ATE1\r\n";
const char at_cifsr_t[] = "AT+CIFSR\r\n";
const char at_cwlap[] = "AT+CWLAP\r\n"; // List of Access Points
const char at_cwjap_i[] = "AT+CWJAP?\r\n"; // Conncted AP
const char at_close[] = "AT+CIPCLOSE\r\n"; // Close connetion
char *at_cwjap_se;
char *at_cwmode_se;
char *at_cipmux_se;
char *at_cipstart_se;
char *at_cipsend_se;
char *at_data_buffer;
u8* l_ready = NULL;
char* g_buffer = NULL;
Access_Point ap[20];
u8 ap_index = 0;
Protocol current_protocol = TCP;
char* connected_ap;
void esp8266_init(u8* line_ready) {
l_ready = line_ready;
g_buffer = usart_get_string();
// Reset and set mode 3
// esp8266_reset(&line_ready);
// esp8266_set_mode(3);
esp8266_check_presence();
// disable echo
esp8266_set_echo(false);
esp8266_wait_for_answer();
// enable echo
// esp8266_set_echo(true, &line_ready);
// esp8266_wait_for_answer(&line_ready);
esp8266_check_presence();
esp8266_wait_for_answer();
// get information (ESSID) about AP
// esp8266_get_connected_ap();
// esp8266_debug_print_connected_ap();
// check IP address (should work)
// esp8266_get_ip_addresses();
// esp8266_debug_print_ip_address();
// get list of Access Points
// esp8266_get_list_of_aps();
// esp8266_debug_print_list_of_aps();
// esp8266_check_presence(&line_ready);
// Setup module to work in single connection mode
esp8266_connection_mode(0);
esp8266_check_presence();
esp8266_wait_for_answer();
}
u8 parse_ok(void) {
char *pos = strstr(g_buffer, ok_answer);
if (pos == 0) {
return 1; // No match
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
return 0;
}
u8 parse_error(void) {
char *pos = strstr(g_buffer, error_answer);
if (pos == 0) {
return 1; // No match
}
memset(g_buffer, 0 , received_string_length);
usart_clear_string();
return 0;
}
u8 parse_ready(void) {
char *pos = strstr(g_buffer, ready_answer);
if (pos == 0) {
return 1; // No match
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
return 0;
}
// string - incoming buffer
u8 parse_AT(void) {
u8 ret = parse_ok();
return ret;
}
u8 parse_AT_RST(void) {
static int state = 0;
if (state == 0) {
if (parse_ok() == 0) {
state = 1;
}
} else if (state == 1) {
if (parse_ready() == 0) {
state = 2;
}
}
return state;
}
typedef enum {
cwlap_encryption = 0,
cwlap_essid = 1,
cwlap_signal = 2,
cwlap_mac = 3,
cwlap_channel = 4
} cwlap_state;
void parse_AT_CWLAP(void) {
// Response line should start with this string
static const char* cwlap = "+CWLAP:";
char *p = strstr(g_buffer, cwlap);
if (p == NULL) {
return;
}
// buffer for 40 chars
const u8 tmp_buffer_len = 40;
char tmp_buffer[tmp_buffer_len];
memset(tmp_buffer, '\0', tmp_buffer_len);
p += strlen(cwlap) + 1; //+CWLAP:(
u8 index = 0;
cwlap_state current_state = 0;
Access_Point current_ap;
while(*p != ')') {
if (*p == ',') { // each section is separated by comma
index++;
switch (current_state) {
case cwlap_encryption:
current_ap.encryption = atoi(tmp_buffer);
break;
case cwlap_essid:
current_ap.essid = malloc(index);
strncpy(current_ap.essid, tmp_buffer, index);
break;
case cwlap_signal:
current_ap.signal = atoi(tmp_buffer);
break;
case cwlap_mac:
current_ap.mac = malloc(index);
strncpy(current_ap.mac, tmp_buffer, index);
break;
case cwlap_channel:
current_ap.channel = atoi(tmp_buffer);
break;
}
current_state = (current_state + 1) % 5;
memset(tmp_buffer, '\0', tmp_buffer_len);
index = 0;
} else {
switch (current_state) {
case cwlap_essid:
case cwlap_mac:
if (*p != '"') {
tmp_buffer[index] = *p;
} else {
--index; // ignore " and keep index of tmp_buffer
}
break;
default:
tmp_buffer[index] = *p;
break;
}
++index;
}
++p;
}
ap[ap_index++] = current_ap;
u8 new_length = p - g_buffer;
memmove(g_buffer, p, new_length);
usart_clear_string();
}
void parse_AT_CWJAP(void) {
// Response is in format: +CWJAP:"AccessPointName"\r\nOK\r\n
static const char* cwjap = "+CWJAP:";
char *p = strstr(g_buffer, cwjap);
if (p == NULL) {
return;
}
p += strlen(cwjap) + 1; //+CWJAP:"
// Find next " char
char *q = strstr(p, "\"");
if (q == NULL) {
return;
}
// Text between " is name of current AP
u8 diff = q - p;
connected_ap = calloc(diff + 1, sizeof(char));
strncpy(connected_ap, p, diff);
// Move rest of string
memmove(g_buffer, q, q - g_buffer);
usart_clear_string();
}
u8 parse_AT_CWMODE(void) {
char *pos = strstr(g_buffer, ok_answer);
if (pos == 0) {
// No match
char *no_change = "no change";
pos = strstr(g_buffer, no_change);
}
if (pos == 0) {
return 1;
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
return 0;
}
u8 parse_AT_CIPSTART(void) {
static u8 state = 0; // Two states, waiting for "OK" and waiting for "Linked"
// TODO: add error support
if (state == 0) {
// We can receive either OK or "link is builded"
if (parse_ok() == 0) { // ok found
state = 1;
} else {
static const char already_connect[] = "ALREAY CONNECT\r\n"; // srsly? connection was established earlier ie. before restart
char *p1 = strstr(g_buffer, already_connect);
if (p1 != 0) {
state = 2;
}
}
} else if (state == 1) {
static const char linked[] = "Linked\r\n";
char *p1 = strstr(g_buffer, linked);
if (p1 != 0) {
state = 2; // Matched
}
}
switch (state) {
case 0:
usart2_print("0");
break;
case 1:
usart2_print("1");
break;
case 2:
usart2_print("2");
break;
}
if (state == 2) {
memset(g_buffer, 0, received_string_length);
usart_clear_string();
state = 0;
return 2;
}
return state;
}
u8 parse_AT_CIPMUX(void) {
if (parse_ok() != 0) {
// no "ok", check is link is established already
char builded[] = "link is builded\r\n";
char *p1 = strstr(g_buffer, builded);
if (p1 == 0) {
return 1; // No match
} // else: "link is builded" is present, proceed
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
return 0;
}
u8 parse_AT_CIPSEND() {
static const char* prompt = "> ";
char *pos = strstr(g_buffer, prompt);
if (pos == NULL) {
return 1;
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
return 0;
}
u8 parse_AT_SEND_DATA() {
static const char* unlink = "Unlink\r\n";
static const char* send_ok = "SEND OK\r\n";
static u8 state = 0;
if (state == 0) {
char *pos = strstr(g_buffer, send_ok);
if (pos == NULL) {
if (parse_error() != 0) {
state = 0;
} else {
state = 1;
}
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
state = 1;
return state;
}
else if (state == 1) {
if (parse_ok() != 0) {
return state;
}
state = 2;
} else if (state == 2) {
char *pos = strstr(g_buffer, unlink);
if (pos == NULL) {
return state;
}
memset(g_buffer, 0, received_string_length);
usart_clear_string();
state = 3;
}
if (state == 2) {
state = 0;
return 2;
}
else if (state == 3) {
state = 0;
return 3;
}
return state;
}
void esp8266_send_command(Type type, Operation operation) {
if (s != STATUS_NOT_WORKING) {
// Operation in progress
// TODO: Add message to queue?
while (1) {}
}
op = operation;
t = type;
s = STATUS_WORKING;
switch (operation) {
case AT:
usart1_print(at_inquiry);
break;
case AT_RST:
usart1_print(at_rst);
break;
case ATE0:
usart1_print(ate0);
break;
case ATE1:
usart1_print(ate1);
break;
case AT_CIFSR:
if (t == TYPE_TEST) {
usart1_print(at_cifsr_t);
}
break;
case AT_CWLAP:
usart1_print(at_cwlap);
break;
case AT_CIPSERVER:
break;
case AT_CWJAP:
if (t == TYPE_INQUIRY) {
usart1_print(at_cwjap_i);
} else if (t == TYPE_SET_EXECUTE) {
usart1_print(at_cwjap_se);
}
break;
case AT_CWQAP:
break;
case AT_CWMODE:
if (t == TYPE_SET_EXECUTE) {
usart1_print(at_cwmode_se);
}
break;
case AT_CIPMUX:
if (t == TYPE_SET_EXECUTE) {
usart1_print(at_cipmux_se);
}
break;
case AT_CIPSTART:
if (t == TYPE_SET_EXECUTE) {
usart1_print(at_cipstart_se);
}
break;
case AT_CIPSEND:
if (t == TYPE_SET_EXECUTE) {
usart1_print(at_cipsend_se);
}
break;
case AT_SEND_DATA:
usart1_print(at_data_buffer);
break;
case AT_CLOSE:
usart1_print(at_close);
break;
}
}
void esp8266_parse_line(void) {
if (s != STATUS_WORKING) {
// Nothing to do
return;
}
u8 val;
switch (op) {
case AT:
if (parse_AT() != 0) {
usart2_print("-"); // error
} else {
usart2_print("+"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_RST:
val = parse_AT_RST();
if (val == 0) {
usart2_print("0"); // waiting
} else if (val == 1) {
usart2_print("1"); // reboot initialized
} else if (val == 2) {
usart2_print("2"); // success
s = STATUS_NOT_WORKING;
}
break;
case ATE0:
case ATE1:
if (parse_ok() != 0) {
usart2_print("q"); // error
} else {
usart2_print("Q"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_CIFSR:
if (parse_AT() != 0) {
usart2_print("e"); // error
} else {
usart2_print("E"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_CWLAP: // get list of access points
parse_AT_CWLAP();
if (parse_ok() != 0) {
usart2_print("w"); // error
} else {
usart2_print("W"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_CIPSERVER:
break;
case AT_CWJAP: // get connected ap
parse_AT_CWJAP();
if (parse_ok() != 0) {
usart2_print("o"); // error
} else {
usart2_print("O"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_CWQAP:
break;
case AT_CWMODE:
if (parse_AT_CWMODE() != 0) {
usart2_print("b"); // error
} else {
usart2_print("B"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_CIPMUX:
if (parse_AT_CIPMUX() != 0) {
usart2_print("x"); // error
} else {
usart2_print("X"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_CIPSTART:
val = parse_AT_CIPSTART();
if (val == 0) {
usart2_print("m1"); // waiting
} else if (val == 1) {
usart2_print("m2"); // OK received
if (current_protocol == UDP) {
s = STATUS_NOT_WORKING; // UDP is stateless
}
} else if (val == 2) {
usart2_print("M"); // Linked received
s = STATUS_NOT_WORKING;
}
break;
case AT_CIPSEND:
if (parse_AT_CIPSEND() != 0) {
usart2_print("u"); // error
} else {
usart2_print("U"); // success
s = STATUS_NOT_WORKING;
}
break;
case AT_SEND_DATA:
val = parse_AT_SEND_DATA();
if (val == 0) {
usart2_print("y1"); // waiting for end of transmission
} else if (val == 1) {
usart2_print("y2"); // sent successfully
} else if (val == 2) {
usart2_print("y3"); // echo received
s = STATUS_NOT_WORKING;
} else {
usart2_print("Y"); // unlinked
s = STATUS_NOT_WORKING;
}
break;
case AT_CLOSE:
if (parse_ok() != 0) {
usart2_print("h"); // error
} else {
usart2_print("H"); // success
s = STATUS_NOT_WORKING;
}
break;
}
}
void esp8266_wait_for_answer(void) {
u16 timeout = 1000;
while (s != STATUS_NOT_WORKING) {
if (*l_ready) {
esp8266_parse_line();
*l_ready = 0;
} else {
if (timeout-- == 0) {
usart2_print("\r\nTIMEOUT\r\n");
s = STATUS_NOT_WORKING;
}
}
__WFI();
}
}
ESP8266_Status esp8266_status(void) {
return s;
}
bool esp8266_check_presence(void) {
esp8266_send_command(TYPE_INQUIRY, AT);
esp8266_wait_for_answer();
return true;
}
void esp8266_reset(void) {
esp8266_send_command(TYPE_SET_EXECUTE, AT_RST);
esp8266_wait_for_answer();
}
void esp8266_set_echo(bool new_state) {
if (new_state == true) {
esp8266_send_command(TYPE_SET_EXECUTE, ATE1);
} else {
esp8266_send_command(TYPE_SET_EXECUTE, ATE0);
}
}
char* esp8266_get_ip_addresses() {
esp8266_send_command(TYPE_TEST, AT_CIFSR);
esp8266_wait_for_answer();
esp8266_wait_for_answer();
return "";
}
void esp8266_get_list_of_aps() {
esp8266_send_command(TYPE_SET_EXECUTE, AT_CWLAP);
esp8266_wait_for_answer();
}
void esp8266_get_connected_ap() {
esp8266_send_command(TYPE_INQUIRY, AT_CWJAP);
esp8266_wait_for_answer();
}
void esp8266_join_ap(char* ap, char* password) {
// prepare credentials:
char buffer[70];
sprintf(buffer, "AT+CWJAP=\"%s\",\"%s\"\r\n", ap, password);
at_cwjap_se = calloc(strlen(buffer), sizeof(char));
strcpy(at_cwjap_se, buffer);
esp8266_send_command(TYPE_SET_EXECUTE, AT_CWJAP);
esp8266_wait_for_answer();
free(at_cwjap_se);
}
void esp8266_set_mode(u8 mode) {
if ((mode > 3) || mode == 0) {
return; // Only 1, 2, 3
}
at_cwmode_se = calloc(16, sizeof(char));
sprintf(at_cwmode_se, "AT+CWMODE=%d\r\n", mode);
esp8266_send_command(TYPE_SET_EXECUTE, AT_CWMODE);
free(at_cwmode_se);
}
void esp8266_connection_mode(u8 mode) {
if ((mode != 1) && (mode != 0)) {
return; // Only 0 or 1
}
at_cipmux_se = calloc(16, sizeof(char));
sprintf(at_cipmux_se, "AT+CIPMUX=%d\r\n", mode);
esp8266_send_command(TYPE_SET_EXECUTE, AT_CIPMUX);
esp8266_wait_for_answer();
free(at_cipmux_se);
}
void esp8266_send_data(char* ip_address, u16 port, Protocol protocol, char* data, u8 length) {
// Establish connection with remote host
char buffer[256];
char *proto;
proto = calloc(4, sizeof(char));
if (protocol == TCP) {
strcpy(proto, "TCP");
} else if (protocol == UDP) {
strcpy(proto, "UDP");
}
current_protocol = protocol;
sprintf(buffer, "AT+CIPSTART=\"%s\",\"%s\",%u\r\n", proto, ip_address, port);
at_cipstart_se = calloc(strlen(buffer) + 1, sizeof(char));
strcpy(at_cipstart_se, buffer);
esp8266_send_command(TYPE_SET_EXECUTE, AT_CIPSTART);
esp8266_wait_for_answer();
// Send data
sprintf(buffer, "AT+CIPSEND=%u\r\n", length);
at_cipsend_se = calloc(strlen(buffer) + 1, sizeof(char));
strcpy(at_cipsend_se, buffer);
esp8266_send_command(TYPE_SET_EXECUTE, AT_CIPSEND);
esp8266_wait_for_answer();
at_data_buffer = calloc(length + 1, sizeof(u8));
strncpy(at_data_buffer, data, length);
usart2_print("+");
esp8266_send_command(TYPE_SET_EXECUTE, AT_SEND_DATA);
esp8266_wait_for_answer();
free(proto);
free(at_cipsend_se);
free(at_cipstart_se);
free(at_data_buffer);
}
void esp8266_close_connection(void) {
esp8266_send_command(TYPE_SET_EXECUTE, AT_CLOSE);
esp8266_wait_for_answer();
}
char debug_buffer[70];
void esp8266_debug_print_list_of_aps() {
u8 i = 0;
Access_Point tmp;
for (i = 0; i < ap_index; ++i) {
tmp = ap[i];
sprintf(debug_buffer, "Access Point %d\r\n", i);
usart2_print(debug_buffer);
sprintf(debug_buffer, "ESSID: %s\r\n", tmp.essid);
usart2_print(debug_buffer);
sprintf(debug_buffer, "MAC: %s\r\n", tmp.mac);
usart2_print(debug_buffer);
sprintf(debug_buffer, "Signal: %d\r\n", tmp.signal);
usart2_print(debug_buffer);
usart2_print("\r\n");
}
}
void esp8266_debug_print_connected_ap() {
sprintf(debug_buffer, "Current AP: %s\r\n", connected_ap);
usart2_print(debug_buffer);
}
void esp8266_debug_print_ip_address() {
}