123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #include "ml307a.h"
- #include "mba32a.h"
- #include "usart.h"
- #include "tt808.h"
- #include <stdlib.h>
- bool u4RecvFlag = false;
- bool ml307aFail = false;
- bool reset4Gmodule = false;
- bool module4G_F = false;
- uint8_t u4length=0;
- uint8_t step4g;
- Time time;
- bool conn4g_loc=false;
- uint8_t resetcount_4g=0;
- uint8_t g_ML307A_8buf[500]={0};
- uint8_t g_ML307A_Loc[1024]={0};
- uint8_t g_ML307A_8buf_Down[500]={0};
- uint8_t MLBSLOCcount = 0;
- extern UART_HandleTypeDef huart3;
- extern UART_HandleTypeDef huart4;
- char JD_str[13];
- char WD_str[12];
- char JWD_str[25];
- uint32_t JD_dw=0;
- uint32_t WD_dw=0;
- extern Menu_table menu;
- void ML307AReset(void);
- uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum);
- uint8_t MQTT_PUB(uint8_t connect_id, char *topic, char *mesg, char* pRes);
- uint8_t MQTT_CONN(char *IpAddr, uint16_t port, char *ID, char *User, char *Passwd, char* pRes)
- {
-
- char MQTT_Sendbuf[128];
- sprintf((char *)MQTT_Sendbuf,"AT+MQTTCONN=0,\"%s\",%d,\"%s\",\"%s\",\"%s\"\r\n",IpAddr, port, ID,User, Passwd);
- if(sendCmd_4G(MQTT_Sendbuf,pRes,1,1))
- {
- return 1;
- }
- return 0;
- }
- //"AT+MQTTPUB=0,\"world\",1,0,0,26,\"abcdefghijklmnopqrstuvwxyz\"\r\n","+MQTTURC: \"publish\",",1,1)) //QOS01 =<connect_id>,<topic>,<qos>,<retain>,<dup>,<msg_len>[,<message>]
- uint8_t MQTT_PUB(uint8_t connect_id, char *topic, char *mesg, char* pRes)
- {
- char MQTT_Sendbuf[128];
- uint8_t length = strlen(mesg);
- sprintf((char *)MQTT_Sendbuf,"AT+MQTTPUB=%d,\"%s\",1,0,0,%d,\"%s\"\r\n",connect_id, topic, length, mesg);
- if(sendCmd_4G(MQTT_Sendbuf,pRes,1,1))
- {
- return 1;
- }
- return 0;
- }
- /**
- * @breaf 4G初始化
- */
- uint8_t MQTTCONNPUB(void)
- {
-
- if(MQTT_CONN((char*)PCMQTTADDR,PCMQTTPORT,"",(char*)PCMQTTUSER,(char*)PCMQTTPSWD,"+MQTTURC: \"conn\",0,0"))
- {
- printf("#连接MQTT成功\r\n");
- if(sendCmd_4G("AT+MQTTSUB=0,\"world\",1\r\n","+MQTTURC: \"suback\",",1,1))
- {
- printf("#订阅成功");
- conn4g_loc=true;
- return 1;
- }
- }
- return 0;
- }
- void CONNTCP(void);
- bool tcpconn =true;
- uint8_t ICCID[60];
- void TcpConn(char *value, uint8_t cid, char *IP, uint16_t port, uint8_t tcpConnmode)
- {
- char *ptr = value;
- ptr += sprintf(ptr, "AT+MIPOPEN=%d,\"TCP\",\"%s\",%d,60,%d\r\n",cid,IP,port,tcpConnmode);
- }
- void ML307A_Init(void)
- {
- printf("\t###02\t4G_init...\r\n");
- sendCmd_4G("AT+MGNSS=0\r\n", "OK", 1, 1);
- //sendCmd_4G("+++", "OK", 1, 1);//TCP连接 0普通模式 2缓存模式 1透传模式
- switch(step4g)
- {
- case 0:
- {
- if (sendCmd_4G("AT\r\n", "OK", 1, 5))
- {
-
- printf("#4G模组正常\r\n");
- if (sendCmd_4G("AT+ICCID\r\n", "OK", 1, 5))
- {
- memcpy(ICCID,&U4recvBuff[10],20);
- ICCID[20]=0x00;
- ICCID[21]=0x01;
- }
- step4g++;
- }
- else
- {
- ML307AReset();
- }
- }
- case 1:
- {
- if (sendCmd_4G("AT+CPIN?\r\n", "+CPIN: READY", 1, 5))
- {
- printf("#4G模组状态正常\r\n");
- step4g++;
- }
- else
- {
- ML307AReset();
- }
- }
- case 2:
- {
- if (sendCmd_4G("AT+CEREG?\r\n", "+CEREG: 0,1", 1, 5))
- {
- printf("#4G模组附着正常\r\n");
- printf("#4G模组初始化完成\r\n");
- step4g++;
- }
- else
- {
- ML307AReset();
- }
- }
- case 3:
- {
- if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
- {
- printf("#断开连接\r\n");
- }
-
- char TCPconnvalue[100];
- TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式
- if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 3)) //TCP连接 0普通模式 2缓存模式 1透传模式
- {
- step4g++;
- printf("#TCP连接成功\r\n");
- }
- else
- {
- tcpconn=false;
- ML307AReset();
- }
- }
-
- case 4:
- {
- if(sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 1))//默认输入输出为ASCII,0 发16,1 接16,1
- {
- sendCmd_4G("AT+MLBSCFG=\"method\",40\r\n", "OK", 1, 1);//配置LBS接口为ONEos定位服务
- sendCmd_4G("AT+MLBSCFG=\"nearbtsen\",1\r\n", "OK", 1, 1);//启动邻区信息参与定位
- printf("#TCP设置完成\r\n");
-
-
- break;
- }
- else
- {
- ML307AReset();
- }
- }
-
-
-
-
- }
- menu.current=0;//回到初始
- refresh=true;
- module4G_F=true;
- }
- /**
- * @breaf 4G发送串口命令
- */
- uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum)
- {
- uint8_t i = 0;
- uint32_t time;
- for(i = 0; i < sendNum; i++)
- {
- time = timeOut * 10;
- memset(g_ML307A_8buf_Down,0,sizeof(g_ML307A_8buf_Down));
- USART_SendString(&huart4, pCmd);
- HAL_Delay(800);
- while(time--)
- {
- if(strstr((const char *)g_ML307A_8buf_Down, pRes) != NULL) // 如果检索到关键词
- {
- return 1;
- }
- HAL_Delay(100);
- }
- }
- return 0;
- }
- /**
- @brief 重启模块
- @param 无
- @return 无
- */
- void ML307AReset(void)
- {
- step4g=0;
- reset4Gmodule=true;
- module4G_F=false;
- resetcount_4g+=1;
- printf("4G Module_Reset\n");
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
- HAL_Delay(5000);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
- }
- //IP地址:183.230.40.96
- //端口号:1883
- //MQTT客户端ID:dtuv1
- //MQTT用户名:SwU99jNiWK
- //MQTT登录密码: version=2018-10-31&res=products%2FSwU99jNiWK%2Fdevices%2Fdtuv1&et=207998147912&method=md5&sign=WKSoO%2BqDTtQ1LqEklV9NUA%3D%3D
- void Work_4Gtest(void)
- {
-
- {
- //需保存的工作函数
- if(sendCmd_4G("AT+MLBSLOC\r\n", "OK", 1, 1))
- {
- HAL_Delay(5000);
-
- memcpy(JD_str, &g_ML307A_8buf_Down[14], 12);
- memcpy(WD_str, &g_ML307A_8buf_Down[27], 11);
- //printf("%s\r\n",g_ML307A_8buf_Down);
- //调试TCP报文
- double num;
- double num1;
- num = atof(JD_str);
- num1 = atof(WD_str);
- num = num* 1000000;
- num1= num1* 1000000;
- JD_dw = (uint32_t)num;
- WD_dw = (uint32_t)num1;
-
-
- }
-
-
- }
-
- }
- extern TT808 tt808;
|