|
@@ -13,27 +13,27 @@
|
|
|
* Created on: 2022年5月11日
|
|
|
* Author: cw
|
|
|
*/
|
|
|
-#include "AppTaskUart1.h"
|
|
|
#include "stdio.h"
|
|
|
+#include "AppTaskUart1.h"
|
|
|
/***************************************************
|
|
|
-esp8266使用的串口1
|
|
|
-esp8266默认TCP默认断开时间为180s
|
|
|
-
|
|
|
-主机配置
|
|
|
-ESP8266作服务器时,IP固定为192.168.4.1
|
|
|
-AT+CWMODE=2 //设置模块 WIFI 模式为 AP 模式
|
|
|
-AT+CWSAP_DEF=”my_test1”,”12345678”,1,4 //设置模块的 AP 参数:SSID 为my_test1,密码为12345678,通道号为 1,加密方式为:WPA_WPA2_PSK
|
|
|
-AT+CIPMUX=1 //开启多连接
|
|
|
-AT+CIPSERVER=1,8086 //开启 SERVER 模式,设置端口为 8086
|
|
|
-AT+CIPSEND=0,25 //向 ID0 发送 25 字节数据包
|
|
|
-
|
|
|
-从机配置
|
|
|
-AT+CWMODE=1 //设置模块 WIFI 模式为 STA 模式
|
|
|
-AT+CWJAP_DEF="my_test1","12345678" //加入 WIFI 热点
|
|
|
-AT+CIPMUX=0 //开启单连接
|
|
|
-AT+CIPSTART="TCP","192.168.4.1",8086 //建立 TCP 连接到” 192.168.4.1”,8086
|
|
|
-AT+CIPMODE=1 //开启透传模式
|
|
|
-AT+CIPSEND //开始传输
|
|
|
+* esp8266使用的串口1
|
|
|
+* esp8266默认TCP默认断开时间为180s
|
|
|
+
|
|
|
+* 主机配置
|
|
|
+* ESP8266作服务器时,IP固定为192.168.4.1
|
|
|
+* AT+CWMODE=2 //设置模块 WIFI 模式为 AP 模式
|
|
|
+* AT+CWSAP_DEF=”my_test1”,”12345678”,1,4 //设置模块的 AP 参数:SSID 为my_test1,密码为12345678,通道号为 1,加密方式为:WPA_WPA2_PSK
|
|
|
+* AT+CIPMUX=1 //开启多连接
|
|
|
+* AT+CIPSERVER=1,8086 //开启 SERVER 模式,设置端口为 8086
|
|
|
+* AT+CIPSEND=0,25 //向 ID0 发送 25 字节数据包
|
|
|
+
|
|
|
+* 从机配置
|
|
|
+* AT+CWMODE=1 //设置模块 WIFI 模式为 STA 模式
|
|
|
+* AT+CWJAP_DEF="my_test1","12345678" //加入 WIFI 热点
|
|
|
+* AT+CIPMUX=0 //开启单连接
|
|
|
+* AT+CIPSTART="TCP","192.168.4.1",8086 //建立 TCP 连接到” 192.168.4.1”,8086
|
|
|
+* AT+CIPMODE=1 //开启透传模式
|
|
|
+* AT+CIPSEND //开始传输
|
|
|
****************************************************/
|
|
|
typedef sint8 (*pFunc)(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen);
|
|
|
|
|
@@ -46,56 +46,31 @@ typedef struct
|
|
|
static void wifi_init(uint8 Step);
|
|
|
static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, sint8 *retFunc);
|
|
|
static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen);
|
|
|
-static void wifiDataDecode(uint8 *dataIn,uint16 dataLen);
|
|
|
-uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd);
|
|
|
+static uint8 wifiDataDecode(uint8 *dataIn,uint16 dataLen);
|
|
|
+
|
|
|
sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen);
|
|
|
volatile uint32 TimerCounter = 0; //ms单位
|
|
|
-sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen);
|
|
|
-void GlobeVariableHandle_W1(void);
|
|
|
-
|
|
|
+static sint8 TCP_ConnectEstablish(uint32 timeoutMs);
|
|
|
+static sint8 WiFiDisconnec(void);
|
|
|
+static void WiFiStaMan(void);
|
|
|
/* 获取x中第i个字节 */
|
|
|
#define GET_LOW_BYTE(x,i) ((x >> i*8) & 0x000000FF)
|
|
|
|
|
|
-/* 时间全局变量 */
|
|
|
-uint8 timeArray[8] = {0};
|
|
|
-/* 车量编码 */
|
|
|
-uint8 dataVinInfo[17] = {0};
|
|
|
-
|
|
|
-typedef enum
|
|
|
-{
|
|
|
- TCP_IDLE = 0,
|
|
|
- TCP_OPEN_WIFI,
|
|
|
- TCP_SERVICES_OK,
|
|
|
- TCP_SERVICES_CLOSE,
|
|
|
-}TCP_SERVER;
|
|
|
-
|
|
|
-TCP_SERVER tcp_server_process_status;
|
|
|
-
|
|
|
-
|
|
|
|
|
|
const ATCmdFunc_t Atcmdfunc[] =
|
|
|
{
|
|
|
{AT_CMD_TEST, "AT\r\n", at_callbackFunc},
|
|
|
{ATE_0, "ATE0\r\n", at_callbackFunc},
|
|
|
-#if WIFI_MASTER
|
|
|
- {AT_SET_MODE, "AT+CWMODE=2\r\n", at_callbackFunc}, /* 建立SoftAP 模式 */
|
|
|
- {AT_OPEN_MASTER_WIFI, "AT+CWSAP_DEF=", at_callbackFunc},//开启WIFI
|
|
|
- {AT_SET_MUX, "AT+CIPMUX=1\r\n", at_callbackFunc}, /* 设置多链接模式 */
|
|
|
- {AT_CLOSE_SERVER, "AT+CIPSERVER=0\r\n", at_callbackFunc},
|
|
|
- {AT_OPEN_SERVER, "AT+CIPSERVER=", at_callbackFunc}, /* 打开服务器端口 */
|
|
|
- {AT_CIPAP_DEF, "AT+CIPAP_DEF=", at_callbackFunc}, /* 打开服务器端口 */
|
|
|
- {AT_TCP_CLOSE, "AT+CIPCLOSE=5\r\n", at_callbackFunc},
|
|
|
- {AT_CHECK_LINK_STATUS, "AT+CIPSTATUS\r\n", at_callbackFunc},
|
|
|
-#elif WIFI_SLAVE
|
|
|
{AT_SET_MODE, "AT+CWMODE=1\r\n", at_callbackFunc}, /* 建立SoftAP 模式 */
|
|
|
{AT_CWAUTOCONN, "AT+CWAUTOCONN=0\r\n", at_callbackFunc}, /* 建立Soft Station 模式 */
|
|
|
{AT_CWJAP, "AT+CWJAP=", at_callbackFunc}, /* 连接WiFi */
|
|
|
{AT_CIPSTART, "AT+CIPSTART=", at_callbackFunc}, /* 连接IP */
|
|
|
{AT_CIPMODE, "AT+CIPMODE=1\r\n", at_callbackFunc}, /* 设置透传模式 */
|
|
|
{AT_CIPSEND, "AT+CIPSEND\r\n", at_callbackFunc}, /* 开始发送数据 */
|
|
|
-#endif
|
|
|
{AT_SEND_DATA, "AT+CIPSEND=0,", at_callbackFunc},
|
|
|
{AT_CIFSR, "AT+CIFSR\r\n", at_callbackFunc}, /* 查看IP信息 */
|
|
|
+ {AT_CWQAP, "AT+CWQAP\r\n", at_callbackFunc}, /* 断开AP连接 */
|
|
|
+ {AT_ISNOCON, "AT+CWJAP?\r\n", at_callbackFunc}, /* 连接WiFi */
|
|
|
};
|
|
|
|
|
|
|
|
@@ -104,83 +79,44 @@ const ATCmdFunc_t Atcmdfunc[] =
|
|
|
void wifi_task(void *pvParameters)
|
|
|
{
|
|
|
(void)pvParameters;
|
|
|
- sint8 ATRet = -1;
|
|
|
uint16 ReadLen = 0;
|
|
|
- uint8 AtCmdRead[150] = {0};
|
|
|
uint8 UartDataBuffer[512] = {0};
|
|
|
//TimerHandle_t softTimer1 = xTimerCreate("softTimer1", 10, pdTRUE, (void *)0, vTimerCallback);
|
|
|
+
|
|
|
+ /* 等待钥匙下电 */
|
|
|
+ if(1 == KeySignal)
|
|
|
+ {
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(10));
|
|
|
+ }
|
|
|
+
|
|
|
Dio_WriteChannel(DioConf_DioChannel_PTD16_GPIO_OUT_MCU_WIFI_POW_EN, STD_ON);//wifi开机
|
|
|
vTaskDelay(pdMS_TO_TICKS(3000));//开机后等一段时间模块可能会返回开机后的指令 不做处理
|
|
|
+ tcp_server_process_status = TCP_UNCONNECT;
|
|
|
wifi_init(0);
|
|
|
- #if WIFI_MASTER
|
|
|
- tcp_server_process_status = TCP_IDLE;
|
|
|
- #elif WIFI_SLAVE
|
|
|
- tcp_server_process_status = TCP_SERVICES_OK;
|
|
|
- #endif
|
|
|
- uint16 pReadLen = 0;
|
|
|
+ //TCP_ConnectEstablish(0);
|
|
|
while(1)
|
|
|
{
|
|
|
|
|
|
- GlobeVariableHandle_W1();
|
|
|
+ WiFiStaMan();
|
|
|
switch(tcp_server_process_status)
|
|
|
{
|
|
|
- case TCP_IDLE:
|
|
|
- {
|
|
|
-#if WIFI_MASTER /* 等待从机加入wifi,等待从机连接服务器 */
|
|
|
- memset(UartDataBuffer,0x00,sizeof(UartDataBuffer));
|
|
|
- UART_Receive_Data(UART_LPUART1,(uint8*)UartDataBuffer,(uint8*)&pReadLen,100);
|
|
|
- if(pReadLen>0)
|
|
|
- {
|
|
|
- if(strstr((char *)UartDataBuffer,(char *)("+STA_CONNECTED:")))//从机连上wifi
|
|
|
- {
|
|
|
- tcp_server_process_status = TCP_IDLE;
|
|
|
- }
|
|
|
- else if(strstr((char *)UartDataBuffer,(char *)("+STA_DISCONNECTED:"))\
|
|
|
- ||strstr((char *)UartDataBuffer,(char *)("CLOSED")))//从机断开wifi
|
|
|
- {
|
|
|
- tcp_server_process_status = TCP_IDLE;
|
|
|
- }
|
|
|
- else if(strstr((char *)UartDataBuffer,(char *)("CONNECT"))) //从机tcp连上服务器
|
|
|
- {
|
|
|
- tcp_server_process_status = TCP_SERVICES_OK;
|
|
|
- wifiConnFlg = true;
|
|
|
- }
|
|
|
- }
|
|
|
-#elif WIFI_SLAVE /* 断开WiFi和TCP链接后此处重启 */
|
|
|
- wifi_init(3);
|
|
|
- tcp_server_process_status = TCP_SERVICES_OK;
|
|
|
-#endif
|
|
|
- break;
|
|
|
- }
|
|
|
- case TCP_SERVICES_OK://可以正常进行数据交互
|
|
|
+ case TCP_CONNECT://可以正常进行数据交互
|
|
|
{
|
|
|
+
|
|
|
/*********主机收到上位机的指令,向WIFI从机发送数据*********/
|
|
|
- if(wifiAskFlg)
|
|
|
- {
|
|
|
- /* 此处处理来自串口0的数据 */
|
|
|
- wifiAskFlg = false;
|
|
|
- }
|
|
|
/*********************主机收到WIFI从机的数据****************/
|
|
|
memset(UartDataBuffer, 0x00, sizeof(UartDataBuffer));
|
|
|
- UART_Receive_Data(UART_LPUART1,(uint8*)UartDataBuffer,(uint8*)&ReadLen,100);
|
|
|
+ UART_Receive_Data(UART_LPUART1,(uint8*)UartDataBuffer,(uint16*)&ReadLen,100);
|
|
|
if(ReadLen>0)
|
|
|
{
|
|
|
-#if WIFI_MASTER
|
|
|
- if(strstr((char *)UartDataBuffer,(char *)("+STA_DISCONNECTED")))//从机tcp断开
|
|
|
- {
|
|
|
- tcp_server_process_status = TCP_IDLE;
|
|
|
- wifiAskFlg = false;
|
|
|
- }
|
|
|
-#elif WIFI_SLAVE
|
|
|
if(strstr((char *)UartDataBuffer,(char *)("WIFI DISCONNECT")))/* WiFi断开 */
|
|
|
{
|
|
|
- tcp_server_process_status = TCP_SERVICES_CLOSE;
|
|
|
+ tcp_server_process_status = TCP_UNCONNECT;
|
|
|
wifiAskFlg = false;
|
|
|
}
|
|
|
-#endif /* #if WIFI_MASTER */
|
|
|
else if(strstr((char *)UartDataBuffer,(char *)("CLOSED"))) /* 网络链接断开 */
|
|
|
{
|
|
|
- tcp_server_process_status = TCP_IDLE;
|
|
|
+ tcp_server_process_status = TCP_UNCONNECT;
|
|
|
wifiAskFlg = false;
|
|
|
}
|
|
|
else if(strstr((char *)UartDataBuffer, (char *)("+IPD"))) //收到从机的数据
|
|
@@ -201,28 +137,13 @@ void wifi_task(void *pvParameters)
|
|
|
}
|
|
|
/* 数据解码 */
|
|
|
wifiDataDecode(retptr+1,TcpDataLen);
|
|
|
-
|
|
|
- QueueDataTrans_Type DataSend={0, NULL};
|
|
|
- DataSend.RealLen = TcpDataLen;
|
|
|
- DataSend.DataPtr = malloc(TcpDataLen+1);
|
|
|
- memset(DataSend.DataPtr,0x00,ReadLen+1);
|
|
|
- memcpy(DataSend.DataPtr,retptr+1,DataSend.RealLen);
|
|
|
- sint8 McuDataRet = -1;
|
|
|
- McuDataRet= xQueueSend(McuDataRecv_wifi,&DataSend,1000);
|
|
|
- if(McuDataRet != pdPASS)
|
|
|
- {
|
|
|
- if(DataSend.DataPtr != NULL)
|
|
|
- {
|
|
|
- free(DataSend.DataPtr);
|
|
|
- }
|
|
|
- DataSend.DataPtr = NULL;
|
|
|
- }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/* 解码部分 */
|
|
|
if(ReadLen > 0)
|
|
|
{
|
|
@@ -231,7 +152,7 @@ void wifi_task(void *pvParameters)
|
|
|
}
|
|
|
/* 心跳报文发送 */
|
|
|
static uint64 tickcount = 0;
|
|
|
- if(xTaskGetTickCount() - tickcount > pdMS_TO_TICKS(2000))
|
|
|
+ if(xTaskGetTickCount() - tickcount > pdMS_TO_TICKS(3000) && MAX_LOSTHEART >= heartTickLost_W)
|
|
|
{
|
|
|
tickcount = xTaskGetTickCount();
|
|
|
uint16 dataOutLen = 0;
|
|
@@ -240,141 +161,81 @@ void wifi_task(void *pvParameters)
|
|
|
dataOutLen = DecodeSlaveBuffer(dataOutBuffer, sizeof(dataOutBuffer), HEARTBEAT_S);
|
|
|
/* 数据发送 */
|
|
|
tcpSendFunc(0,dataOutBuffer,dataOutLen);
|
|
|
+ heartTickLost_W++; /* 心跳计数 */
|
|
|
+ }
|
|
|
+ /* 断开WiFi连接 */
|
|
|
+ if(heartTickLost_W > MAX_LOSTHEART || 2 == WiFiConnectControl)
|
|
|
+ {
|
|
|
+ if(0 == WiFiDisconnec())
|
|
|
+ {
|
|
|
+ tickcount = 0;
|
|
|
+ tcp_server_process_status = TCP_UNCONNECT;
|
|
|
+ heartTickLost_W = 0;
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
- case TCP_SERVICES_CLOSE:/* 等待WiFi链接建立 */
|
|
|
+ case TCP_UNCONNECT:
|
|
|
{
|
|
|
- //tcp_server_process_status = TCP_IDLE;
|
|
|
- wifi_init(3);
|
|
|
- tcp_server_process_status = TCP_SERVICES_OK;
|
|
|
- if(true == wifiAskFlg)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
break;
|
|
|
}
|
|
|
default:break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-/*
|
|
|
- * 解码后
|
|
|
- * 获得发送数据包
|
|
|
- * */
|
|
|
-uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd)
|
|
|
+
|
|
|
+/**************************解码部分***********************************/
|
|
|
+static uint8 wifiDataDecode(uint8 *dataIn,uint16 dataLen)
|
|
|
{
|
|
|
- uint8 start_field[3] = {0X68,0X23,0X24};
|
|
|
- uint8 car_vin[17] = "12345678901234567";
|
|
|
- uint16 tx_len = 0;
|
|
|
- uint8 time_now[8] = {0};
|
|
|
-
|
|
|
- memcpy(dataOutBuffer + tx_len, start_field, 3);
|
|
|
- tx_len += 3;
|
|
|
- tx_len += 2; /* 长度 +2 */
|
|
|
- tx_len ++; /* 指令 +1*/
|
|
|
- memcpy(dataOutBuffer + tx_len, car_vin, 17);
|
|
|
- tx_len += 17;
|
|
|
+ (void)dataLen;
|
|
|
+ uint8 cmd = 0;
|
|
|
+ cmd = dataIn[5];
|
|
|
+ uint8 sendBuffer[255];
|
|
|
+ uint16 dataOutLen = 0;
|
|
|
+ uint16 frameLen = 0;
|
|
|
+ static uint16 lockOrUnlockIng = 0; /* 目前等待解锁或者加锁反馈 0 无 LOCK_M 加锁 UNLOCK_M 解锁 */
|
|
|
+ static uint32 awaitTick = 0;
|
|
|
|
|
|
- switch(cmd)
|
|
|
+ for(uint16 i = 0; i < dataLen - 23; i++)
|
|
|
{
|
|
|
- case HEARTBEAT_S:
|
|
|
- {
|
|
|
- //uint8 lock_sta = 0; /* 锁止状态 */
|
|
|
- uint8 key_sta = 0; /* 钥匙状态 */
|
|
|
- uint8 ele_sta = 0; /* 电磁阀状态 */
|
|
|
-
|
|
|
- dataOutBuffer[5] = HEARTBEAT_S;
|
|
|
- memcpy(dataOutBuffer + tx_len, time_now, 8);
|
|
|
- tx_len += 8;
|
|
|
- dataOutBuffer[tx_len] = lock_sta;
|
|
|
- tx_len += 1;
|
|
|
- dataOutBuffer[tx_len] = key_sta;
|
|
|
- tx_len += 1;
|
|
|
- dataOutBuffer[tx_len] = ele_sta;
|
|
|
- tx_len += 1;
|
|
|
- break;
|
|
|
- }
|
|
|
- case LOCK_S:
|
|
|
- {
|
|
|
- uint8 answer = 0;
|
|
|
- dataOutBuffer[5] = LOCK_S;
|
|
|
- memcpy(dataOutBuffer + tx_len, time_now, 8);
|
|
|
- tx_len += 8;
|
|
|
- dataOutBuffer[tx_len] = answer;
|
|
|
- tx_len += 1;
|
|
|
- lock_sta = 2;
|
|
|
- break;
|
|
|
- }
|
|
|
- case UNLOCK_S:
|
|
|
- {
|
|
|
- uint8 answer = 0X0;
|
|
|
- dataOutBuffer[5] = UNLOCK_S;
|
|
|
- memcpy(dataOutBuffer + tx_len, time_now, 8);
|
|
|
- tx_len += 8;
|
|
|
- dataOutBuffer[tx_len] = answer;
|
|
|
- tx_len += 1;
|
|
|
- lock_sta = 1;
|
|
|
- break;
|
|
|
- }
|
|
|
- case GETINFO_S:
|
|
|
+ if(0X68 == dataIn[i] && 0X23 == dataIn[i + 1] && 0X24 == dataIn[i + 2])
|
|
|
{
|
|
|
- memcpy(dataOutBuffer + tx_len, time_now, 8);
|
|
|
- tx_len += 8;
|
|
|
-
|
|
|
- SLAVE_INFO slav_info_temp = {0};
|
|
|
- memset(slav_info_temp.sn, 0, 26);
|
|
|
- slav_info_temp.com_sta = 1;
|
|
|
- slav_info_temp.BMS_sta = 2;
|
|
|
- slav_info_temp.touch_sta = 3;
|
|
|
- slav_info_temp.bms_v = 4;
|
|
|
- slav_info_temp.bms_a = 5;
|
|
|
- slav_info_temp.soc = 6;
|
|
|
- slav_info_temp.soh = 7;
|
|
|
- slav_info_temp.str_sta = 8;
|
|
|
- slav_info_temp.stop_sta = 9;
|
|
|
- slav_info_temp.odo = 10;
|
|
|
- slav_info_temp.inputA = 11;
|
|
|
- slav_info_temp.outputA = 12;
|
|
|
- slav_info_temp.inputKwh = 13;
|
|
|
- slav_info_temp.outputKwh = 14;
|
|
|
- slav_info_temp.enger = 15;
|
|
|
- dataOutBuffer[5] = GETINFO_S;
|
|
|
- memcpy(dataOutBuffer + tx_len, &slav_info_temp, sizeof(SLAVE_INFO));
|
|
|
- tx_len += sizeof(SLAVE_INFO);
|
|
|
- break;
|
|
|
+ frameLen = dataIn[i + 3] + (dataIn[i + 4] << 8);
|
|
|
+ if((i + frameLen <= dataLen) && 0X68 == dataIn[frameLen - 1])
|
|
|
+ {
|
|
|
+ cmd = dataIn[i + 5];
|
|
|
+ EffectiveDataFed = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cmd = 0XFF;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
- default:
|
|
|
- buffSize = 0;
|
|
|
- break;
|
|
|
}
|
|
|
- dataOutBuffer[tx_len] = 0X68;
|
|
|
- tx_len += 1;
|
|
|
- dataOutBuffer[3] = tx_len & 0XFF;
|
|
|
- dataOutBuffer[4] = (tx_len >> 8) & 0XFF;
|
|
|
|
|
|
- return tx_len;
|
|
|
-}
|
|
|
-
|
|
|
-/**************************解码部分***********************************/
|
|
|
-static void wifiDataDecode(uint8 *dataIn,uint16 dataLen)
|
|
|
-{
|
|
|
- uint8 cmd = 0;
|
|
|
- cmd = dataIn[5];
|
|
|
- uint8 sendBuffer[256];
|
|
|
- uint16 dataOutLen = 0;
|
|
|
switch(cmd)
|
|
|
{
|
|
|
case LOCK_M:
|
|
|
{
|
|
|
- dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), LOCK_S);
|
|
|
- tcpSendFunc(0,sendBuffer,dataOutLen);
|
|
|
+ LockSta = 1;
|
|
|
+ if(LOCK_M != lockOrUnlockIng)
|
|
|
+ {
|
|
|
+ lockOrUnlockIng = LOCK_S;
|
|
|
+ awaitTick = xTaskGetTickCount();
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
case UNLOCK_M:
|
|
|
{
|
|
|
- dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), UNLOCK_S);
|
|
|
- tcpSendFunc(0,sendBuffer,dataOutLen);
|
|
|
+ UnlockSta = 1;
|
|
|
+ if(UNLOCK_M != lockOrUnlockIng)
|
|
|
+ {
|
|
|
+ lockOrUnlockIng = UNLOCK_S;
|
|
|
+ awaitTick = xTaskGetTickCount();
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
case GETINFO_M:
|
|
@@ -383,14 +244,38 @@ static void wifiDataDecode(uint8 *dataIn,uint16 dataLen)
|
|
|
tcpSendFunc(0,sendBuffer,dataOutLen);
|
|
|
break;
|
|
|
}
|
|
|
- default:break;
|
|
|
+ case HEARTBEAT_M:
|
|
|
+ {
|
|
|
+ /* 心跳响应 处理心跳返回包内的内容*/
|
|
|
+ heartTickLost_W = 0;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if((0 != lockOrUnlockIng) &&
|
|
|
+ ((xTaskGetTickCount() - awaitTick >= pdMS_TO_TICKS(20000)) ||
|
|
|
+ ((LOCK_S == lockOrUnlockIng && 0 == SolValActuSt) ||
|
|
|
+ (UNLOCK_S == lockOrUnlockIng && 1 == SolValActuSt))))
|
|
|
+ {
|
|
|
+ dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), lockOrUnlockIng);
|
|
|
+ tcpSendFunc(0,sendBuffer,dataOutLen);
|
|
|
+ if(LOCK_S == lockOrUnlockIng)
|
|
|
+ UnlockSta = 0;
|
|
|
+ else if(UNLOCK_S == lockOrUnlockIng)
|
|
|
+ LockSta = 0;
|
|
|
+ else {}
|
|
|
+ lockOrUnlockIng = 0; // 复位
|
|
|
}
|
|
|
+ else{}
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen)
|
|
|
{
|
|
|
sint8 outValue = -1;
|
|
|
+ (void)TcpConnectId;
|
|
|
#ifdef AT_CMD_SEND /* 启用AT指令发送数据 */
|
|
|
|
|
|
uint8 sendErrConuter= 0;
|
|
@@ -418,7 +303,7 @@ sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen)
|
|
|
}
|
|
|
#else
|
|
|
/* 透传 */
|
|
|
- outValue = UART_Send_Data(UART_LPUART1, (uint8 *)SendDataPtr, SendDataLen, 100);
|
|
|
+ outValue = UART_Send_Data(UART_LPUART1, (uint8 *)SendDataPtr, SendDataLen, 500);
|
|
|
#endif/* #ifdef AT_CMD_SEND */
|
|
|
return outValue;
|
|
|
}
|
|
@@ -466,30 +351,6 @@ static void wifi_init(uint8 Step)
|
|
|
WIFI_CMD_INIT(AT_CWAUTOCONN,NULL,0,InitStep,ATRet);
|
|
|
break;
|
|
|
}
|
|
|
- case 4:
|
|
|
- {
|
|
|
- const char* wifi_init = "\"Hoisting\",\"jx999999\"\r\n";
|
|
|
- //const char* wifi_init = "\"123456\",\"12345678\"\r\n";
|
|
|
- WIFI_CMD_INIT(AT_CWJAP,(uint8*)wifi_init,strlen(wifi_init),InitStep,ATRet);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 5:
|
|
|
- {
|
|
|
- const char* wifi_ip_info_init = "\"TCP\",\"192.168.3.177\",5588\r\n";
|
|
|
- //const char* wifi_ip_info_init = "\"TCP\",\"192.168.0.13\",5588\r\n";
|
|
|
- WIFI_CMD_INIT(AT_CIPSTART,wifi_ip_info_init,strlen(wifi_ip_info_init),InitStep,ATRet);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 6:/* 设置透传模式 */
|
|
|
- {
|
|
|
- WIFI_CMD_INIT(AT_CIPMODE,NULL,0,InitStep,ATRet);
|
|
|
- if(0 == ATRet)
|
|
|
- {
|
|
|
- vTaskDelay(pdMS_TO_TICKS(2000));
|
|
|
- AtcmdTransmit(AT_CIPSEND, NULL, 0, &ATRet);
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
default:
|
|
|
{
|
|
|
return;
|
|
@@ -547,14 +408,17 @@ static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen,
|
|
|
static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen)
|
|
|
{
|
|
|
sint8 OutValue = -1;
|
|
|
+ (void)pReadLen;
|
|
|
+ (void)PSendStr;
|
|
|
uint8 *retptr = NULL;
|
|
|
char *OkAns = "OK";
|
|
|
char *Error = "ERROR";
|
|
|
- char *FAIL = "FAIL";
|
|
|
+ //char *FAIL = "FAIL";
|
|
|
+ //char* isBuilded = "link is builded";
|
|
|
char* noChange = "no change";
|
|
|
char* ALCON = "ALREADY CONNECTED";
|
|
|
char* busy = "busy p...";
|
|
|
- char* isBuilded = "link is builded";
|
|
|
+
|
|
|
retptr = (uint8 *)strstr((char *)pReadStr, noChange);
|
|
|
if(retptr)
|
|
|
{
|
|
@@ -570,6 +434,7 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
|
|
|
case AT_CWAUTOCONN:
|
|
|
case AT_CIFSR:
|
|
|
case AT_CIPMODE:
|
|
|
+ case AT_CWQAP:
|
|
|
{
|
|
|
if (retptr)
|
|
|
{
|
|
@@ -588,7 +453,7 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
- UART_Receive_Data(UART_LPUART1, (uint8*)pReadStr,(uint8*)&ReadLen, 500);
|
|
|
+ UART_Receive_Data(UART_LPUART1, (uint8*)pReadStr,(uint16*)&ReadLen, 500);
|
|
|
|
|
|
cyclNum++;
|
|
|
}
|
|
@@ -598,8 +463,16 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
+ case AT_ISNOCON:
|
|
|
+ {
|
|
|
+ char* iscon = "+CWJAP:0";
|
|
|
|
|
|
- //WIFI GOT IP
|
|
|
+ retptr = (uint8 *)strstr((char *)pReadStr, iscon);
|
|
|
+ if(0 != retptr)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
@@ -610,34 +483,114 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
|
|
|
}
|
|
|
return OutValue;
|
|
|
}
|
|
|
-sint8 getBackStatus(sint8 ret)
|
|
|
+static sint8 WiFiDisconnec(void)
|
|
|
{
|
|
|
- switch(ret)
|
|
|
+ /* 退出WiFi 断开连接 */
|
|
|
+ UART_Send_Data(UART_LPUART1, (uint8 *)"+++", 3, 100);
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(400));
|
|
|
+ UART_Send_Data(UART_LPUART1, (uint8 *)"\r\n", 2, 100);
|
|
|
+ sint8 ret = 0;
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(400));
|
|
|
+ UART_Reset(UART_LPUART1);
|
|
|
+ AtcmdTransmit(AT_CWQAP, NULL, 0, &ret);
|
|
|
+ if(0 != ret)
|
|
|
{
|
|
|
- case 0:
|
|
|
- return 2;
|
|
|
- case 3:
|
|
|
- return 4;
|
|
|
- default:return 3;
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(400));
|
|
|
+ UART_Reset(UART_LPUART1);
|
|
|
+ AtcmdTransmit(AT_ISNOCON, NULL, 0, &ret);
|
|
|
}
|
|
|
+ EffectiveDataFed = 0;
|
|
|
+ return ret;
|
|
|
}
|
|
|
-void GlobeVariableHandle_W1(void)
|
|
|
+/*
|
|
|
+ * 建立TCP连接函数
|
|
|
+ * timeoutMs 超时时间
|
|
|
+ * 数值为0时意为一直等待连接
|
|
|
+ * */
|
|
|
+static sint8 TCP_ConnectEstablish(uint32 timeoutMs)
|
|
|
{
|
|
|
+ //WIFI模块初始化
|
|
|
+ uint8 InitStep = 0;
|
|
|
sint8 ATRet = -1;
|
|
|
+ uint32 initTime = xTaskGetTickCount();
|
|
|
+ while(1)
|
|
|
+ {
|
|
|
+
|
|
|
+ switch(InitStep)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ {
|
|
|
+ //const char* wifi_init = "\"Hoisting\",\"jx999999\"\r\n";
|
|
|
+ const char* wifi_init = "\"123456\",\"12345678\"\r\n";
|
|
|
+ WIFI_CMD_INIT(AT_CWJAP,(uint8*)wifi_init,strlen(wifi_init),InitStep,ATRet);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 1:
|
|
|
+ {
|
|
|
+ //const char* wifi_ip_info_init = "\"TCP\",\"192.168.3.177\",5588\r\n";
|
|
|
+ const char* wifi_ip_info_init = "\"TCP\",\"192.168.0.181\",5588\r\n";
|
|
|
+ WIFI_CMD_INIT(AT_CIPSTART,wifi_ip_info_init,strlen(wifi_ip_info_init),InitStep,ATRet);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 2:/* 设置透传模式 */
|
|
|
+ {
|
|
|
+ WIFI_CMD_INIT(AT_CIPMODE,NULL,0,InitStep,ATRet);
|
|
|
+ if(0 == ATRet)
|
|
|
+ {
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
+ AtcmdTransmit(AT_CIPSEND, NULL, 0, &ATRet);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ return 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if(xTaskGetTickCount() - initTime > pdMS_TO_TICKS(timeoutMs) && 0 != timeoutMs)
|
|
|
+ {
|
|
|
+ /* 超时退出 */
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
- if(1 == AT_WAT_Flag)
|
|
|
+static void WiFiStaMan(void)
|
|
|
+{
|
|
|
+
|
|
|
+ if((WIFI_UNENABLE != tcp_server_process_status && 1 == WiFiPwrControl)
|
|
|
+ || (1 == KeySignal && TCP_CONNECT != tcp_server_process_status)) /* WiFi关机 */
|
|
|
+ {
|
|
|
+
|
|
|
+ WiFiPwrControl = 0;
|
|
|
+ if(TCP_CONNECT == tcp_server_process_status)
|
|
|
+ {
|
|
|
+ WiFiDisconnec();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ;
|
|
|
+ }
|
|
|
+ Dio_WriteChannel(DioConf_DioChannel_PTD16_GPIO_OUT_MCU_WIFI_POW_EN, STD_OFF);
|
|
|
+ tcp_server_process_status = WIFI_UNENABLE;
|
|
|
+
|
|
|
+ }
|
|
|
+ if(WIFI_UNENABLE == tcp_server_process_status && 2 == WiFiPwrControl) /* WiFi开机 */
|
|
|
{
|
|
|
- AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet);
|
|
|
- AT_WAT_Flag = getBackStatus(ATRet);
|
|
|
+ WiFiPwrControl = 0;
|
|
|
+ Dio_WriteChannel(DioConf_DioChannel_PTD16_GPIO_OUT_MCU_WIFI_POW_EN, STD_ON);
|
|
|
}
|
|
|
- else if(1 == AT_WIFI_INFO_FLAG)
|
|
|
+ if(TCP_UNCONNECT == tcp_server_process_status && 1 == WiFiConnectControl) /* 连接WiFi */
|
|
|
{
|
|
|
- AtcmdTransmit(AT_CWJAP, WiFi_Info + 1, WiFi_Info[0], &ATRet);
|
|
|
- AT_WIFI_INFO_FLAG = getBackStatus(ATRet);
|
|
|
+ WiFiConnectControl = 0;
|
|
|
+ TCP_ConnectEstablish(0);
|
|
|
+ tcp_server_process_status = TCP_CONNECT;
|
|
|
}
|
|
|
- else if(1 == AT_IPINFO_SET_Flag)
|
|
|
+ if(TCP_CONNECT == tcp_server_process_status && 2 == WiFiConnectControl) /* 断开WiFi */
|
|
|
{
|
|
|
- AtcmdTransmit(AT_CIPSTART, IP_SetInfo + 1, IP_SetInfo[0], &ATRet);
|
|
|
- AT_IPINFO_SET_Flag = getBackStatus(ATRet);
|
|
|
+ WiFiConnectControl = 0;
|
|
|
+ WiFiDisconnec();
|
|
|
+ tcp_server_process_status = TCP_UNCONNECT;
|
|
|
}
|
|
|
}
|