O_嵌入式专题目录
单独指令函数(C - FreeRtos)
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)
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)
评论已关闭