O_Air724UG_AT指令巴法云MQTT函数


O_嵌入式专题目录


单独指令函数(C - FreeRtos)

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
uint8_t sendStep = 5;   //If connected IP before, start at step 5, do not have to initialize again, otherwise back to step 0
static uint8_t HttpGetLte(const char *urlGet)
{
uint8_t *data = (uint8_t*) malloc(RX_BUF_SIZE+1); //Save data from uart
uint8_t successFlag = 0;
uint8_t sleepTime = 0; //Delay seconds in every step, can be shorter if uart speed if faster
while(1)
{
if(sendStep == 0)
{
//Check 4G model
while(1)
{
Uart2SendData("AT\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT\r\n\r\nOK\r\n")) //Compare strings
sendStep = 1;
else
ESP_LOGI("HttpGetLte", "@0:%s", (char *)data);
break;
}
}
if(sleepTime) //Delay in every step, based on ms and multiply 1000, result is 'sleepTime' seconds
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
else if(sendStep == 1)
{
//Check SIM card
while(1)
{
Uart2SendData("AT+CPIN?\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CPIN?\r\n\r\n+CPIN: READY\r\n\r\nOK\r\n"))
sendStep = 2;
else
ESP_LOGI("HttpGetLte", "@1:%s", (char *)data);
break;
}
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
else if(sendStep == 2)
{
//Check regist
while(1)
{
Uart2SendData("AT+CGATT?\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CGATT?\r\n\r\n+CGATT: 1\r\n\r\nOK\r\n"))
sendStep = 3;
else
ESP_LOGI("HttpGetLte", "@2:%s", (char *)data);
break;
}
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
else if(sendStep == 3)
{
//Set APN
while(1)
{
Uart2SendData("AT+CSTT\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CSTT\r\n\r\nOK\r\n"))
sendStep = 4;
else if(strstr((char *)data, "AT+CSTT\r\n\r\n") && strstr((char *)data, "+CME ERROR"))
sendStep = 5;
else
ESP_LOGI("HttpGetLte", "@3:%s", (char *)data);
break;
}
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
else if(sendStep == 4)
{
//Active network
while(1)
{
Uart2SendData("AT+CIICR\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CIICR\r\n\r\nOK\r\n"))
sendStep = 5;
else if(strstr((char *)data, "AT+CIICR\r\n\r\n") && strstr((char *)data, "+CME ERROR"))
sendStep = 0;
else
ESP_LOGI("HttpGetLte", "@4:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 5)
{
//Check ip
while(1)
{
Uart2SendData("AT+CIFSR\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CIFSR\r\n\r\n") && !strstr((char *)data, "+CME ERROR"))
sendStep = 6;
else if(strstr((char *)data, "AT+CIFSR\r\n\r\n") && strstr((char *)data, "+CME ERROR"))
{
while(1)
{
Uart2SendData("AT+CIPSHUT\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CIPSHUT\r\n\r\nSHUT OK\r\n"))
sendStep = 3;
else
ESP_LOGI("HttpGetLte", "@5-1:%s", (char *)data);
break;
}
}
}
else
ESP_LOGI("HttpGetLte", "@5-2:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 6)
{
while(1)
{
Uart2SendData("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n\r\nOK\r\n"))
sendStep = 7;
else
ESP_LOGI("HttpGetLte", "@6:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 7)
{
while(1)
{
Uart2SendData("AT+SAPBR=3,1,\"APN\",\"\"\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+SAPBR=3,1,\"APN\",\"\"\r\n\r\nOK\r\n"))
sendStep = 8;
else
ESP_LOGI("HttpGetLte", "@7:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 8)
{
while(1)
{
Uart2SendData("AT+SAPBR=1,1\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+SAPBR=1,1\r\n\r\nOK\r\n"))
sendStep = 9;
else if(strstr((char *)data, "AT+SAPBR=1,1\r\n\r\n") && strstr((char *)data, "+CME ERROR"))
sendStep = 9;
else
ESP_LOGI("HttpGetLte", "@8:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 9)
{
while(1)
{
Uart2SendData("AT+SAPBR=2,1\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+SAPBR=2,1\r\n\r\n") && strstr((char *)data, "+SAPBR: 1,1,"))
sendStep = 10;
else if(strstr((char *)data, "AT+SAPBR=2,1\r\n\r\n") && !strstr((char *)data, "+SAPBR: 1,1,"))
sendStep = 8;
else
ESP_LOGI("HttpGetLte", "@9:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 10)
{
//Init http
while(1)
{
Uart2SendData("AT+HTTPINIT\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+HTTPINIT\r\n\r\nOK\r\n"))
sendStep = 11;
else if(strstr((char *)data, "AT+HTTPINIT\r\n\r\n") && strstr((char *)data, "+CME ERROR: 3"))
sendStep = 11;
else
ESP_LOGI("HttpGetLte", "@10:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 11)
{
//Set http CID
while(1)
{
Uart2SendData("AT+HTTPPARA=\"CID\",1\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+HTTPPARA=\"CID\",1\r\n\r\nOK\r\n"))
sendStep = 12;
else
ESP_LOGI("HttpGetLte", "@11:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 12)
{
//Set url
char *sendUrlTempStr = (char *) malloc(MAX_HTTP_OUTPUT_BUFFER);
sprintf(sendUrlTempStr, "AT+HTTPPARA=\"URL\",\"%s\"\r\n", urlGet);
while(1)
{
Uart2SendData(sendUrlTempStr);
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+HTTPPARA=\"URL\",") && strstr((char *)data, "OK\r\n"))
sendStep = 13;
else
ESP_LOGI("HttpGetLte", "@12:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 13)
{
//Send http
while(1)
{
Uart2SendData("AT+HTTPACTION=1\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
//if(strstr((char *)data, "AT+HTTPACTION=1\r\n\r\nOK\r\n"))
//sendStep = 14;
//break;
if(strstr((char *)data, "+HTTPACTION: 1,200,"))
{
sendStep = 14;
// break;
}
else
ESP_LOGI("HttpGetLte", "@13:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 14)
{
//Get response
while(1)
{
Uart2SendData("AT+HTTPREAD\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+HTTPREAD\r\n\r\n+HTTPREAD:"))
{
successFlag = 1;
strcpy(output_buffer, (char *)data);
ESP_LOGI("HttpGetLte", "%s", output_buffer);
sendStep = 15;
}
else
ESP_LOGI("HttpGetLte", "@14:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 15)
{
//Terminate http
while(1)
{
Uart2SendData("AT+HTTPTERM\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+HTTPTERM\r\n\r\nOK\r\n"))
sendStep = 16;
else if(strstr((char *)data, "AT+HTTPTERM\r\n\r\n") && strstr((char *)data, "+CME ERROR: 3"))
sendStep = 16;
else
ESP_LOGI("HttpGetLte", "@15:%s", (char *)data);
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else if(sendStep == 16)
{
//Shutdown network
while(1)
{
Uart2SendData("AT+CIPSHUT\r\n");
const int rxBytes = uart_read_bytes(UART_NUM_2, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);;
if(rxBytes > 0)
{
data[rxBytes] = 0;
if(strstr((char *)data, "AT+CIPSHUT\r\n\r\nSHUT OK\r\n"))
sendStep = 20;
else if(strstr((char *)data, "AT+CIPSHUT\r\n\r\n") && strstr((char *)data, "+CME ERROR: 3"))
sendStep = 20;
else
{
//sendStep = 20;
ESP_LOGI("HttpGetLte", "@16:%s", (char *)data);
}
break;
}
if(sleepTime)
vTaskDelay(sleepTime*1000 / portTICK_PERIOD_MS);
}
}
else
{
if(successFlag)
{
sendStep = 5;
return 1;
}
else
{
//sendStep = 17;
return 0;
}
//vTaskDelay(1000 / portTICK_PERIOD_MS);
}
ESP_LOGI("HttpGetLteStep", "%d", sendStep);
}
free(data); //清空数据缓冲区
return 0;
}

图传+指令函数(MicroPython)

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
sendStep = 5
def Uart4GSendImg(sendImg, url): #If need to send Img, set 'sendImg' = 1
global sendStep, ipAddress
imgUrlJson = ""
responseJson = ""
sleepTime = 0
while 1:
if sendStep == 0:
#Check 4G model
uart4G.write('AT\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT\r\n\r\nOK\r\n' in readData4G:
sendStep = 1
break
if readData4G != None:
print("@0:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 1:
#Check SIM card
uart4G.write('AT+CPIN?\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+CPIN?\r\n\r\n+CPIN: READY\r\n\r\nOK\r\n' in readData4G:
sendStep = 2
break
if readData4G != None:
print("@1:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 2:
#Check regist
uart4G.write('AT+CGATT?\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+CGATT?\r\n\r\n+CGATT: 1\r\n\r\nOK\r\n' in readData4G:
sendStep = 3
break
if readData4G != None:
print("@2:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 3:
#Set APN
uart4G.write('AT+CSTT\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+CSTT\r\n\r\nOK\r\n' in readData4G:
sendStep = 4
break
elif readData4G != None and 'AT+CSTT\r\n\r\n' in readData4G and '+CME ERROR' in readData4G:
sendStep = 5
break
if readData4G != None:
print("@3:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 4:
#Active network
uart4G.write('AT+CIICR\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+CIICR\r\n\r\nOK\r\n' in readData4G:
sendStep = 5
break
elif readData4G != None and 'AT+CIICR\r\n\r\n' in readData4G and '+CME ERROR' in readData4G:
sendStep = 0
break
if readData4G != None:
print("@4:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 5:
#Check ip
uart4G.write('AT+CIFSR\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+CIFSR\r\n\r\n' in readData4G and not ('+CME ERROR' in readData4G):
ipAddress = readData4G[12:-2]
sendStep = 6
break
elif readData4G != None and 'AT+CIFSR\r\n\r\n' in readData4G and '+CME ERROR' in readData4G:
uart4G.write('AT+CIPSHUT\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+CIPSHUT\r\n\r\nSHUT OK\r\n' in readData4G:
sendStep = 3
break
if readData4G != None:
print("@5:", readData4G)
break
if readData4G != None:
print("@5:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 6:
uart4G.write('AT+SAPBR=3,1,"CONTYPE","GPRS"\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+SAPBR=3,1,"CONTYPE","GPRS"\r\n\r\nOK\r\n' in readData4G:
sendStep = 7
break
if readData4G != None:
print("@6:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 7:
uart4G.write('AT+SAPBR=3,1,"APN",""\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+SAPBR=3,1,"APN",""\r\n\r\nOK\r\n' in readData4G:
sendStep = 8
break
if readData4G != None:
print("@7:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 8:
uart4G.write('AT+SAPBR=1,1\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+SAPBR=1,1\r\n\r\nOK\r\n' in readData4G:
sendStep = 9
break
elif readData4G != None and 'AT+SAPBR=1,1\r\n\r\n' in readData4G and '+CME ERROR' in readData4G:
sendStep = 9
break
if readData4G != None:
print("@8:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 9:
uart4G.write('AT+SAPBR=2,1\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+SAPBR=2,1\r\n\r\n' in readData4G and '+SAPBR: 1,1,' in readData4G:
sendStep = 10
break
elif readData4G != None and 'AT+SAPBR=2,1\r\n\r\n' in readData4G and not ('+SAPBR: 1,1,' in readData4G):
sendStep = 8
break
if readData4G != None:
print("@9:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 10:
#Init http
uart4G.write('AT+HTTPINIT\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPINIT\r\n\r\nOK\r\n' in readData4G:
sendStep = 11
break
elif readData4G != None and 'AT+HTTPINIT\r\n\r\n' in readData4G and '+CME ERROR: 3' in readData4G:
sendStep = 11
break
if readData4G != None:
print("@10:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 11:
#Set http CID
uart4G.write('AT+HTTPPARA="CID",1\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPPARA="CID",1\r\n\r\nOK\r\n' in readData4G:
sendStep = 12
break
if readData4G != None:
print("@11:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 12:
#Set http url
if sendImg == 1: #If need to send Img
uart4G.write('AT+HTTPPARA="URL","http://images.bemfa.com/upload/v1/upimages.php"\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPPARA="URL",' in readData4G and 'OK\r\n' in readData4G:
sendStep = 13
break
if readData4G != None:
print("@12:", readData4G)
break
else:
sendStep = 19
time.sleep(sleepTime)
elif sendStep == 13:
#Set http user header
uart4G.write('AT+HTTPPARA="USERDATA","User-Agent:MaixPy\\r\\nAuthorization:7d54f85af42976ee3c2693e6xxxxxxxx\\r\\nAuthtopic:K210IMG"\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPPARA="USERDATA",' in readData4G and 'OK\r\n' in readData4G:
sendStep = 15
break
if readData4G != None:
print("@13:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 14:
uart4G.write('AT+HTTPPARA?\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None:
print("@14:", readData4G)
sendStep = 15
break
else:
break
time.sleep(sleepTime)
elif sendStep == 15:
#Send file/image
img = image.Image("/flash/oPic.jpg")
img = img.compress(quality=60)
imgBytes = img.to_bytes()
#uart4G.write('AT+HTTPDATA=1024,100000\r\n')
print('AT+HTTPDATA=%d,10000\r\n' % img.size())
uart4G.write('AT+HTTPDATA=%d,10000\r\n' % img.size())
time.sleep(1)
while 1:
readData4G = uart4G.read()
if readData4G != None and 'DOWNLOAD' in readData4G:
if imgBytes:
print("Write ...")
uart4G.write(img)
print('Write', len(imgBytes))
while 1:
readData4G = uart4G.read()
if readData4G != None and '\r\nOK\r\n' in readData4G:
sendStep = 16
break
if readData4G != None:
print("@15:", readData4G)
break
#elif readData4G == None:
#print("Add")
#uart4G.write('\r\n')
break
time.sleep(sleepTime)
elif sendStep == 16:
#Send http
uart4G.write('AT+HTTPACTION=1\r\n')
while 1:
readData4G = uart4G.read()
#if readData4G != None and 'AT+HTTPACTION=1\r\n\r\nOK\r\n' in readData4G:
#sendStep = 17
#break
if readData4G != None and '+HTTPACTION: 1,200,' in readData4G:
sendStep = 17
break
if readData4G != None:
print("@16:", readData4G)
#break
time.sleep(sleepTime)
elif sendStep == 17:
#Get response
uart4G.write('AT+HTTPREAD\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPREAD\r\n\r\n+HTTPREAD:' in readData4G:
if sendImg:
imgUrlJson = readData4G[readData4G.index(b"\r\n{")+2:readData4G.index(b"}\r\n")+1]
responseJson = None
else:
responseJson = readData4G[readData4G.index(b"\r\n{")+2:readData4G.index(b"}\r\n")+1]
imgUrlJson = None
print(readData4G)
sendStep = 18
break
if readData4G != None:
print("@17:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 18:
#Terminate http
uart4G.write('AT+HTTPTERM\r\n')
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPTERM\r\n\r\nOK\r\n' in readData4G:
sendStep = 20
break
if readData4G != None:
print("@18:", readData4G)
break
time.sleep(sleepTime)
elif sendStep == 19:
#Send message
uart4G.write('AT+HTTPPARA="URL","{}"\r\n'.format(url))
while 1:
readData4G = uart4G.read()
if readData4G != None and 'AT+HTTPPARA="URL",' in readData4G and 'OK\r\n' in readData4G:
sendStep = 16
break
if readData4G != None:
print("@19:", readData4G)
break
time.sleep(sleepTime)
else:
if imgUrlJson:
sendStep = 5
return imgUrlJson
elif responseJson:
sendStep = 5
return responseJson
else:
sendStep = 17
#return None
time.sleep(1)
print(sendStep)