1612 lines
35 KiB
C
1612 lines
35 KiB
C
|
|
/*
|
|||
|
|
*********************************************************************************************************
|
|||
|
|
* IAR Development Kits
|
|||
|
|
* on the
|
|||
|
|
*
|
|||
|
|
* M451
|
|||
|
|
*
|
|||
|
|
* Filename : uart_dtu.c
|
|||
|
|
* Version : V1.00
|
|||
|
|
* Programmer(s) : Qian Xianghong
|
|||
|
|
*********************************************************************************************************
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#include "includes.h"
|
|||
|
|
|
|||
|
|
#include "drv_dtu.h"
|
|||
|
|
#include "drv_gps.h"
|
|||
|
|
|
|||
|
|
// <20>Ƿ<EFBFBD><C7B7><EFBFBD>ӡDTU<54><55><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ַ<EFBFBD>
|
|||
|
|
uint8_t DTU_uartPrint = 0;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ڳ<EFBFBD>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1>
|
|||
|
|
const uint16_t DTU_tmrQPendLong = 120; // 120s
|
|||
|
|
const uint16_t DTU_tmrQPendShort = 5; // 5s
|
|||
|
|
const uint16_t DTU_tmrQPendSpec = 40; // 40s
|
|||
|
|
|
|||
|
|
// DTU<54>ϵ<EFBFBD><CFB5><EFBFBD>־
|
|||
|
|
uint8_t DTU_hasPowered = 0;
|
|||
|
|
uint8_t DTU_imeiRead = 0;
|
|||
|
|
uint8_t DTU_simCard = 0;
|
|||
|
|
uint8_t DTU_simReg = 0;
|
|||
|
|
uint8_t DTU_gprsReg = 0;
|
|||
|
|
uint8_t DTU_isATMode = 0;
|
|||
|
|
uint8_t DTU_gprsAttached = 0;
|
|||
|
|
uint8_t DTU_linkEstablished = 0;
|
|||
|
|
uint8_t DTU_sslConfig = 0;
|
|||
|
|
uint8_t DTU_mqttConfig = 0;
|
|||
|
|
uint8_t DTU_ipFetched = 0;
|
|||
|
|
char DTU_APN[20] = "";
|
|||
|
|
uint8_t DTU_isRMCFormat = 0;
|
|||
|
|
uint8_t DTU_fail_count = 0;
|
|||
|
|
uint8_t DTU_shutdown_count = 0;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
|
|||
|
|
static void DTU_ClearQueue()
|
|||
|
|
{
|
|||
|
|
uint8_t timeOut;
|
|||
|
|
uint32_t timeOutTick;
|
|||
|
|
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
timeOut = 1;
|
|||
|
|
timeOutTick = GetDelayTick(500);
|
|||
|
|
while(!IsTickOut(timeOutTick))
|
|||
|
|
{
|
|||
|
|
if(LoopBuff_GetCount(&DTU_TaskM))
|
|||
|
|
{
|
|||
|
|
timeOut = 0;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
LoopBuff_Clear(&DTU_TaskM);
|
|||
|
|
} while(!timeOut);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>ȴ<EFBFBD>DTU<54><55><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
uint32_t DTU_ParseResult(char *checkPattern, char *checkErrPattern, char *resultBuff, uint16_t waitSeconds)
|
|||
|
|
{
|
|||
|
|
uint8_t done = 0, timeOut;
|
|||
|
|
uint32_t timeOutTick;
|
|||
|
|
uint8_t u8DTU;
|
|||
|
|
uint16_t count, idx = 0;
|
|||
|
|
uint32_t ret = FALSE;
|
|||
|
|
uint8_t strLen = strlen(checkPattern);
|
|||
|
|
uint8_t errLen = strlen(checkErrPattern);
|
|||
|
|
|
|||
|
|
memset(DTU_recvBuff, 0, DTU_RECVBUFF_SIZE);
|
|||
|
|
// <20><><EFBFBD>ⷵ<EFBFBD><E2B7B5><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
timeOut = 1;
|
|||
|
|
timeOutTick = GetDelayTick(waitSeconds * 1000);
|
|||
|
|
while(!IsTickOut(timeOutTick))
|
|||
|
|
{
|
|||
|
|
if(LoopBuff_GetCount(&DTU_TaskM))
|
|||
|
|
{
|
|||
|
|
timeOut = 0;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!timeOut)
|
|||
|
|
{
|
|||
|
|
count = LoopBuff_GetCount(&DTU_TaskM);
|
|||
|
|
while(count--)
|
|||
|
|
{
|
|||
|
|
// ȡ<><C8A1><EFBFBD><EFBFBD>
|
|||
|
|
memmove(&u8DTU, LoopBuff_GetDataPtr(&DTU_TaskM, DTU_TaskM.info.rdPtr), 1);
|
|||
|
|
LoopBuff_RemoveItems(&DTU_TaskM, 1);
|
|||
|
|
|
|||
|
|
// if(u8DTU == '\n')
|
|||
|
|
// printf("\\n");
|
|||
|
|
// else if(u8DTU == '\r')
|
|||
|
|
// printf("\\r");
|
|||
|
|
// else
|
|||
|
|
// printf("%c", u8DTU);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ǽ<EFBFBD><C7BC>ⷢ<EFBFBD>͵<EFBFBD><CDB5><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>><3E><><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7>سɹ<D8B3>
|
|||
|
|
if(strcmp(checkPattern, ">") == 0 && u8DTU == '>')
|
|||
|
|
{
|
|||
|
|
strcpy(resultBuff, ">");
|
|||
|
|
ret = TRUE;
|
|||
|
|
|
|||
|
|
done = 1;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
// <20><><EFBFBD>з<EFBFBD>
|
|||
|
|
if(u8DTU == '\n')
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD>\nǰ<6E><C7B0><EFBFBD><EFBFBD>\r
|
|||
|
|
if(idx > 0 && DTU_recvBuff[idx - 1] == '\r')
|
|||
|
|
DTU_recvBuff[--idx] = 0;
|
|||
|
|
|
|||
|
|
// <20>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>绰<EFBFBD><E7BBB0><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if((strLen > 0 && strstr(DTU_recvBuff, checkPattern)) || (strLen == 0 && idx > 0))
|
|||
|
|
{
|
|||
|
|
DTU_recvBuff[idx] = 0;
|
|||
|
|
strcpy(resultBuff, DTU_recvBuff);
|
|||
|
|
ret = TRUE;
|
|||
|
|
|
|||
|
|
done = 1;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
// <20><><EFBFBD>鷵<EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
if(errLen > 0 && strstr(DTU_recvBuff, checkErrPattern))
|
|||
|
|
{
|
|||
|
|
DTU_recvBuff[idx] = 0;
|
|||
|
|
strcpy(resultBuff, DTU_recvBuff);
|
|||
|
|
ret = FALSE;
|
|||
|
|
|
|||
|
|
done = 1;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
// û<><C3BB><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
memset(DTU_recvBuff, 0, DTU_RECVBUFF_SIZE);
|
|||
|
|
idx = 0;
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
if(idx < DTU_RECVBUFF_SIZE)
|
|||
|
|
DTU_recvBuff[idx++] = u8DTU;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} while(!done && !timeOut);
|
|||
|
|
|
|||
|
|
return ret;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ִ<><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t DTU_ExecuteCmd(char *cmd, char *checkPattern, char *checkErrPattern, char *resultBuff, uint16_t waitSeconds)
|
|||
|
|
{
|
|||
|
|
uint32_t ret = FALSE;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>鲢<EFBFBD><E9B2A2>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>GPS<50><53>Դ
|
|||
|
|
if(DTU_needCheckGPS)
|
|||
|
|
DTU_CheckGPS();
|
|||
|
|
|
|||
|
|
if(DTU_hasPowered)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
|
// UART_Transmit(&huart1, (uint8_t *) "AT\r", 3);
|
|||
|
|
// delay_ms(5);
|
|||
|
|
|
|||
|
|
printf("\n=> %s\n", cmd);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
DTU_ClearQueue();
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ӻس<D3BB><D8B3><EFBFBD><EFBFBD>з<EFBFBD>
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) cmd, strlen(cmd));
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) "\r", 1);
|
|||
|
|
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
ret = DTU_ParseResult(checkPattern, checkErrPattern, resultBuff, waitSeconds);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return ret;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><CBB7>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>len<65><6E><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD>size<7A><65><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
|
// <20><><EFBFBD><EFBFBD>ֵΪʵ<CEAA><CAB5><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
|||
|
|
uint16_t DTU_WaitResponse(uint8_t ssl, uint16_t len, uint8_t *buff, uint16_t size, uint16_t waitSeconds)
|
|||
|
|
{
|
|||
|
|
uint8_t timeOut;
|
|||
|
|
uint32_t timeOutTick;
|
|||
|
|
uint8_t u8DTU, phase;
|
|||
|
|
uint16_t i, count, data_count;
|
|||
|
|
uint16_t preidx, idx;
|
|||
|
|
char cntbuff[40], prebuff[40], *p, *p1;
|
|||
|
|
|
|||
|
|
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
idx = 0;
|
|||
|
|
|
|||
|
|
memset(prebuff, 0, sizeof(prebuff));
|
|||
|
|
preidx = 0;
|
|||
|
|
phase = 0;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
timeOut = 1;
|
|||
|
|
timeOutTick = GetDelayTick(idx == 0 ? waitSeconds * 1000 : 1000);
|
|||
|
|
while(!IsTickOut(timeOutTick))
|
|||
|
|
{
|
|||
|
|
if(LoopBuff_GetCount(&DTU_TaskM))
|
|||
|
|
{
|
|||
|
|
timeOut = 0;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!timeOut)
|
|||
|
|
{
|
|||
|
|
count = LoopBuff_GetCount(&DTU_TaskM);
|
|||
|
|
while(count--)
|
|||
|
|
{
|
|||
|
|
// ȡ<><C8A1><EFBFBD><EFBFBD>
|
|||
|
|
memmove(&u8DTU, LoopBuff_GetDataPtr(&DTU_TaskM, DTU_TaskM.info.rdPtr), 1);
|
|||
|
|
LoopBuff_RemoveItems(&DTU_TaskM, 1);
|
|||
|
|
|
|||
|
|
// if(u8DTU == '\n')
|
|||
|
|
// printf("\\n");
|
|||
|
|
// else if(u8DTU == '\r')
|
|||
|
|
// printf("\\r");
|
|||
|
|
// else if(isprint(u8DTU))
|
|||
|
|
// printf("%c", u8DTU);
|
|||
|
|
// else
|
|||
|
|
// printf("[%02X]", u8DTU);
|
|||
|
|
|
|||
|
|
if(phase == 0)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD>URC<52><43>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
|
if(preidx >= sizeof(prebuff) - 1)
|
|||
|
|
return 0;
|
|||
|
|
|
|||
|
|
prebuff[preidx++] = u8DTU;
|
|||
|
|
if(u8DTU == '\n')
|
|||
|
|
{
|
|||
|
|
if(!ssl)
|
|||
|
|
p = strstr(prebuff, "+QIURC: \"recv\",0,");
|
|||
|
|
else
|
|||
|
|
p = strstr(prebuff, "+QSSLURC: \"recv\",1,");
|
|||
|
|
if(p) // <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
p += (ssl ? 19 : 17);
|
|||
|
|
p1 = strstr(p, "\r\n");
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>URC<52><43>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
|
if(!p1 || p1 >= p + sizeof(cntbuff))
|
|||
|
|
return 0;
|
|||
|
|
|
|||
|
|
memset(cntbuff, 0, sizeof(cntbuff));
|
|||
|
|
strncpy(cntbuff, p, p1 - p);
|
|||
|
|
data_count = atoi(cntbuff);
|
|||
|
|
phase = 1;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
memset(prebuff, 0, sizeof(prebuff));
|
|||
|
|
preidx = 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if(idx < size)
|
|||
|
|
{
|
|||
|
|
if(data_count--)
|
|||
|
|
buff[idx++] = u8DTU;
|
|||
|
|
if(data_count == 0)
|
|||
|
|
{
|
|||
|
|
memset(prebuff, 0, sizeof(prebuff));
|
|||
|
|
preidx = 0;
|
|||
|
|
phase = 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} while(idx < size && !timeOut);
|
|||
|
|
|
|||
|
|
if(!ssl)
|
|||
|
|
{
|
|||
|
|
printf("\nresponse from server (%d bytes):\n", idx);
|
|||
|
|
for(i = 0; i < idx && i < 128; i++)
|
|||
|
|
printf("%02X ", buff[i]);
|
|||
|
|
if(idx > 128)
|
|||
|
|
printf("...");
|
|||
|
|
printf("\n");
|
|||
|
|
}
|
|||
|
|
if(idx < len)
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "RECV");
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>루ʼ<EBA3A8>ձ<EFBFBD><D5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>εĴ<CEB5><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>룩
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "");
|
|||
|
|
}
|
|||
|
|
return idx;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ģ<><C4A3>DEACT
|
|||
|
|
uint8_t DTU_Shutdown()
|
|||
|
|
{
|
|||
|
|
uint8_t ret = 0;
|
|||
|
|
char buff[40];
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ñ<EFBFBD>־
|
|||
|
|
DTU_imeiRead = 0;
|
|||
|
|
DTU_simCard = 0;
|
|||
|
|
DTU_simReg = 0;
|
|||
|
|
DTU_gprsReg = 0;
|
|||
|
|
DTU_gprsAttached = 0;
|
|||
|
|
DTU_linkEstablished = 0;
|
|||
|
|
DTU_sslConfig = 0;
|
|||
|
|
DTU_mqttConfig = 0;
|
|||
|
|
DTU_ipFetched = 0;
|
|||
|
|
DTU_fail_count = 0;
|
|||
|
|
dcBuff.dtuData.connected = 0;
|
|||
|
|
DTU_shutdown_count++;
|
|||
|
|
|
|||
|
|
if(DTU_ExecuteCmd("AT+QIDEACT=1", "OK", "ERROR", buff, DTU_tmrQPendSpec))
|
|||
|
|
{
|
|||
|
|
strcpy(DTU_APN, "");
|
|||
|
|
printf("\nDTU shutdown.\n");
|
|||
|
|
ret = 1;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
printf("\nDTU shutdown failed.\n");
|
|||
|
|
|
|||
|
|
return ret;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t DTU_SendAndRecv(uint8_t ssl, uint8_t *data, uint16_t len, uint16_t *recvLen, uint8_t *recvBuff, uint16_t recvSize)
|
|||
|
|
{
|
|||
|
|
char cmd[40], buff[40];
|
|||
|
|
int8_t try_count = 2;
|
|||
|
|
uint32_t ret = TRUE;
|
|||
|
|
uint16_t rlen = *recvLen;
|
|||
|
|
uint16_t totalLen = 0, rslen;
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
|
// UART_Transmit(&huart1, (uint8_t *) "AT\r", 3);
|
|||
|
|
// delay_ms(5);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
DTU_ClearQueue();
|
|||
|
|
// EC20һ<30><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>1460<36>ֽ<EFBFBD>
|
|||
|
|
while(totalLen < len)
|
|||
|
|
{
|
|||
|
|
if(len - totalLen > 1460)
|
|||
|
|
rslen = 1460;
|
|||
|
|
else
|
|||
|
|
rslen = len - totalLen;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ӻس<D3BB><D8B3><EFBFBD><EFBFBD>з<EFBFBD>
|
|||
|
|
if(!ssl)
|
|||
|
|
sprintf(cmd, "AT+QISEND=0,%d", rslen);
|
|||
|
|
else
|
|||
|
|
sprintf(cmd, "AT+QSSLSEND=1,%d", rslen);
|
|||
|
|
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
ret = FALSE;
|
|||
|
|
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) cmd, strlen(cmd));
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) "\r", 1);
|
|||
|
|
if(DTU_ParseResult(">", "", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
delay_ms(10);
|
|||
|
|
UART_Transmit(&huart1, data + totalLen, rslen);
|
|||
|
|
ret = DTU_ParseResult("SEND OK", "SEND FAIL", buff, DTU_tmrQPendSpec);
|
|||
|
|
}
|
|||
|
|
if(ret)
|
|||
|
|
{
|
|||
|
|
totalLen += rslen;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
} while(--try_count > 0);
|
|||
|
|
|
|||
|
|
if(!ret)
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "SEND");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(ret && rlen > 0)
|
|||
|
|
{
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
rslen = DTU_WaitResponse(ssl, rlen, recvBuff, recvSize, DTU_tmrQPendSpec);
|
|||
|
|
memmove(recvLen, &rslen, 2);
|
|||
|
|
ret = (rslen >= rlen);
|
|||
|
|
if(ret)
|
|||
|
|
break;
|
|||
|
|
delay_ms(200);
|
|||
|
|
} while(--try_count > 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(ret)
|
|||
|
|
{
|
|||
|
|
DTU_fail_count = 0;
|
|||
|
|
DTU_shutdown_count = 0;
|
|||
|
|
printf("\nSend %d bytes ok\n", len);
|
|||
|
|
// <20><>¼<EFBFBD><C2BC><EFBFBD>ͳɹ<CDB3><C9B9><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
DTU_succTime = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nSend failed\n");
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 2)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DTU<54>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>
|
|||
|
|
void DTU_CheckSleep()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void DTU_Sleep()
|
|||
|
|
{
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ģ<><C4A3><EFBFBD>ϵ磬<CFB5><E7A3AC>1<EFBFBD><31><EFBFBD>Ӳ<EFBFBD><D3B2>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
void DTU_PowerOn()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(!DTU_hasPowered)
|
|||
|
|
{
|
|||
|
|
printf("\nDTU power on ...\n");
|
|||
|
|
|
|||
|
|
delay_ms(200);
|
|||
|
|
VCC_GSM_ON();
|
|||
|
|
// delay_ms(10000);
|
|||
|
|
DTU_ParseResult("RDY", "", buff, DTU_tmrQPendShort * 3);
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>gps<70><73>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
DTU_gpsTime = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ñ<EFBFBD>־
|
|||
|
|
DTU_hasPowered = 1;
|
|||
|
|
|
|||
|
|
// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("ATE0", "OK", "", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nDTU echo off.\n");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
printf("\nDTU echo off failed.\n");
|
|||
|
|
delay_ms(2000);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>汾
|
|||
|
|
if(DTU_ExecuteCmd("ATI", "Revision: ", "", buff, DTU_tmrQPendShort))
|
|||
|
|
printf("\n%s\n", buff);
|
|||
|
|
|
|||
|
|
if(GPS_Waiting || dcBuff.configDisplay.op_SEND_GPS_DATA)
|
|||
|
|
{
|
|||
|
|
if(try_count > 0)
|
|||
|
|
delay_ms(2000);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GPS
|
|||
|
|
DTU_EnableGPS();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ģ<><C4A3><EFBFBD>ص<EFBFBD>
|
|||
|
|
void DTU_PowerOff()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
|
|||
|
|
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>GPS
|
|||
|
|
DTU_needCheckGPS = 0;
|
|||
|
|
|
|||
|
|
if(DTU_hasPowered)
|
|||
|
|
{
|
|||
|
|
// <20>ر<EFBFBD>GPS
|
|||
|
|
DTU_DisableGPS();
|
|||
|
|
|
|||
|
|
if(DTU_ExecuteCmd("AT+QPOWD=1", "POWERED DOWN", "", buff, DTU_tmrQPendShort * 2))
|
|||
|
|
printf("\nGPRS disconnected.\n");
|
|||
|
|
else
|
|||
|
|
printf("\nGPRS disconnect failed.\n");
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ñ<EFBFBD>־
|
|||
|
|
DTU_hasPowered = 0;
|
|||
|
|
DTU_imeiRead = 0;
|
|||
|
|
DTU_simCard = 0;
|
|||
|
|
DTU_simReg = 0;
|
|||
|
|
DTU_gprsReg = 0;
|
|||
|
|
DTU_isATMode = 0;
|
|||
|
|
DTU_gprsAttached = 0;
|
|||
|
|
DTU_linkEstablished = 0;
|
|||
|
|
DTU_sslConfig = 0;
|
|||
|
|
DTU_mqttConfig = 0;
|
|||
|
|
DTU_ipFetched = 0;
|
|||
|
|
strcpy(DTU_APN, "");
|
|||
|
|
DTU_isRMCFormat = 0;
|
|||
|
|
DTU_fail_count = 0;
|
|||
|
|
dcBuff.dtuData.connected = 0;
|
|||
|
|
DTU_shutdown_count = 0;
|
|||
|
|
|
|||
|
|
printf("\nDTU power off ...\n");
|
|||
|
|
VCC_GSM_OFF();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡIMEI<45><49><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>4Gģ<47><C4A3><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t DTU_ReadIMEI()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(DTU_imeiRead)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+GSN", "", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
if(strlen(buff) == 15)
|
|||
|
|
{
|
|||
|
|
DTU_imeiRead = 1;
|
|||
|
|
printf("\nThe IMEI is: %s\n", buff);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nRead IMEI failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>sim card
|
|||
|
|
uint32_t DTU_CheckSimCard()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 4;
|
|||
|
|
|
|||
|
|
if(DTU_simCard)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+CPIN?","+CPIN:", "", buff, DTU_tmrQPendShort) && strstr(buff, "+CPIN: READY"))
|
|||
|
|
{
|
|||
|
|
DTU_simCard = 1;
|
|||
|
|
printf("\nSim card ready.\n");
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DTU_simReg = 0;
|
|||
|
|
DTU_gprsReg = 0;
|
|||
|
|
DTU_gprsAttached = 0;
|
|||
|
|
DTU_linkEstablished = 0;
|
|||
|
|
DTU_sslConfig = 0;
|
|||
|
|
DTU_mqttConfig = 0;
|
|||
|
|
DTU_ipFetched = 0;
|
|||
|
|
printf("\nSim card not ready.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>GPRS<52><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>40<34><30>
|
|||
|
|
uint32_t DTU_CheckGprs()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint32_t check_time;
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
if(DTU_gprsAttached)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
check_time = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
|
|||
|
|
DTU_ExecuteCmd("AT+CGATT=1", "OK", "ERROR", buff, DTU_tmrQPendLong);
|
|||
|
|
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+CGATT?", "+CGATT: 1", "+CGATT: 0", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
DTU_gprsAttached = 1;
|
|||
|
|
printf("\nGprs attached.\n");
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delay_ms(1000);
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
} while(Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second) < check_time + 120);
|
|||
|
|
|
|||
|
|
DTU_gprsAttached = 0;
|
|||
|
|
DTU_linkEstablished = 0;
|
|||
|
|
DTU_sslConfig = 0;
|
|||
|
|
DTU_mqttConfig = 0;
|
|||
|
|
DTU_ipFetched = 0;
|
|||
|
|
printf("\nGprs not attached.\n");
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>1<EFBFBD><31>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 1)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>APN
|
|||
|
|
uint32_t DTU_SetAPN(char *APN)
|
|||
|
|
{
|
|||
|
|
char cmd[50], buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(strcmp(DTU_APN, APN) == 0)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QICSGP=1,1,\"%s\",\"\",\"\"", APN);
|
|||
|
|
if(DTU_ExecuteCmd(cmd, "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
strcpy(DTU_APN, APN);
|
|||
|
|
printf("\nSet APN to \"%s\".\n", APN);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nSet APN failed.\n");
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·
|
|||
|
|
uint32_t DTU_EstablishLink()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(DTU_linkEstablished)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+QIACT=1", "OK", "ERROR", buff, DTU_tmrQPendLong))
|
|||
|
|
{
|
|||
|
|
DTU_linkEstablished = 1;
|
|||
|
|
printf("\nGprs link established\n");
|
|||
|
|
|
|||
|
|
// DTU_ExecuteCmd("AT+QIDNSCFG=1", "OK", "ERROR", buff, DTU_tmrQPendShort);
|
|||
|
|
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DTU_ipFetched = 0;
|
|||
|
|
printf("\nGprs link not established.\n");
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 1)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t DTU_ConfigSSL()
|
|||
|
|
{
|
|||
|
|
char cmd[40], buff[40];
|
|||
|
|
|
|||
|
|
if(DTU_sslConfig)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>顢<EFBFBD>ϴ<EFBFBD>CA֤<41><D6A4>
|
|||
|
|
while(!DTU_ExecuteCmd("AT+QFLST=\"RAM:ca_cert.pem\"", "+QFLST: \"RAM:ca_cert.pem\"", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QFUPL=\"RAM:ca_cert.pem\",%d", strlen(ca_cert));
|
|||
|
|
if(DTU_ExecuteCmd(cmd, "CONNECT", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) ca_cert, strlen(ca_cert));
|
|||
|
|
if(DTU_ParseResult("+QFUPL:", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nca_cert.pem uploaded\n");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
printf("\nca_cert.pem upload failed\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>顢<EFBFBD>ϴ<EFBFBD>CLIENT֤<54><D6A4>
|
|||
|
|
while(!DTU_ExecuteCmd("AT+QFLST=\"RAM:client_cert.pem\"", "+QFLST: \"RAM:client_cert.pem\"", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QFUPL=\"RAM:client_cert.pem\",%d", strlen(client_cert));
|
|||
|
|
if(DTU_ExecuteCmd(cmd, "CONNECT", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) client_cert, strlen(client_cert));
|
|||
|
|
if(DTU_ParseResult("+QFUPL:", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nclient_cert.pem uploaded\n");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
printf("\nclient_cert.pem upload failed\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>顢<EFBFBD>ϴ<EFBFBD>CLIENT<4E><54>Կ
|
|||
|
|
while(!DTU_ExecuteCmd("AT+QFLST=\"RAM:client_key.pem\"", "+QFLST: \"RAM:client_key.pem\"", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QFUPL=\"RAM:client_key.pem\",%d", strlen(client_key));
|
|||
|
|
if(DTU_ExecuteCmd(cmd, "CONNECT", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) client_key, strlen(client_key));
|
|||
|
|
if(DTU_ParseResult("+QFUPL:", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nclient_key.pem uploaded\n");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
printf("\nclient_key.pem upload failed\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"ignorelocaltime\",1,1", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"sslversion\",1,4", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"ciphersuite\",1,0xFFFF", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"seclevel\",1,2", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"cacert\",1,\"RAM:ca_cert.pem\"", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"clientcert\",1,\"RAM:client_cert.pem\"", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QSSLCFG=\"clientkey\",1,\"RAM:client_key.pem\"", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
|
|||
|
|
DTU_sslConfig = 1;
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t DTU_ConfigMQTT(char *product, char *device, char *secret)
|
|||
|
|
{
|
|||
|
|
char cmd[100], buff[40], psn[14];
|
|||
|
|
|
|||
|
|
if(DTU_mqttConfig)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
sprintf(psn, "20%02d%02d%02d%02d%03d", dcBuff.configBottle.PSN[0], dcBuff.configBottle.PSN[1],
|
|||
|
|
dcBuff.configBottle.PSN[2], dcBuff.configBottle.PSN[3],
|
|||
|
|
(dcBuff.configBottle.PSN[4] << 8) | dcBuff.configBottle.PSN[5]);
|
|||
|
|
|
|||
|
|
// ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>¶<EFBFBD><C2B6><EFBFBD>topic
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QMTCFG=\"session\",0,1", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
// URCֻ<43><D6BB><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QMTCFG=\"recv/mode\",0,1,1", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><C8A8>
|
|||
|
|
sprintf(cmd, "AT+QMTCFG=\"aliauth\",0,\"%s\",\"%s\",\"%s\"",
|
|||
|
|
product, device, secret);
|
|||
|
|
if(!DTU_ExecuteCmd(cmd, "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
return FALSE;
|
|||
|
|
|
|||
|
|
DTU_mqttConfig = 1;
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>վ<EFBFBD><D5BE>λ<EFBFBD><CEBB>γ<EFBFBD><CEB3>
|
|||
|
|
uint32_t DTU_GetCellLocPosition()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
char *p, *p1 = NULL;
|
|||
|
|
|
|||
|
|
// <20><>ѯ<EFBFBD><D1AF>ǰmulti PDNs<4E>Ƿ<EFBFBD>enabled<65><64>Ĭ<EFBFBD><C4AC>enabled<65><64>
|
|||
|
|
// if(!DTU_ExecuteCmd("AT+QCFG=\"PDP/DuplicateChk\"", "+QCFG: \"pdp/duplicatechk\"", "ERROR", buff, DTU_tmrQPendShort)
|
|||
|
|
// || strstr(buff, ",1") == 0)
|
|||
|
|
// {
|
|||
|
|
// // <20><><EFBFBD>õ<EFBFBD>ǰmulti PDNsΪenabled
|
|||
|
|
// if(!DTU_ExecuteCmd("AT+QCFG=\"PDP/DuplicateChk\",1", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
// {
|
|||
|
|
// printf("\nSet multi PDNs failed.\n");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// // <20><>ѯ<EFBFBD><D1AF>ǰcontextid<69>Ƿ<EFBFBD>Ϊ1<CEAA><31>Ĭ<EFBFBD><C4AC>Ϊ1<CEAA><31>
|
|||
|
|
// if(!DTU_ExecuteCmd("AT+QLOCCFG=\"contextid\"", "+QLOCCFG: \"contextid\"", "ERROR", buff, DTU_tmrQPendShort)
|
|||
|
|
// || strstr(buff, ",1") == 0)
|
|||
|
|
// {
|
|||
|
|
// // <20><><EFBFBD>õ<EFBFBD>ǰcontextidΪ1
|
|||
|
|
// if(!DTU_ExecuteCmd("AT+QLOCCFG=\"contextid\",1", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
// {
|
|||
|
|
// printf("\nSet Cell-Loc contextid failed.\n");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// <20><><EFBFBD><EFBFBD>token<65><6E>Ĭ<EFBFBD><C4AC>Ϊ<EFBFBD>գ<EFBFBD>
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QLOCCFG=\"token\"", "+QLOCCFG: \"token\"", "ERROR", buff, DTU_tmrQPendShort)
|
|||
|
|
|| strstr(buff, "exist") == 0)
|
|||
|
|
{
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QLOCCFG=\"token\",\"u3S224CX18r4O045\"", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nSet Cell-Loc token failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// <20><>ѯ<EFBFBD><D1AF>ǰ<EFBFBD><C7B0>վ<EFBFBD><D5BE>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊwww.queclocator.com:80
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QLOCCFG=\"server\"", "+QLOCCFG: \"server\"", "ERROR", buff, DTU_tmrQPendShort)
|
|||
|
|
|| strstr(buff, "www.queclocator.com:80") == 0)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><C7B0>վ<EFBFBD><D5BE>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊwww.queclocator.com:80
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QLOCCFG=\"server\",\"www.queclocator.com:80\"", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nSet Cell-Loc server failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+QCELLLOC", "+QCELLLOC: ", "ERROR", buff, DTU_tmrQPendSpec))
|
|||
|
|
{
|
|||
|
|
p = strstr(buff, "+QCELLLOC: ");
|
|||
|
|
if(p)
|
|||
|
|
{
|
|||
|
|
p += 11;
|
|||
|
|
p1 = strstr(p, ",");
|
|||
|
|
if(p1)
|
|||
|
|
{
|
|||
|
|
dcBuff.dtuData.posState = 1; // <20><>վ<EFBFBD><D5BE>λ<EFBFBD>ɹ<EFBFBD>
|
|||
|
|
|
|||
|
|
*p1 = 0;
|
|||
|
|
dcBuff.dtuData.longitude = atof(p) * 1000000;
|
|||
|
|
p = p1 + 1;
|
|||
|
|
dcBuff.dtuData.latitude = atof(p) * 1000000;
|
|||
|
|
|
|||
|
|
printf("\nLatitude:\t%10.6f\n", dcBuff.dtuData.latitude * 0.000001);
|
|||
|
|
printf("Longitude:\t%10.6f\n", dcBuff.dtuData.longitude * 0.000001);
|
|||
|
|
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>token<65><6E><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
|
if(!DTU_ExecuteCmd("AT+QLOCCFG=\"token\",\"u3S224CX18r4O045\"", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nSet Cell-Loc token failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nGet Cell-Loc position failed.\n");
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>AT<41><54><EFBFBD><EFBFBD><EEA3AC><EFBFBD><CDB8><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
// <20><>!! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4Gģ<47><C4A3><EFBFBD><EFBFBD>Ч !!!
|
|||
|
|
uint32_t DTU_SetATMode()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(DTU_isATMode)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+QIMODE=0","OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
DTU_isATMode = 1;
|
|||
|
|
printf("\nSet to AT cmd mode.\n");
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nSet AT mode failed.\n");
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>IP<49><50>ַ
|
|||
|
|
uint32_t DTU_FetchIP()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(DTU_ipFetched)
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+QIACT?", "+QIACT: 1,1", "+QIACT: 1,0", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
DTU_ipFetched = 1;
|
|||
|
|
printf("\nLocal ip is: %s\n", buff + 14);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nFetch ip failed.\n");
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽΪDNS
|
|||
|
|
// <20><>!! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4Gģ<47><C4A3><EFBFBD><EFBFBD>Ч !!!
|
|||
|
|
uint32_t DTU_SetDNSIP()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+QIDNSIP=1", "OK", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nSet remote server to domain name.\n");
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nSet remote server to domain name failed.\n");
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Direct push mode
|
|||
|
|
uint32_t DTU_Connect(uint8_t ssl, char *server, short port)
|
|||
|
|
{
|
|||
|
|
char cmd[80], buff[40];
|
|||
|
|
uint32_t ret = FALSE;
|
|||
|
|
uint8_t try_count = 1;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(!ssl)
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,1", server, port);
|
|||
|
|
if(!DTU_ExecuteCmd(cmd, "+QIOPEN: 0,", "ERROR", buff, DTU_tmrQPendSpec))
|
|||
|
|
break;
|
|||
|
|
if(strstr(buff, "+QIOPEN: 0,0"))
|
|||
|
|
{
|
|||
|
|
ret = TRUE;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QSSLOPEN=1,1,1,\"%s\",%d,1", server, port);
|
|||
|
|
if(!DTU_ExecuteCmd(cmd, "+QSSLOPEN: 1,", "ERROR", buff, DTU_tmrQPendSpec))
|
|||
|
|
break;
|
|||
|
|
if(strstr(buff, "+QSSLOPEN: 1,0"))
|
|||
|
|
{
|
|||
|
|
ret = TRUE;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(ret)
|
|||
|
|
{
|
|||
|
|
dcBuff.dtuData.connected = 1;
|
|||
|
|
printf("\nConnect to %s: %d\n", server, port);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dcBuff.dtuData.connected = 0;
|
|||
|
|
printf("\nCan not connect to %s: %d\n", server, port);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 2)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t DTU_Close(uint8_t ssl)
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
uint8_t ret;
|
|||
|
|
|
|||
|
|
if(!dcBuff.dtuData.connected)
|
|||
|
|
return TRUE;
|
|||
|
|
dcBuff.dtuData.connected = 0;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>OK<4F><4B>Ҳ<EFBFBD><D2B2><EFBFBD>ܷ<EFBFBD><DCB7>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹرգ<D8B1><D5A3><EFBFBD><F2B1BBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رգ<D8B1><D5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>رճɹ<D5B3>
|
|||
|
|
if(!ssl)
|
|||
|
|
ret = DTU_ExecuteCmd("AT+QICLOSE=0", "OK", "ERROR", buff, DTU_tmrQPendSpec);
|
|||
|
|
else
|
|||
|
|
ret = DTU_ExecuteCmd("AT+QSSLCLOSE=1", "OK", "ERROR", buff, DTU_tmrQPendSpec);
|
|||
|
|
if(ret)
|
|||
|
|
{
|
|||
|
|
printf("\nConnection closed.\n");
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nConnection close failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡsim<69><6D><EFBFBD>绰<EFBFBD><E7BBB0><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t DTU_ReadPhoneNo()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(strlen(dcBuff.powerInfo.simNumber))
|
|||
|
|
return TRUE;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+QCCID", "+QCCID: ", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.simNumber, buff + 8);
|
|||
|
|
printf("\nThe phone no is: %s\n", dcBuff.powerInfo.simNumber);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nRead phone no failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD>ź<EFBFBD>ǿ<EFBFBD><C7BF>
|
|||
|
|
uint32_t DTU_ReadRssi(uint8_t *rssi)
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 6;
|
|||
|
|
char *pCSQ, *pCommon;
|
|||
|
|
uint8_t csq;
|
|||
|
|
|
|||
|
|
*rssi = 0;
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+CSQ", "+CSQ:", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
pCSQ = strstr(buff, "+CSQ: ");
|
|||
|
|
pCommon = strstr(buff, ",");
|
|||
|
|
if(pCommon)
|
|||
|
|
*pCommon = 0;
|
|||
|
|
// <20><>0~31<33><31>ǿ<EFBFBD><C7BF>ֵת<D6B5><D7AA>Ϊ<EFBFBD>ٷֱ<D9B7>
|
|||
|
|
csq = atoi(pCSQ + 6);
|
|||
|
|
printf("\nThe CSQ is: %d\n", csq);
|
|||
|
|
if(csq != 99 && csq != 199)
|
|||
|
|
{
|
|||
|
|
if(csq < 100)
|
|||
|
|
{
|
|||
|
|
if(csq <= 31)
|
|||
|
|
*rssi = csq * 100 / 31;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if(csq <= 191)
|
|||
|
|
*rssi = csq - 100 + 9;
|
|||
|
|
}
|
|||
|
|
if(*rssi > 100)
|
|||
|
|
*rssi = 100;
|
|||
|
|
printf("\nThe rssi is: %d\n", *rssi);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
delay_ms(500);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nRead rssi failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|||
|
|
uint32_t DTU_ReadRegistry(uint8_t *reg)
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
char *pReg;
|
|||
|
|
uint32_t check_time;
|
|||
|
|
uint16_t DTU_regTime = 60;
|
|||
|
|
uint8_t rssi = 0;
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
if(DTU_simReg)
|
|||
|
|
{
|
|||
|
|
*reg = 1;
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DTU_ReadRssi(&rssi);
|
|||
|
|
if(rssi >= 50)
|
|||
|
|
DTU_regTime = 150;
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
check_time = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
|
|||
|
|
*reg = 0;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+CREG?", "+CREG:", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
pReg = strstr(buff, ",");
|
|||
|
|
if(pReg)
|
|||
|
|
{
|
|||
|
|
*reg = *(pReg + 1) - '0';
|
|||
|
|
if(*reg == 0)
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
if(*reg == 1 || *reg == 5)
|
|||
|
|
{
|
|||
|
|
printf("\nRegistry status: %d.\n", *reg);
|
|||
|
|
*reg = 1;
|
|||
|
|
DTU_simReg = 1;
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delay_ms(1000);
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
} while(Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second) < check_time + DTU_regTime);
|
|||
|
|
|
|||
|
|
printf("\nRegistry status: %d.\n", *reg);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 2)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
DTU_gprsReg = 0;
|
|||
|
|
DTU_gprsAttached = 0;
|
|||
|
|
DTU_linkEstablished = 0;
|
|||
|
|
DTU_sslConfig = 0;
|
|||
|
|
DTU_mqttConfig = 0;
|
|||
|
|
DTU_ipFetched = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡGPRS<52><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|||
|
|
uint32_t DTU_ReadGPRSRegistry(uint8_t *reg)
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
char *pReg;
|
|||
|
|
uint32_t check_time;
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
if(DTU_gprsReg)
|
|||
|
|
{
|
|||
|
|
*reg = 1;
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
check_time = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
|
|||
|
|
*reg = 0;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
if(DTU_ExecuteCmd("AT+CGREG?", "+CGREG:", "", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
pReg = strstr(buff, ",");
|
|||
|
|
if(pReg)
|
|||
|
|
{
|
|||
|
|
*reg = *(pReg + 1) - '0';
|
|||
|
|
if(*reg == 0)
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
if(*reg == 1 || *reg == 5)
|
|||
|
|
{
|
|||
|
|
printf("\nGPRS registry status: %d.\n", *reg);
|
|||
|
|
*reg = 1;
|
|||
|
|
DTU_gprsReg = 1;
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delay_ms(1000);
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
} while(Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second) < check_time + 60);
|
|||
|
|
|
|||
|
|
printf("\nGPRS registry status: %d.\n", *reg);
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
char *DTU_DecideAPN()
|
|||
|
|
{
|
|||
|
|
if(strncmp(dcBuff.powerInfo.simNumber, "898604", 6) == 0)
|
|||
|
|
return "CMIOT";
|
|||
|
|
if(strncmp(dcBuff.powerInfo.simNumber, "898607", 6) == 0)
|
|||
|
|
return "CMIOT";
|
|||
|
|
if(strncmp(dcBuff.powerInfo.simNumber, "898602", 6) == 0)
|
|||
|
|
return "CMIOT";
|
|||
|
|
if(strncmp(dcBuff.powerInfo.simNumber, "898600", 6) == 0)
|
|||
|
|
return "CMIOT";
|
|||
|
|
if(strncmp(dcBuff.powerInfo.simNumber, "898608", 6) == 0)
|
|||
|
|
return "CMIOT";
|
|||
|
|
if(strcmp(dcBuff.configDisplay.APN, "CMNET") == 0)
|
|||
|
|
return "CMIOT";
|
|||
|
|
return dcBuff.configDisplay.APN;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡGPRS<52><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>Ϣ
|
|||
|
|
uint32_t Sim808_ReadGPRS()
|
|||
|
|
{
|
|||
|
|
uint8_t val;
|
|||
|
|
|
|||
|
|
printf("\nRead RSSI data ....\n");
|
|||
|
|
DTU_PowerOn();
|
|||
|
|
if(!DTU_ReadIMEI())
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "EC20");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
if(!DTU_CheckSimCard())
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "SIM");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
DTU_ReadPhoneNo();
|
|||
|
|
if(!DTU_SetAPN(DTU_DecideAPN()))
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "APN");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
if(!DTU_ReadRegistry(&val))
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "REG");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
dcBuff.dtuData.networked = (val == 1 ? 1 : 0);
|
|||
|
|
if(DTU_ReadRssi(&val))
|
|||
|
|
dcBuff.dtuData.rssi = val;
|
|||
|
|
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void DTU_setOffsetSeconds()
|
|||
|
|
{
|
|||
|
|
// char buff[40], *p;
|
|||
|
|
// S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
// uint32_t totalSeconds1, totalSeconds2;
|
|||
|
|
// int8_t zz;
|
|||
|
|
|
|||
|
|
// if(DTU_ExecuteCmd("AT+QLTS=2", "+QLTS: ", "ERROR", buff, DTU_tmrQPendShort)) // Get UTC time
|
|||
|
|
// {
|
|||
|
|
// // <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
// RTC_GetDateAndTime(&sRTC);
|
|||
|
|
// totalSeconds1 = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
// sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
|
|||
|
|
// // UTC<54><43><EFBFBD><EFBFBD>
|
|||
|
|
// p = strstr(buff, "+QLTS: \"") + 8;
|
|||
|
|
// printf("\n%s\n", p);
|
|||
|
|
// // 4λ<34><CEBB><EFBFBD><EFBFBD>
|
|||
|
|
// p[4] = 0;
|
|||
|
|
// sRTC.u32Year = atoi(p);
|
|||
|
|
// p += 5;
|
|||
|
|
// // 2λ<32>·<EFBFBD>
|
|||
|
|
// p[2] = 0;
|
|||
|
|
// sRTC.u32Month = atoi(p);
|
|||
|
|
// p += 3;
|
|||
|
|
// // 2λ<32><CEBB><EFBFBD><EFBFBD>
|
|||
|
|
// p[2] = 0;
|
|||
|
|
// sRTC.u32Day = atoi(p);
|
|||
|
|
// p += 3;
|
|||
|
|
// // 2λСʱ
|
|||
|
|
// p[2] = 0;
|
|||
|
|
// sRTC.u32Hour = atoi(p);
|
|||
|
|
// p += 3;
|
|||
|
|
// // 2λ<32><CEBB><EFBFBD><EFBFBD>
|
|||
|
|
// p[2] = 0;
|
|||
|
|
// sRTC.u32Minute = atoi(p);
|
|||
|
|
// p += 3;
|
|||
|
|
// // 2λ<32><CEBB>
|
|||
|
|
// sRTC.u32Second = (*p - '0') * 10 + (*(p + 1) - '0');
|
|||
|
|
// p += 2;
|
|||
|
|
// // 3λʱ<CEBB><EFBFBD><EEA3A8>1/4Сʱ, <20><><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>
|
|||
|
|
// p[3] = 0;
|
|||
|
|
// zz = atoi(p);
|
|||
|
|
//
|
|||
|
|
// totalSeconds2 = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
// sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
|
|||
|
|
// // <20><>UTCʱ<43><CAB1>ת<EFBFBD><D7AA>ΪGMTʱ<54>䣨ʱ<E4A3A8><CAB1>Ϊ0<CEAA><30>
|
|||
|
|
// totalSeconds2 -= zz * 900;
|
|||
|
|
// // <20><>GMTʱ<54><CAB1>ת<EFBFBD><D7AA>ΪUTCʱ<43>䣨<EFBFBD><E4A3A8><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ʱ<EFBFBD>
|
|||
|
|
// totalSeconds2 += dcBuff.configData.timeLag * 3600;
|
|||
|
|
|
|||
|
|
// if(RTC_offsetSeconds == 0)
|
|||
|
|
// RTC_offsetSeconds = totalSeconds2 - totalSeconds1;
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GPRS<52><53><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t Sim808_GPRSDial()
|
|||
|
|
{
|
|||
|
|
uint8_t reg;
|
|||
|
|
|
|||
|
|
printf("\nGPRS Dialing ....\n");
|
|||
|
|
// DTU_PowerOn();
|
|||
|
|
// if(!DTU_ReadIMEI())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "EC20");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// if(!DTU_CheckSimCard())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "SIM");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// if(!DTU_SetAPN(DTU_DecideAPN()))
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "APN");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// if(!DTU_ReadRegistry(®))
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "REG");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// DTU_ReadGPRSRegistry(®);
|
|||
|
|
//// if(!DTU_CheckGprs())
|
|||
|
|
//// {
|
|||
|
|
//// strcpy(dcBuff.powerInfo.gprsFailCode, "GPRS");
|
|||
|
|
//// return FALSE;
|
|||
|
|
//// }
|
|||
|
|
//// if(!DTU_SetATMode())
|
|||
|
|
//// {
|
|||
|
|
//// strcpy(dcBuff.powerInfo.gprsFailCode, "AT");
|
|||
|
|
//// return FALSE;
|
|||
|
|
//// }
|
|||
|
|
// if(!DTU_EstablishLink())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "ACT");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ʱ<EFBFBD>䣩
|
|||
|
|
// if(RTC_offsetSeconds == 0)
|
|||
|
|
// DTU_setOffsetSeconds();
|
|||
|
|
// if(RTC_offsetSeconds == 0)
|
|||
|
|
// DTU_setOffsetSecondsFromServer();
|
|||
|
|
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t Sim808_Connect(uint8_t ssl, char *server, int port)
|
|||
|
|
{
|
|||
|
|
if(!Sim808_GPRSDial())
|
|||
|
|
return FALSE;
|
|||
|
|
|
|||
|
|
// if(!DTU_FetchIP())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "IP");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// if(!DTU_SetDNSIP())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "DNS");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
if(ssl && !DTU_ConfigSSL())
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "SSL");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nConnect to server....\n");
|
|||
|
|
|
|||
|
|
if(!DTU_Connect(ssl, server, port))
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "CONN");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>վ<EFBFBD><D5BE>λ<EFBFBD><CEBB>γ<EFBFBD><CEB3>
|
|||
|
|
uint32_t Sim808_GetCellLocPosition()
|
|||
|
|
{
|
|||
|
|
if(!Sim808_GPRSDial())
|
|||
|
|
return FALSE;
|
|||
|
|
|
|||
|
|
printf("\nGet Cell-Loc position....\n");
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>վ<EFBFBD><D5BE>λ<EFBFBD><CEBB>γ<EFBFBD><CEB3>
|
|||
|
|
return DTU_GetCellLocPosition();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t Sim808_SendAndRecv(uint8_t ssl, uint8_t *data, uint16_t len, uint16_t *recvLen, uint8_t *recvBuff, uint16_t recvSize)
|
|||
|
|
{
|
|||
|
|
return DTU_SendAndRecv(ssl, data, len, recvLen, recvBuff, recvSize);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// EC20һ<30><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>1500<30>ֽ<EFBFBD>
|
|||
|
|
uint32_t DTU_MqttPublish(uint8_t *data, uint16_t len, char *product, char *device)
|
|||
|
|
{
|
|||
|
|
static uint16_t msgid = 1;
|
|||
|
|
char cmd[100], buff[40], psn[14];
|
|||
|
|
int8_t try_count = 2;
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
sprintf(psn, "20%02d%02d%02d%02d%03d", dcBuff.configBottle.PSN[0], dcBuff.configBottle.PSN[1],
|
|||
|
|
dcBuff.configBottle.PSN[2], dcBuff.configBottle.PSN[3],
|
|||
|
|
(dcBuff.configBottle.PSN[4] << 8) | dcBuff.configBottle.PSN[5]);
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
|
// UART_Transmit(&huart1, (uint8_t *) "AT\r", 3);
|
|||
|
|
// delay_ms(5);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
DTU_ClearQueue();
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ӻس<D3BB><D8B3><EFBFBD><EFBFBD>з<EFBFBD>
|
|||
|
|
sprintf(cmd, "AT+QMTPUBEX=0,%d,1,0,\"/sys/%s/%s/thing/model/up_raw\",%d",
|
|||
|
|
msgid, product, device, len);
|
|||
|
|
// printf("\n%s\n", cmd);
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) cmd, strlen(cmd));
|
|||
|
|
UART_Transmit(&huart1, (uint8_t *) "\r", 1);
|
|||
|
|
if(DTU_ParseResult(">", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
delay_ms(10);
|
|||
|
|
UART_Transmit(&huart1, data, len);
|
|||
|
|
if(DTU_ParseResult("+QMTPUBEX: ", "ERROR", buff, DTU_tmrQPendSpec))
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "+QMTPUBEX: 0,%d,0", msgid);
|
|||
|
|
if(strstr(buff, cmd))
|
|||
|
|
{
|
|||
|
|
DTU_fail_count = 0;
|
|||
|
|
DTU_shutdown_count = 0;
|
|||
|
|
printf("\nSend %d bytes ok\n", len);
|
|||
|
|
msgid++;
|
|||
|
|
if(msgid == 0)
|
|||
|
|
msgid++;
|
|||
|
|
// <20><>¼<EFBFBD><C2BC><EFBFBD>ͳɹ<CDB3><C9B9><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
DTU_succTime = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "SEND");
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nSend failed\n");
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 2)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t Sim808_MqttPublish(uint8_t *data, uint16_t len, char *product, char *device)
|
|||
|
|
{
|
|||
|
|
return DTU_MqttPublish(data, len, product, device);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t MQTT_Close()
|
|||
|
|
{
|
|||
|
|
char buff[40];
|
|||
|
|
uint8_t try_count = 2;
|
|||
|
|
|
|||
|
|
if(!dcBuff.dtuData.connected)
|
|||
|
|
return TRUE;
|
|||
|
|
dcBuff.dtuData.connected = 0;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>OK<4F><4B>Ҳ<EFBFBD><D2B2><EFBFBD>ܷ<EFBFBD><DCB7>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹرգ<D8B1><D5A3><EFBFBD><F2B1BBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رգ<D8B1><D5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>رճɹ<D5B3>
|
|||
|
|
if(DTU_ExecuteCmd("AT+QMTDISC=0","+QMTDISC: 0,0", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
printf("\nConnection closed.\n");
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nConnection close failed.\n");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MQTT
|
|||
|
|
uint32_t DTU_MqttConnect(const char *server, int port)
|
|||
|
|
{
|
|||
|
|
char cmd[80], buff[40];
|
|||
|
|
uint8_t try_count = 1;
|
|||
|
|
|
|||
|
|
while(try_count--)
|
|||
|
|
{
|
|||
|
|
sprintf(cmd, "AT+QMTOPEN=0,\"%s\",%d", server, port);
|
|||
|
|
if(DTU_ExecuteCmd(cmd, "+QMTOPEN: 0,0", "ERROR", buff, DTU_tmrQPendSpec)
|
|||
|
|
&& DTU_ExecuteCmd("AT+QMTCONN=0,\"Anjiehui_4G\"","+QMTCONN: 0,0,0", "ERROR", buff, DTU_tmrQPendShort))
|
|||
|
|
{
|
|||
|
|
dcBuff.dtuData.connected = 1;
|
|||
|
|
printf("\nConnect to %s:%d\n", server, port);
|
|||
|
|
return TRUE;
|
|||
|
|
}
|
|||
|
|
delay_ms(200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dcBuff.dtuData.connected = 0;
|
|||
|
|
printf("\nCan not connect to %s:%d\n", server, port);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32>ʧ<EFBFBD>ܣ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ػ<EFBFBD>/<2F>ص<EFBFBD>
|
|||
|
|
if(++DTU_fail_count >= 2)
|
|||
|
|
{
|
|||
|
|
if(DTU_shutdown_count >= 1 || !DTU_Shutdown())
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t Sim808_MqttConnect(const char *server, int port, char *product, char *device, char *secret)
|
|||
|
|
{
|
|||
|
|
if(!Sim808_GPRSDial())
|
|||
|
|
return FALSE;
|
|||
|
|
|
|||
|
|
// if(!DTU_FetchIP())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "IP");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
// if(!DTU_SetDNSIP())
|
|||
|
|
// {
|
|||
|
|
// strcpy(dcBuff.powerInfo.gprsFailCode, "DNS");
|
|||
|
|
// return FALSE;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
if(!DTU_ConfigMQTT(product, device, secret))
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "MQTT");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nConnect to MQTT-Server....\n");
|
|||
|
|
|
|||
|
|
if(!DTU_MqttConnect(server, port))
|
|||
|
|
{
|
|||
|
|
strcpy(dcBuff.powerInfo.gprsFailCode, "CONN");
|
|||
|
|
return FALSE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TRUE;
|
|||
|
|
}
|