-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim800_rtos_test.ino
590 lines (439 loc) · 13.5 KB
/
sim800_rtos_test.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
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
#include <stdlib.h>
#include <HardwareSerial.h>
#include <string.h>
// Use only core 1 for demo purposes
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
// Creating objects for serials
HardwareSerial Sim800(2); // Hardware UART2
HardwareSerial DummyRf(1); // Hardware UART1
// ****************************************************************************
// Macros
#define LED_PIN 0 // For blink task
#define LED2_PIN 4 // To test something
#define MAX_SIZE_UART_RX_BUFFER 256 // For uart
#define MAX_SIZE_UART_TX_BUFFER 256 // For uart
#define MAX_SIZE_RF_RX_BUFFER 256 // For uart
#define MAX_SIZE_SERIAL_RX_BUFFER 256 // For uart
// *****************************************************************************
// Global variables
char gsm_rx_buffer[MAX_SIZE_UART_RX_BUFFER];
char gsm_tx_buffer[MAX_SIZE_UART_TX_BUFFER];
char rf_rx_buffer[MAX_SIZE_RF_RX_BUFFER]; // Uart rx buffer for dummy rf module
char serial_rx_buffer[MAX_SIZE_SERIAL_RX_BUFFER];
char balance[150];
char http_parsed_data[150];
volatile uint16_t gsm_rx_index = 0;
volatile char gsm_incoming_byte;
volatile uint16_t rf_rx_index = 0;
volatile char rf_incoming_byte;
volatile uint16_t serial_rx_index = 0;
volatile char serial_incoming_byte;
volatile uint8_t flag_OK_Status = 0;
volatile uint8_t flag_HTTP_Status_200 = 0;
volatile uint8_t flag_HTTP_Get_New_Data = 0;
volatile uint8_t flag_HTTP_Unread_Data = 0;
// ****************************************************************************
// Global Functions
uint8_t gsm_end_of_data(unsigned char data) // detect "end of line" for sim800
{
volatile static uint8_t cr_flag = 0;
volatile static uint8_t data_step = 0;
data_step++;
if (data == '\r') {
cr_flag = 1;
data_step = 1;
}
if ((data_step == 2) && (cr_flag = 1) && (data == '\n')) //CRLF came and there is nothing between them
{
data_step = 0;
cr_flag = 0;
return 1;
}
else {
return 0;
}
}
void sim800_init() {
uint8_t attempts_count = 0;
uint8_t dont_continue = 0;
vTaskDelay(7000 / portTICK_PERIOD_MS);
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("ATE1\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during ATE1");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+CMGF=1\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during AT");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+CNMI=1,2,0,0,0\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during CNMI");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+SAPBR=3,1,\"Contype\", \"GPRS\"\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during SAPBR");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+SAPBR=3,1,\"APN\",\"internet\"\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during SAPBR");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
// No need to check
Sim800.print("AT+SAPBR=1,1\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
flag_OK_Status = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
Sim800.print("AT+SAPBR=2,1\r\n");
do {
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 50) {
dont_continue = 1;
Serial.println("Error during getting IP");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
if (dont_continue == 1) {
Serial.println("Failed to init sim800");
}
}
//-------
void http_get_request(char *buffer, char *card_number) {
char url[150] = { "https://www.azadvending.com/NestleJo/api/rfid/verifycard/" };
uint8_t attempts_count = 0;
uint8_t dont_continue = 0;
strcat(url, card_number);
Sim800.print("AT+HTTPTERM\r\n");
vTaskDelay(100 / portTICK_PERIOD_MS);
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+HTTPINIT\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during HTTP Init");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+HTTPSSL=1\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
dont_continue = 1;
Serial.println("Error during HTTP SSL");
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+HTTPPARA=\"CID\",1\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
Serial.println("Error during HTTP Parameter");
dont_continue = 1;
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+HTTPPARA=\"URL\",\"");
Sim800.print(url);
Sim800.print("\"\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
Serial.println("Error during HTTP URL Parameter");
dont_continue = 1;
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
if (dont_continue != 1) {
flag_HTTP_Status_200 = 0;
Sim800.print("AT+HTTPACTION=0\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
flag_OK_Status = 0;
do {
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
Serial.println("Error during HTTP Action");
dont_continue = 1;
break;
}
} while (flag_HTTP_Status_200 == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
Sim800.print("AT+HTTPREAD\r\n");
do {
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 100) {
Serial.println("Error during HTTP READ");
dont_continue = 1;
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
flag_OK_Status = 0;
if (dont_continue != 1) {
do {
Sim800.print("AT+HTTPTERM\r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
attempts_count++;
if (attempts_count >= 10) {
Serial.println("Error during HTTP Term");
dont_continue = 1;
break;
}
} while (flag_OK_Status == 0);
}
attempts_count = 0;
if (dont_continue == 1) {
Serial.println("HTTP Request Error");
}
else {
strcpy(buffer, http_parsed_data);
}
}
// ****************************************************************************
//*****************************************************************************
// Tasks
// Blink LED Task to test the tasks' stability
void toggleLED(void *parameter) {
while (1) {
digitalWrite(LED_PIN, HIGH);
vTaskDelay(1000 / portTICK_PERIOD_MS);
digitalWrite(LED_PIN, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void checkSim800(void *parameters) {
while (1) {
if (Sim800.available() > 0) {
gsm_incoming_byte = Sim800.read();
if (gsm_rx_index >= MAX_SIZE_UART_RX_BUFFER - 1) // Prevent buffer overflow
{
gsm_rx_index = 0;
}
if (gsm_end_of_data(gsm_incoming_byte)) {
if (flag_HTTP_Get_New_Data == 1) {
strcpy(http_parsed_data, gsm_rx_buffer);
flag_HTTP_Get_New_Data = 0;
flag_HTTP_Unread_Data = 1;
}
if (strstr((char *)gsm_rx_buffer, "OK")) {
flag_OK_Status = 1;
digitalWrite(LED2_PIN, HIGH);
gsm_rx_index = 0;
memset((char *)gsm_rx_buffer, '\0', sizeof(gsm_rx_buffer));
}
if (strstr((char *)gsm_rx_buffer, "HTTPACTION: 0,200")) {
flag_HTTP_Status_200 = 1;
digitalWrite(LED2_PIN, HIGH);
gsm_rx_index = 0;
memset((char *)gsm_rx_buffer, '\0', sizeof(gsm_rx_buffer));
}
if (strstr((char *)gsm_rx_buffer, "+HTTPREAD:")) {
flag_HTTP_Get_New_Data = 1;
gsm_rx_index = 0;
memset((char *)gsm_rx_buffer, '\0', sizeof(gsm_rx_buffer));
}
// If the end of line came but no meaningful data
gsm_rx_index = 0;
memset((char *)gsm_rx_buffer, '\0', sizeof(gsm_rx_buffer));
}
// If it isn't the end of line, keep adding to buffer
else {
gsm_rx_buffer[gsm_rx_index] = gsm_incoming_byte;
gsm_rx_index++;
}
}
}
}
void checkRfModule(void *parameters) {
while (1) {
if (DummyRf.available() > 0) {
rf_incoming_byte = DummyRf.read();
if (rf_rx_index >= MAX_SIZE_RF_RX_BUFFER - 1) // Prevent buffer overflow
{
rf_rx_index = 0;
}
if (rf_incoming_byte == '\n') {
rf_rx_index = 0;
memset((char *)rf_rx_buffer, '\0', sizeof(rf_rx_buffer));
}
// If it isn't end of line, keep adding to buffer
else {
rf_rx_buffer[rf_rx_index] = rf_incoming_byte;
rf_rx_index++;
}
}
}
}
void checkSerial(void *parameters) {
while (1) {
if (Serial.available() > 0) {
serial_incoming_byte = Serial.read();
if (serial_rx_index >= MAX_SIZE_SERIAL_RX_BUFFER - 1) // Prevent buffer overflow
{
serial_rx_index = 0;
}
if (serial_incoming_byte == '\n') {
if (strstr((char *)serial_rx_buffer, "trigger")) {
Serial.println("http get request trigger");
digitalWrite(LED2_PIN, LOW);
http_get_request(balance, "123456");
if (flag_HTTP_Unread_Data == 1) {
Serial.println("New data has been read");
Serial.println(balance);
vTaskDelay(100 / portTICK_PERIOD_MS);
flag_HTTP_Unread_Data = 0;
}
serial_rx_index = 0;
memset((char *)serial_rx_buffer, '\0', sizeof(serial_rx_buffer));
}
serial_rx_index = 0;
memset((char *)serial_rx_buffer, '\0', sizeof(serial_rx_buffer));
}
// If it isn't end of line, keep adding to buffer
else {
serial_rx_buffer[serial_rx_index] = serial_incoming_byte;
serial_rx_index++;
}
}
}
}
//*****************************************************************************
// Main
void setup() {
// Configure pin
pinMode(LED_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
// Configure serials and wait a second
Serial.begin(115200);
Sim800.begin(9600, SERIAL_8N1, 16, 17); // RX - TX
DummyRf.begin(9600, SERIAL_8N1, 32, 33); // RX - TX
// BaudRate values can be changed.
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println("Sim800 FreeRTOS Demo");
xTaskCreatePinnedToCore(
toggleLED, // Function to be called
"Toggle LED", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass
1, // Task priority
NULL, // Task handle
app_cpu); // Run on one core for demo purposes (ESP32 only)
xTaskCreatePinnedToCore(
checkSim800, // Function to be called
"Check Sim800", // Name of task
2048, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass
1, // Task priority (must be same to prevent lockup)
NULL, // Task handle
app_cpu); // Run on one core for demo purposes (ESP32 only)
xTaskCreatePinnedToCore(
checkRfModule, // Function to be called
"Check Rf", // Name of task
2048, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass
1, // Task priority (must be same to prevent lockup)
NULL, // Task handle
app_cpu); // Run on one core for demo purposes (ESP32 only)
xTaskCreatePinnedToCore(
checkSerial, // Function to be called
"Check Serial", // Name of task
2048, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass
1, // Task priority (must be same to prevent lockup)
NULL, // Task handle
app_cpu); // Run on one core for demo purposes (ESP32 only)
sim800_init();
vTaskDelay(100 / portTICK_PERIOD_MS);
// Delete "setup and loop" task
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! This is really important !!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// ||
// \/
vTaskDelete(NULL);
}
void loop() {
// Execution should never get here
}