lte.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "lte.h"
  2. uint8_t UART3_RxCounter = 0;
  3. //定义通过串口6发送AT指令命令
  4. // timeout *= 100ms
  5. int LTE_SendCmd(char *cmd, char* reply, int timeout)
  6. {
  7. UART3_RxCounter = 0;
  8. memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE);
  9. Usart_SendString(USART6, cmd);
  10. while(timeout--) {
  11. OSTimeDly(100);
  12. if(strstr((char*)&LTE_RxBuff, reply))
  13. break;
  14. printf("time = %d ", timeout);
  15. }
  16. printf("\r\n");
  17. if(timeout <= 0)
  18. return -1;
  19. return 0;
  20. }
  21. int LTE_init()
  22. {
  23. char* rss_str;
  24. int rssi,res;
  25. u16 cat1_timeout = 0;
  26. while(LTE_SendCmd("AT+IPR=115200\r\n","OK", 3)){
  27. OSTimeDly(1);
  28. cat1_timeout ++;
  29. if(cat1_timeout >= 2000){
  30. printf(" IPR false\r\n");
  31. return TIMEOUT;
  32. }
  33. }
  34. cat1_timeout = 0;
  35. while(LTE_SendCmd("AT&W\r\n","OK", 3)){
  36. OSTimeDly(1);
  37. cat1_timeout ++;
  38. if(cat1_timeout >= 2000){
  39. printf(" IPR false\r\n");
  40. return TIMEOUT;
  41. }
  42. }
  43. cat1_timeout = 0;
  44. while(LTE_SendCmd("AT\r\n","OK", 2)){
  45. OSTimeDly(1);
  46. cat1_timeout ++;
  47. if(cat1_timeout >= 2000){
  48. printf(" uart false\r\n");
  49. return TIMEOUT;
  50. }
  51. }
  52. cat1_timeout = 0;
  53. printf("uart ok\r\n");
  54. while(LTE_SendCmd("AT+CPIN?\r\n","READY", 50)){// 最大延时5s
  55. OSTimeDly(1);
  56. cat1_timeout ++;
  57. if(cat1_timeout >= 2000){
  58. return TIMEOUT;
  59. }
  60. }
  61. printf("simcard ok\r\n");
  62. cat1_timeout = 0;
  63. while(LTE_SendCmd("AT+CSQ\r\n","+CSQ", 3)){// 最大延时300ms
  64. rss_str = strstr((char*)&LTE_RxBuff,"+CSQ:");
  65. if (!rss_str) {
  66. return 1;
  67. }
  68. sscanf(rss_str, "+CSQ:%d,%d", &rssi, &res);
  69. if(rssi != 99)
  70. printf("RSSI is %d\r\n",rssi);
  71. memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE);
  72. return 0;
  73. OSTimeDly(1);
  74. cat1_timeout ++;
  75. if(cat1_timeout >= 2000){
  76. return TIMEOUT;
  77. }
  78. }
  79. cat1_timeout = 0;
  80. while(LTE_SendCmd("AT+CEREG?\r\n","0,1", 2)){// 最大延时200ms
  81. if (strstr((char*)LTE_RxBuff, "0,1")){
  82. printf("\r\n%s\r\n", LTE_RxBuff);
  83. memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE);
  84. return 0;
  85. }
  86. cat1_timeout ++;
  87. if(cat1_timeout >= 2000){
  88. return TIMEOUT;
  89. }
  90. }
  91. printf("网络注册 ok\r\n");
  92. cat1_timeout = 0;
  93. while(LTE_SendCmd("AT+CGATT\r\n","+CGATT: 1", 100)){ // 最大延时140s
  94. OSTimeDly(1);
  95. cat1_timeout ++;
  96. if(cat1_timeout >= 2000){
  97. return TIMEOUT;
  98. }
  99. }
  100. printf(" 网络附着 ok\r\n");
  101. cat1_timeout = 0;
  102. while(LTE_SendCmd("AT+QSCLK=0 \r\n","OK", 3)){ // 最大延时300ms
  103. OSTimeDly(1);
  104. cat1_timeout ++;
  105. if(cat1_timeout >= 2000){
  106. return TIMEOUT;
  107. }
  108. }
  109. printf("close sleep mode\r\n");
  110. }
  111. int LTE_TCP_init()
  112. {
  113. u16 cat1_timeout = 0;
  114. //配置PDP上下文ID为1
  115. while(LTE_SendCmd("AT+QICFG=\"transpktsize\"50\r\n","OK", 3)){// 最大延时300ms
  116. OSTimeDly(1);
  117. cat1_timeout ++;
  118. if(cat1_timeout >= 2000){
  119. return TIMEOUT;
  120. }
  121. }
  122. return 0;
  123. }
  124. int LTE_http()
  125. {
  126. u16 cat1_timeout = 0;
  127. //配置PDP上下文ID为1
  128. while(LTE_SendCmd("AT+QHTTPCFG=\"contextid\",1\r\n","OK", 3)){// 最大延时300ms
  129. OSTimeDly(1);
  130. cat1_timeout ++;
  131. if(cat1_timeout >= 2000){
  132. return TIMEOUT;
  133. }
  134. }
  135. cat1_timeout = 0;
  136. //启用输出 HTTP 响应头信息
  137. while(LTE_SendCmd("AT+QHTTPCFG=\"responseheader\",1\r\n","OK", 3)){// 最大延时300ms
  138. OSTimeDly(1);
  139. cat1_timeout ++;
  140. if(cat1_timeout >= 2000){
  141. return TIMEOUT;
  142. }
  143. }
  144. cat1_timeout = 0;
  145. printf("QHTTPCFG ok\r\n");
  146. //查询 PDP 上下文状态
  147. while(LTE_SendCmd("AT+QIACT?\r\n","OK", 100)){//最大延时150S
  148. OSTimeDly(1);
  149. cat1_timeout ++;
  150. if(cat1_timeout >= 2000){
  151. return TIMEOUT;
  152. }
  153. }
  154. cat1_timeout = 0;
  155. printf("PDP_CHECK one ok\r\n");
  156. while(LTE_SendCmd("AT+QHTTPCFG=\"contenttype\",1\r\n","OK", 3)){// 最大延时300ms
  157. OSTimeDly(1);
  158. cat1_timeout ++;
  159. if(cat1_timeout >= 2000){
  160. return TIMEOUT;
  161. }
  162. }
  163. cat1_timeout = 0;
  164. printf("CFG ok\r\n");
  165. /*
  166. "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n"
  167. APN 联通:UNINET 移动:CMNET 电信:CTNET
  168. */
  169. while(LTE_SendCmd("AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n","OK", 3000)){// 最大响应时间 无
  170. OSTimeDly(1);
  171. cat1_timeout ++;
  172. if(cat1_timeout >= 2000){
  173. return TIMEOUT;
  174. }
  175. }
  176. cat1_timeout = 0;
  177. printf("PDP_CONFIG ok\r\n");
  178. // 使配置生效
  179. while(LTE_SendCmd("AT+CFUN=1 \r\n","OK", 100)){// 最大响应时间15S
  180. OSTimeDly(1);
  181. cat1_timeout ++;
  182. if(cat1_timeout >= 2000){
  183. return TIMEOUT;
  184. }
  185. }
  186. cat1_timeout = 0;
  187. printf("PDP_CONFIG ok\r\n");
  188. //查询 PDP 上下文状态
  189. while(LTE_SendCmd("AT+QIACT?\r\n","+QIACT", 100)){//最大延时150S
  190. OSTimeDly(1);
  191. cat1_timeout ++;
  192. if(cat1_timeout >= 2000){
  193. return TIMEOUT;
  194. }
  195. }
  196. cat1_timeout = 0;
  197. printf("PDP_CHECK two ok\r\n");
  198. //激活 PDP 上下文 1
  199. while(LTE_SendCmd("AT+QIACT=1\r\n","OK", 500)){
  200. OSTimeDly(1);
  201. cat1_timeout ++;
  202. if(cat1_timeout >= 2000){
  203. return TIMEOUT;
  204. }
  205. }
  206. cat1_timeout = 0;
  207. printf("PDP_激活 ok\r\n");
  208. return SUCCESS;
  209. }
  210. /*
  211. *HTTP(S)服务器的 URL 必须以 http://或 https://开头,表示访问 HTTP 或 HTTPS 服务器
  212. */
  213. int LTE_send_URL(char* url)
  214. {
  215. u16 cat1_timeout = 0;
  216. char message[32];// 根据网址具体长度决定
  217. snprintf(message, sizeof(message), "AT+QHTTPURL=%d,%d\r\n", strlen(url), 80);
  218. while(LTE_SendCmd(message,"CONNECT", 1000)){// 返回connect,切换到透传模式
  219. OSTimeDly(1);
  220. cat1_timeout ++;
  221. if(cat1_timeout >= 2000){
  222. return TIMEOUT;
  223. }
  224. }
  225. cat1_timeout = 0;
  226. printf("ready to send url\r\n");
  227. while(LTE_SendCmd(url,"OK", 5000)){
  228. OSTimeDly(1);
  229. cat1_timeout ++;
  230. if(cat1_timeout >= 2000){
  231. return TIMEOUT;
  232. }
  233. }
  234. cat1_timeout = 0;
  235. printf("url send OK\r\n");
  236. return SUCCESS; //成功
  237. }
  238. int LTE_HTTP_get()
  239. {
  240. u16 cat1_timeout = 0;
  241. int err,httprspcode,content_length,fd;
  242. __start:
  243. //发送 HTTP GET 请求,最大响应时间为 80 秒
  244. while(LTE_SendCmd("AT+QHTTPGET=80\r\n","OK", 80)){
  245. OSTimeDly(1);
  246. cat1_timeout ++;
  247. if(cat1_timeout >= 2000){
  248. return TIMEOUT;
  249. }
  250. }
  251. sscanf((char*)&LTE_RxBuff, "+QHTTPGET: %d,%d,%d ", &err, &httprspcode, &content_length);
  252. if(200 == httprspcode){
  253. printf("connect OK\r\n");
  254. }
  255. else{
  256. goto __start;
  257. }
  258. // 读取 HTTP 响应信息并将其储存到 UFS 文件中
  259. cat1_timeout = 0;
  260. while(LTE_SendCmd("AT+QHTTPREADFILE=\"UFS:test.txt\",80 \r\n","OK", 100)){
  261. OSTimeDly(1);
  262. cat1_timeout ++;
  263. if(cat1_timeout >= 2000){
  264. return TIMEOUT;
  265. }
  266. }
  267. cat1_timeout = 0;
  268. // 打开文件
  269. while(LTE_SendCmd("AT+QFOPEN=\"UFS:test.txt\",2\r\n","OK", 100)){
  270. OSTimeDly(1);
  271. cat1_timeout ++;
  272. if(cat1_timeout >= 2000){
  273. return TIMEOUT;
  274. }
  275. }
  276. sscanf((char*)&LTE_RxBuff, "+QFOPEN:%d ", &fd);// 获取文件指针
  277. // 读取文件
  278. char readMessage[32];
  279. memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE);
  280. sprintf(readMessage,"AT+QFREAD=%d,%d\r\n", fd,sizeof(LTE_RxBuff));
  281. while(LTE_SendCmd(readMessage,"OK", 500)){// 最大响应时间5S
  282. OSTimeDly(1);
  283. cat1_timeout ++;
  284. if(cat1_timeout >= 2000){
  285. return TIMEOUT;
  286. }
  287. }
  288. // 解析数据
  289. addGatewayParams((char*)&LTE_RxBuff);
  290. cat1_timeout = 0;
  291. // 关闭文件
  292. char closeCMD[32];
  293. sprintf(closeCMD,"AT+QFCLOSE=%d\r\n", fd);
  294. while(LTE_SendCmd(closeCMD,"OK", 100)){
  295. OSTimeDly(1);
  296. cat1_timeout ++;
  297. if(cat1_timeout >= 2000){
  298. return TIMEOUT;
  299. }
  300. }
  301. }