#include "lte.h" uint8_t UART3_RxCounter = 0; //定义通过串口6发送AT指令命令 // timeout *= 100ms int LTE_SendCmd(char *cmd, char* reply, int timeout) { UART3_RxCounter = 0; memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE); Usart_SendString(USART6, cmd); while(timeout--) { OSTimeDly(100); if(strstr((char*)<E_RxBuff, reply)) break; printf("time = %d ", timeout); } printf("\r\n"); if(timeout <= 0) return -1; return 0; } int LTE_init() { char* rss_str; int rssi,res; u16 cat1_timeout = 0; while(LTE_SendCmd("AT+IPR=115200\r\n","OK", 3)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ printf(" IPR false\r\n"); return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT&W\r\n","OK", 3)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ printf(" IPR false\r\n"); return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT\r\n","OK", 2)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ printf(" uart false\r\n"); return TIMEOUT; } } cat1_timeout = 0; printf("uart ok\r\n"); while(LTE_SendCmd("AT+CPIN?\r\n","READY", 50)){// 最大延时5s OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } printf("simcard ok\r\n"); cat1_timeout = 0; while(LTE_SendCmd("AT+CSQ\r\n","+CSQ", 3)){// 最大延时300ms rss_str = strstr((char*)<E_RxBuff,"+CSQ:"); if (!rss_str) { return 1; } sscanf(rss_str, "+CSQ:%d,%d", &rssi, &res); if(rssi != 99) printf("RSSI is %d\r\n",rssi); memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE); return 0; OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT+CEREG?\r\n","0,1", 2)){// 最大延时200ms if (strstr((char*)LTE_RxBuff, "0,1")){ printf("\r\n%s\r\n", LTE_RxBuff); memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE); return 0; } cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } printf("网络注册 ok\r\n"); cat1_timeout = 0; while(LTE_SendCmd("AT+CGATT\r\n","+CGATT: 1", 100)){ // 最大延时140s OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } printf(" 网络附着 ok\r\n"); cat1_timeout = 0; while(LTE_SendCmd("AT+QSCLK=0 \r\n","OK", 3)){ // 最大延时300ms OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } printf("close sleep mode\r\n"); } int LTE_TCP_init() { u16 cat1_timeout = 0; //配置PDP上下文ID为1 while(LTE_SendCmd("AT+QICFG=\"transpktsize\"50\r\n","OK", 3)){// 最大延时300ms OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } return 0; } int LTE_http() { u16 cat1_timeout = 0; //配置PDP上下文ID为1 while(LTE_SendCmd("AT+QHTTPCFG=\"contextid\",1\r\n","OK", 3)){// 最大延时300ms OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; //启用输出 HTTP 响应头信息 while(LTE_SendCmd("AT+QHTTPCFG=\"responseheader\",1\r\n","OK", 3)){// 最大延时300ms OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("QHTTPCFG ok\r\n"); //查询 PDP 上下文状态 while(LTE_SendCmd("AT+QIACT?\r\n","OK", 100)){//最大延时150S OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("PDP_CHECK one ok\r\n"); while(LTE_SendCmd("AT+QHTTPCFG=\"contenttype\",1\r\n","OK", 3)){// 最大延时300ms OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("CFG ok\r\n"); /* "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n" APN 联通:UNINET 移动:CMNET 电信:CTNET */ while(LTE_SendCmd("AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n","OK", 3000)){// 最大响应时间 无 OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("PDP_CONFIG ok\r\n"); // 使配置生效 while(LTE_SendCmd("AT+CFUN=1 \r\n","OK", 100)){// 最大响应时间15S OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("PDP_CONFIG ok\r\n"); //查询 PDP 上下文状态 while(LTE_SendCmd("AT+QIACT?\r\n","+QIACT", 100)){//最大延时150S OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("PDP_CHECK two ok\r\n"); //激活 PDP 上下文 1 while(LTE_SendCmd("AT+QIACT=1\r\n","OK", 500)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("PDP_激活 ok\r\n"); return SUCCESS; } /* *HTTP(S)服务器的 URL 必须以 http://或 https://开头,表示访问 HTTP 或 HTTPS 服务器 */ int LTE_send_URL(char* url) { u16 cat1_timeout = 0; char message[32];// 根据网址具体长度决定 snprintf(message, sizeof(message), "AT+QHTTPURL=%d,%d\r\n", strlen(url), 80); while(LTE_SendCmd(message,"CONNECT", 1000)){// 返回connect,切换到透传模式 OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("ready to send url\r\n"); while(LTE_SendCmd(url,"OK", 5000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; printf("url send OK\r\n"); return SUCCESS; //成功 } int LTE_HTTP_get() { u16 cat1_timeout = 0; int err,httprspcode,content_length,fd; __start: //发送 HTTP GET 请求,最大响应时间为 80 秒 while(LTE_SendCmd("AT+QHTTPGET=80\r\n","OK", 80)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } sscanf((char*)<E_RxBuff, "+QHTTPGET: %d,%d,%d ", &err, &httprspcode, &content_length); if(200 == httprspcode){ printf("connect OK\r\n"); } else{ goto __start; } // 读取 HTTP 响应信息并将其储存到 UFS 文件中 cat1_timeout = 0; while(LTE_SendCmd("AT+QHTTPREADFILE=\"UFS:test.txt\",80 \r\n","OK", 100)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; // 打开文件 while(LTE_SendCmd("AT+QFOPEN=\"UFS:test.txt\",2\r\n","OK", 100)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } sscanf((char*)<E_RxBuff, "+QFOPEN:%d ", &fd);// 获取文件指针 // 读取文件 char readMessage[32]; memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE); sprintf(readMessage,"AT+QFREAD=%d,%d\r\n", fd,sizeof(LTE_RxBuff)); while(LTE_SendCmd(readMessage,"OK", 500)){// 最大响应时间5S OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 解析数据 addGatewayParams((char*)<E_RxBuff); cat1_timeout = 0; // 关闭文件 char closeCMD[32]; sprintf(closeCMD,"AT+QFCLOSE=%d\r\n", fd); while(LTE_SendCmd(closeCMD,"OK", 100)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } }