Commit d2b4ee17 authored by aohui.li's avatar aohui.li

增加状态机、开启4G和以太网、开启以太网静态IP可配(v1.01)

parent 28d624b9
...@@ -36,17 +36,18 @@ ...@@ -36,17 +36,18 @@
#include "rtc.h" #include "rtc.h"
#include "ram.h" #include "ram.h"
#if CONFIG_W5100S_SPI_ETHERNET > 0
#include "../infa/APP/httputil.h" #include "../infa/APP/httputil.h"
#include "w5100s.h" #include "w5100s.h"
#include "dhcp.h" #include "dhcp.h"
#include "loopback.h" #include "loopback.h"
#include "wizchip_conf.h" #include "wizchip_conf.h"
#include "w5100s_conf.h" #include "w5100s_conf.h"
#include "eth.h" #if CONFIG_W5100S_SPI_ETHERNET > 0
#include "eth.h"
#endif
#include "../infa/APP/tcp_server.h" #include "../infa/APP/tcp_server.h"
#include "../eth_parsing/ts_iot01b.h" #include "../eth_parsing/ts_iot01b.h"
#endif
#if CONFIG_EC200S_USB_4G > 0 #if CONFIG_EC200S_USB_4G > 0
#include "celluar_comm.h" #include "celluar_comm.h"
#include "celluar.h" #include "celluar.h"
...@@ -72,17 +73,16 @@ struct beingConfigIOT{ ...@@ -72,17 +73,16 @@ struct beingConfigIOT{
.Config_Id = 0 .Config_Id = 0
}; };
unsigned long g_ts[IOTDEV_MAX_NUM] = {0}; // 记录最新收到心跳时的时间 // unsigned long g_ts[EACH_GROUP_MAX_NUM] = {0}; // 记录最新收到心跳时的时间
QueueHandle_t xMessageQueueHandle = NULL; QueueHandle_t xMessageQueueHandle = NULL;
unsigned char g_slaver_status = 0;
static bool g_bDataSend_TimerArr = false;
static bool g_bErrorSend_TimerArr = false; static bool g_bErrorSend_TimerArr = false;
bool g_DataUpdateFlag = false; bool g_DataUpdateFlag = false;
bool g_bErrorUpdateFlag = false; bool g_bErrorUpdateFlag = false;
extern TimerHandle_t xSensorTimer; extern TimerHandle_t xSensorTimeoutTimer;
extern TimerHandle_t xSensorDataFreshTimer;
SemaphoreHandle_t xSemaphoreWaitResp = NULL; SemaphoreHandle_t xSemaphoreWaitResp = NULL;
SemaphoreHandle_t xSemaphoreDataResp = NULL; SemaphoreHandle_t xSemaphoreDataResp = NULL;
SemaphoreHandle_t xSemaphoreStopResp = NULL;
SemaphoreHandle_t xSemaphoreConfigResp = NULL;
/* Private macros ------------------------------------------------------------*/ /* Private macros ------------------------------------------------------------*/
// Task info // Task info
...@@ -340,7 +340,9 @@ static TimerHandle_t g_xKeyTimer; ...@@ -340,7 +340,9 @@ static TimerHandle_t g_xKeyTimer;
static TimerHandle_t g_xWorkTimer; static TimerHandle_t g_xWorkTimer;
static TimerHandle_t g_xPushTimer; static TimerHandle_t g_xPushTimer;
static TimerHandle_t g_xDLICalTimer; static TimerHandle_t g_xDLICalTimer;
static TimerHandle_t g_xDataSendTimer; TimerHandle_t g_xDataSendTimer[WIREROPE_GROUP_MAX_NUM];
TimerHandle_t g_xFeatureTimer = NULL;
bool g_bFeatureTimerArr;
static TimerHandle_t g_xErrorSendTimer; static TimerHandle_t g_xErrorSendTimer;
static TimerHandle_t g_xCfgUpTimer; static TimerHandle_t g_xCfgUpTimer;
static TimerHandle_t g_xSenConfigTimer; static TimerHandle_t g_xSenConfigTimer;
...@@ -477,10 +479,8 @@ unsigned long *g_pulConfigNumberPointerList[] = ...@@ -477,10 +479,8 @@ unsigned long *g_pulConfigNumberPointerList[] =
, ,
&g_xWITConfig.ulSenParmInfoValue &g_xWITConfig.ulSenParmInfoValue
#endif #endif
#if CONFIG_W5100S_SPI_ETHERNET > 0
, ,
&g_xWITConfig.ulEthNetInfoValue &g_xWITConfig.ulEthNetInfoValue
#endif
}; };
static const char *const g_pcImportantParameterName[] = static const char *const g_pcImportantParameterName[] =
...@@ -619,11 +619,17 @@ static unsigned int lastActiveFlag = 0; ...@@ -619,11 +619,17 @@ static unsigned int lastActiveFlag = 0;
#endif #endif
// my_iot function // my_iot function
void iot_Sensor_Init(void); void iot_Sensor_Clear(bool all);
void Message_Deal_Start_Task(void); void message_deal_start_task(void);
void tcp_server_task_start(void); void tcp_server_task_start(void);
bool WIT_SendIOTData(void); // bool WIT_SendIOTData(void);
bool WIT_SendIOTError(void); // bool WIT_MissingErrorSend(void);
bool WIT_SendIOTData(unsigned int group);
// bool WIT_MissingErrorSend(unsigned int group, unsigned int dev);
bool WIT_MissingErrorSend(unsigned int group);
void vSensorHeartBeatTimerCallback();
bool WIT_SendGatewayFeature(void); bool WIT_SendGatewayFeature(void);
bool IOTConfigFunction(); bool IOTConfigFunction();
void iot_Sensor_Print(void); void iot_Sensor_Print(void);
...@@ -681,16 +687,23 @@ void WIT_Init() ...@@ -681,16 +687,23 @@ void WIT_Init()
// Init mode and value // Init mode and value
// 从内部flash读取配置并进行配置 // 从内部flash读取配置并进行配置
WIT_ModeAndValueInit(); WIT_ModeAndValueInit();
//iot_Sensor_Print();
// for (int i = 0; i < IOTDEV_MAX_NUM; i++)
// {
// printf("my_IOTDev[%d] id_sn = %04x,%04x\n",i, g_xWITConfig.my_IOTDev[i].id, g_xWITConfig.my_IOTDev[i].sn);
// }
printf("threshold = %ld\n", g_xWITConfig.my_IOTThreshold);
printf("period = %ld\n", g_xWITConfig.ulPeriod);
g_xWITConfig.eGatewayMode = 1;
// Config check
if (g_xWITConfig.spdSel > 3)
{
g_xWITConfig.spdSel = 3;
}
iot_Sensor_Print();
printf("ulDataPeriod = %ld\n", g_xWITConfig.ulDataPeriod);
printf("ulFeaturePeriod = %ld\n", g_xWITConfig.ulFeaturePeriod);
#if CONFIG_BLUETOOTH > 0
// if_sensor_config_init();
#endif
if_sensor_config_init();
g_xWITConfig.eGatewayMode = 1; g_xWITConfig.eGatewayMode = 1;
// Config check // Config check
...@@ -720,9 +733,12 @@ void WIT_Init() ...@@ -720,9 +733,12 @@ void WIT_Init()
// connect to server only in STA mode // connect to server only in STA mode
WIT_MQTTConnect(); WIT_MQTTConnect();
vTaskDelay(pdMS_TO_TICKS(100)); vTaskDelay(pdMS_TO_TICKS(100));
// 启动tcp server & 消息处理函数
tcp_server_task_start(); tcp_server_task_start();
vTaskDelay(pdMS_TO_TICKS(100)); vTaskDelay(pdMS_TO_TICKS(100));
Message_Deal_Start_Task(); message_deal_start_task();
vTaskDelay(pdMS_TO_TICKS(100));
// Check has fan monitor point // Check has fan monitor point
if (g_xWITConfig.spdSyncEnable != 0) if (g_xWITConfig.spdSyncEnable != 0)
...@@ -1074,25 +1090,25 @@ void LearnThr(void) ...@@ -1074,25 +1090,25 @@ void LearnThr(void)
} }
// 如果学习完成变为正常模式 // 如果学习完成变为正常模式
if (g_xWITConfig.usCurrentLearningCount >= g_xWITConfig.usTotalLearningCount) // if (g_xWITConfig.usCurrentLearningCount >= g_xWITConfig.usTotalLearningCount)
{ // {
// 强制发送最后一组学习数据 // // 强制发送最后一组学习数据
g_bDLISend = true; // g_bDLISend = true;
// 切换为正常模式 时间周期修改 // // 切换为正常模式 时间周期修改
g_xWITConfig.eGatewayMode = GatewayMode_Normal; // g_xWITConfig.eGatewayMode = GatewayMode_Normal;
// 切换为正常模式上发周期 // // 切换为正常模式上发周期
g_xWITConfig.ulPeriod = g_xWITConfig.ulFeaturePeriodCopy; // g_xWITConfig.ulPeriod = g_xWITConfig.ulFeaturePeriodCopy;
g_xWITConfig.ulWavePeriod = g_xWITConfig.ulWavePeriodCopy; // g_xWITConfig.ulWavePeriod = g_xWITConfig.ulWavePeriodCopy;
// 重启数据定时器 // // 重启数据定时器
xTimerChangePeriod(g_xGatewayFeatureTimer, g_xWITConfig.ulPeriod, 1000); // xTimerChangePeriod(g_xGatewayFeatureTimer, g_xWITConfig.ulPeriod, 1000);
xTimerChangePeriod(g_xSensorFeatureTimer, g_xWITConfig.ulPeriod, 1000); // xTimerChangePeriod(g_xSensorFeatureTimer, g_xWITConfig.ulPeriod, 1000);
xTimerChangePeriod(g_xAlarmTimer, g_xWITConfig.ulPeriod, 1000); // xTimerChangePeriod(g_xAlarmTimer, g_xWITConfig.ulPeriod, 1000);
xTimerChangePeriod(g_xWaveTimer, g_xWITConfig.ulWavePeriod, 1000); // xTimerChangePeriod(g_xWaveTimer, g_xWITConfig.ulWavePeriod, 1000);
// 写入配置 // // 写入配置
xTimerStart(g_xCfgUpTimer, 1000); // xTimerStart(g_xCfgUpTimer, 1000);
} // }
} }
void WIT_Run() void WIT_Run()
...@@ -1125,31 +1141,39 @@ void WIT_Run() ...@@ -1125,31 +1141,39 @@ void WIT_Run()
} }
//Send IOT Data //Send IOT Data
if (g_bDataSend_TimerArr == true && g_DataUpdateFlag == true) // The threshold control is missing. // if (g_bDataSend_TimerArr == true && g_DataUpdateFlag == true) // The threshold control is missing.
{ // {
if ((WIT_SendIOTData() == true) && (WIT_SendGatewayFeature() == true)) // if ((WIT_SendIOTData() == true) && (WIT_SendGatewayFeature() == true))
{ // {
printf("Data Send success\n"); // printf("Data Send success\n");
g_DataUpdateFlag = false; // g_DataUpdateFlag = false;
g_bDataSend_TimerArr = false; // g_bDataSend_TimerArr = false;
} // }
else{ // else{
// printf("Send fail\n"); // // printf("Send fail\n");
} // }
} // }
//Send Error Data //Send Error Data
if (g_bErrorSend_TimerArr == true && g_bErrorUpdateFlag == true) // if (g_bErrorSend_TimerArr == true && g_bErrorUpdateFlag == true)
// {
// if (WIT_MissingErrorSend() == true )
// {
// printf("Error Send success\n");
// g_bErrorSend_TimerArr = false;
// g_bErrorUpdateFlag = false;
// }
// else{
// printf("Send fail\n");
// }
// }
if (g_bFeatureTimerArr == TRUE)
{ {
if (WIT_SendIOTError() == true ) if (WIT_SendGatewayFeature() == true )
{ {
printf("Error Send success\n"); g_bFeatureTimerArr = false;
g_bErrorSend_TimerArr = false;
g_bErrorUpdateFlag = false;
} }
else{
printf("Send fail\n");
}
} }
} }
...@@ -1481,10 +1505,10 @@ static void WIT_EventPollTask(void *pvParameter) ...@@ -1481,10 +1505,10 @@ static void WIT_EventPollTask(void *pvParameter)
{ {
g_bUpdateConfigureArea = false; g_bUpdateConfigureArea = false;
WIT_UpdateConfigureArea(); WIT_UpdateConfigureArea();
#if CONFIG_W5100S_SPI_ETHERNET > 0 // #if CONFIG_W5100S_SPI_ETHERNET > 0
if(get_pending_bit()) if(get_pending_bit())
esp_restart(); esp_restart();
#endif // #endif
} }
if (g_bUpdateThresholdArea == true) if (g_bUpdateThresholdArea == true)
...@@ -1925,37 +1949,41 @@ static void WIT_ModeAndValueInit() ...@@ -1925,37 +1949,41 @@ static void WIT_ModeAndValueInit()
// strcpy(g_xWITConfig.cMQTTBroker, "112.17.107.240"); // strcpy(g_xWITConfig.cMQTTBroker, "112.17.107.240");
// strcpy(g_xWITConfig.cMQTTPort, "1883"); // strcpy(g_xWITConfig.cMQTTPort, "1883");
// bNeedUpdateConfigArea = true; // bNeedUpdateConfigArea = true;
// strcpy(g_xWITConfig.cMQTTBroker, "www.witium.com.cn"); #if 0
// strcpy(g_xWITConfig.cMQTTPort, "12883"); // 测试平台
// strcpy(g_xWITConfig.cMQTTBroker, "116.62.127.242"); g_xWITConfig.ulSerialNumber = 20240108;
strcpy(g_xWITConfig.cMQTTBroker, "www.witium.com.cn");
strcpy(g_xWITConfig.cMQTTPort, "12883");
#else
// 正式平台
// g_xWITConfig.ulSerialNumber = 23122021;
strcpy(g_xWITConfig.cMQTTBroker, "116.62.127.242");
strcpy(g_xWITConfig.cMQTTPort, "1883");
#endif
// strcpy(g_xWITConfig.cMQTTBroker, "116.62.127.242");
// strcpy(g_xWITConfig.cMQTTPort, "1883"); // strcpy(g_xWITConfig.cMQTTPort, "1883");
// bNeedUpdateConfigArea = true; // bNeedUpdateConfigArea = true;
// g_xWITConfig.ulPort = 345; // g_xWITConfig.ulPort = 345;
g_xWITConfig.ulSerialNumber = 20240108; // g_xWITConfig.ulPort = 12883;
g_xWITConfig.ulPort = 12883; // g_xWITConfig.cTargetIP[0] = 222;
g_xWITConfig.cTargetIP[0] = 222; // g_xWITConfig.cTargetIP[1] = 71;
g_xWITConfig.cTargetIP[1] = 71; // g_xWITConfig.cTargetIP[2] = 96;
g_xWITConfig.cTargetIP[2] = 96; // g_xWITConfig.cTargetIP[3] = 91;
g_xWITConfig.cTargetIP[3] = 91; // g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[0].addr = 1;
// g_xWITConfig.my_IOTDev[0].addr = 1; // g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[0].id = 0x003f;
// g_xWITConfig.my_IOTDev[0].id = 0x003f; // g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[0].sn = 0x0001;
// g_xWITConfig.my_IOTDev[0].sn = 0x0001; // g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[0].bIsUsed = true;
// g_xWITConfig.my_IOTDev[0].bIsTimeout = false;
// g_xWITConfig.my_IOTDev[0].bIsUsed = true; iot_Sensor_Clear(false); // 清空传感器配置
iot_Sensor_Init(); // 清空传感器配置
// sprintf(g_xWITConfig.cAPName, "Witium-%08lu", g_xWITConfig.ulSerialNumber); // sprintf(g_xWITConfig.cAPName, "Witium-%08lu", g_xWITConfig.ulSerialNumber);
// bNeedUpdateConfigArea = true; // bNeedUpdateConfigArea = true;
// sprintf(g_xWITConfig.cAPName, "Witium-%08lu", g_xWITConfig.ulSerialNumber); // sprintf(g_xWITConfig.cAPName, "Witium-%08lu", g_xWITConfig.ulSerialNumber);
for (int i = 0; i < WIT_SENSOR_GROUP_MAX_NUM; i++) // iot_Sensor_Print();
{
g_xWITConfig.xGroupTable[i].ulOperation = 0;
g_xWITConfig.xGroupTable[i].ulWavePushEnable = 1;
g_xWITConfig.xGroupTable[i].ulFeaPushEnable = 1;
}
// WIT_UpdateConfigureArea(); // WIT_UpdateConfigureArea();
// g_xWITConfig.ulConfigureValue = 0; // g_xWITConfig.ulConfigureValue = 0;
bNeedUpdateConfigArea = true;
// Check ID and motor work time // Check ID and motor work time
// 1.Firstly check backup area // 1.Firstly check backup area
...@@ -2006,7 +2034,8 @@ static void WIT_ModeAndValueInit() ...@@ -2006,7 +2034,8 @@ static void WIT_ModeAndValueInit()
g_xWITConfig.eOpMode = WIFI_FORCE_AP; g_xWITConfig.eOpMode = WIFI_FORCE_AP;
#endif #endif
g_xWITConfig.ulSerialNumber = ulSN; g_xWITConfig.ulSerialNumber = ulSN;
g_xWITConfig.ulPeriod = WIT_NORMAL_FEATURE_PERIOD; g_xWITConfig.ulFeaturePeriod = WIT_FEATURE_PERIOD;
g_xWITConfig.ulDataPeriod = WIT_DATA_PERIOD;
g_xWITConfig.bConfigUpdated = false; g_xWITConfig.bConfigUpdated = false;
#if CONFIG_ESP32_WIFI > 0 #if CONFIG_ESP32_WIFI > 0
strcpy(g_xWITConfig.xWIFI.cDHCP, "DHCP"); strcpy(g_xWITConfig.xWIFI.cDHCP, "DHCP");
...@@ -2101,8 +2130,8 @@ static void WIT_ModeAndValueInit() ...@@ -2101,8 +2130,8 @@ static void WIT_ModeAndValueInit()
if (g_xWITConfig.ulCopyValueInfo != WIT_CONFIGURE_VALUE) if (g_xWITConfig.ulCopyValueInfo != WIT_CONFIGURE_VALUE)
{ {
g_xWITConfig.ulFeaturePeriodCopy = g_xWITConfig.ulPeriod; // g_xWITConfig.ulFeaturePeriodCopy = g_xWITConfig.ulPeriod;
g_xWITConfig.ulWavePeriodCopy = g_xWITConfig.ulWavePeriod; // g_xWITConfig.ulWavePeriodCopy = g_xWITConfig.ulWavePeriod;
g_xWITConfig.ulCopyValueInfo = WIT_CONFIGURE_VALUE; g_xWITConfig.ulCopyValueInfo = WIT_CONFIGURE_VALUE;
bNeedUpdateConfigArea = true; bNeedUpdateConfigArea = true;
} }
...@@ -2139,7 +2168,6 @@ static void WIT_ModeAndValueInit() ...@@ -2139,7 +2168,6 @@ static void WIT_ModeAndValueInit()
bNeedUpdateConfigArea = true; bNeedUpdateConfigArea = true;
} }
#endif #endif
#if CONFIG_W5100S_SPI_ETHERNET > 0
if (g_xWITConfig.ulEthNetInfoValue != WIT_CONFIGURE_VALUE) if (g_xWITConfig.ulEthNetInfoValue != WIT_CONFIGURE_VALUE)
{ {
g_xWITConfig.g_xConfigIp.mac[0] = 0x00; g_xWITConfig.g_xConfigIp.mac[0] = 0x00;
...@@ -2152,7 +2180,7 @@ static void WIT_ModeAndValueInit() ...@@ -2152,7 +2180,7 @@ static void WIT_ModeAndValueInit()
g_xWITConfig.g_xConfigIp.ip[0] = 192; g_xWITConfig.g_xConfigIp.ip[0] = 192;
g_xWITConfig.g_xConfigIp.ip[1] = 168; g_xWITConfig.g_xConfigIp.ip[1] = 168;
g_xWITConfig.g_xConfigIp.ip[2] = 1; g_xWITConfig.g_xConfigIp.ip[2] = 1;
g_xWITConfig.g_xConfigIp.ip[3] = 100; g_xWITConfig.g_xConfigIp.ip[3] = 160;
g_xWITConfig.g_xConfigIp.gw[0] = 192; g_xWITConfig.g_xConfigIp.gw[0] = 192;
g_xWITConfig.g_xConfigIp.gw[1] = 168; g_xWITConfig.g_xConfigIp.gw[1] = 168;
...@@ -2173,7 +2201,7 @@ static void WIT_ModeAndValueInit() ...@@ -2173,7 +2201,7 @@ static void WIT_ModeAndValueInit()
bNeedUpdateConfigArea = true; bNeedUpdateConfigArea = true;
} }
set_wiz_NetInfo(&g_xWITConfig.g_xConfigIp); set_wiz_NetInfo(&g_xWITConfig.g_xConfigIp);
#endif
#if USE_WIT_PLATFORM > 0 && CONFIG_ESP32_WIFI > 0 #if USE_WIT_PLATFORM > 0 && CONFIG_ESP32_WIFI > 0
// 把之前辉度的机器更新成杰牌后前缀显示JCI // 把之前辉度的机器更新成杰牌后前缀显示JCI
if (strstr(g_xWITConfig.cAPName, "Witium-") != NULL) if (strstr(g_xWITConfig.cAPName, "Witium-") != NULL)
...@@ -2190,9 +2218,8 @@ static void WIT_ModeAndValueInit() ...@@ -2190,9 +2218,8 @@ static void WIT_ModeAndValueInit()
bNeedUpdateConfigArea = true; bNeedUpdateConfigArea = true;
} }
#endif #endif
#if CONFIG_W5100S_SPI_ETHERNET > 0
ETHERNET_Init(&g_bModuleError, NULL); ETHERNET_Init(&g_bModuleError, NULL);
#endif
// 获取当前学习数量 // 获取当前学习数量
g_xWITConfig.usCurrentLearningCount = WIT_GetFeatureMinLearnCount(); g_xWITConfig.usCurrentLearningCount = WIT_GetFeatureMinLearnCount();
WIT_PreTaskInit(); WIT_PreTaskInit();
...@@ -2601,7 +2628,14 @@ static void WIT_MQTTConnectHandler(unsigned long ulMQTTID, bool bStatus) ...@@ -2601,7 +2628,14 @@ static void WIT_MQTTConnectHandler(unsigned long ulMQTTID, bool bStatus)
} }
bool WIT_SendIOTData(void) float FloatRetain(float value)
{
char buf[32] = {0};
sprintf(buf, "%.2f", value);
return atof(buf);;
}
bool WIT_SendIOTData(unsigned int group)
{ {
cJSON *pxRoot = NULL; cJSON *pxRoot = NULL;
cJSON *pxObj = NULL; cJSON *pxObj = NULL;
...@@ -2613,20 +2647,39 @@ bool WIT_SendIOTData(void) ...@@ -2613,20 +2647,39 @@ bool WIT_SendIOTData(void)
pucSendBuffer = MQTT_SendBufferPointer(g_xWITMQTT.lHandle); pucSendBuffer = MQTT_SendBufferPointer(g_xWITMQTT.lHandle);
ulBufferSize = MQTT_GetRestSendBufferSize(g_xWITMQTT.lHandle); ulBufferSize = MQTT_GetRestSendBufferSize(g_xWITMQTT.lHandle);
pxRoot = cJSON_CreateArray(); pxRoot = cJSON_CreateArray();
pxObj = cJSON_CreateObject(); char cAddr[4] = {0};
char addr[8]; char cOpDataName[ALIAS_MAX_NAME_LENGTH] = {0};
unsigned long now_ts = RTC_InternalGetTimeStamp(); unsigned long now_ts = RTC_InternalGetTimeStamp();
int alarmNum = 0;
char cIDSN[16] = {0};
char cNameAvr[32] = {0};
char cNameMax[32] = {0};
char cNameAlm[32] = {0};
for(int i = 0; i < IOTDEV_MAX_NUM; i++)
pxObj = cJSON_CreateObject();
if(pxObj == NULL)
return false;
sprintf(cAddr, "%d", group+1);
cJSON_AddStringToObject(pxObj, "addr", cAddr);
cJSON_AddNumberToObject(pxObj, "ts", now_ts);
for(unsigned short i = 0; i < EACH_GROUP_MAX_NUM; i++)
{ {
if(g_xWITConfig.my_IOTDev[i].bIsUsed == true && g_xWITConfig.my_IOTDev[i].bIsTimeout == false) if(g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsUsed && g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsPlugIn)
{ {
sprintf(addr, "%d", i+1); strncpy(cOpDataName, g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].alias, ALIAS_MAX_NAME_LENGTH);
cJSON_AddStringToObject(pxObj, "addr", addr); cJSON_AddNumberToObject(pxObj, cOpDataName, g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].rope_value / 10.0 );
cJSON_AddNumberToObject(pxObj, "iot1", g_xWITConfig.my_IOTDev[i].rope_value / 10.0); snprintf(cNameAvr, sizeof(cNameAvr), "avrPwR%d", i+1);
cJSON_AddNumberToObject(pxObj, "ts", now_ts); snprintf(cNameMax, sizeof(cNameMax), "maxPwR%d", i+1);
snprintf(cNameAlm, sizeof(cNameAlm), "almwR%d", i+1);
cJSON_AddNumberToObject(pxObj, cNameAvr, calculateAverage(g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].fifo_data, g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsFull) / 10.0);
cJSON_AddNumberToObject(pxObj, cNameMax, maxDataWireRope(g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].fifo_data, g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsFull) / 10.0);
cJSON_AddNumberToObject(pxObj, cNameAlm, getAlarm(g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].alarmRecord, g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsFull));
} }
} }
cJSON_AddItemToArray(pxRoot, pxObj); cJSON_AddItemToArray(pxRoot, pxObj);
bPackageStatus = cJSON_PrintPreallocated(pxRoot, (char *)pucSendBuffer, ulBufferSize, false); bPackageStatus = cJSON_PrintPreallocated(pxRoot, (char *)pucSendBuffer, ulBufferSize, false);
...@@ -2643,8 +2696,9 @@ bool WIT_SendIOTData(void) ...@@ -2643,8 +2696,9 @@ bool WIT_SendIOTData(void)
} }
return bStatus; return bStatus;
} }
#if 0
bool WIT_SendIOTError(void) #else
bool WIT_MissingErrorSend(unsigned int group)
{ {
cJSON *pxRoot = NULL; cJSON *pxRoot = NULL;
cJSON *pxObj = NULL; cJSON *pxObj = NULL;
...@@ -2656,20 +2710,24 @@ bool WIT_SendIOTError(void) ...@@ -2656,20 +2710,24 @@ bool WIT_SendIOTError(void)
pucSendBuffer = MQTT_SendBufferPointer(g_xWITMQTT.lHandle); pucSendBuffer = MQTT_SendBufferPointer(g_xWITMQTT.lHandle);
ulBufferSize = MQTT_GetRestSendBufferSize(g_xWITMQTT.lHandle); ulBufferSize = MQTT_GetRestSendBufferSize(g_xWITMQTT.lHandle);
pxRoot = cJSON_CreateArray(); pxRoot = cJSON_CreateArray();
char addr[8]; char cAddr[8];
unsigned long now_ts = RTC_InternalGetTimeStamp(); unsigned long now_ts = RTC_InternalGetTimeStamp();
for(int i = 0; i < IOTDEV_MAX_NUM; i++) pxObj = cJSON_CreateObject();
if(pxObj == NULL)
return bStatus;
sprintf(cAddr, "%d", group+1);
cJSON_AddStringToObject(pxObj, "addr", cAddr);
for(unsigned short i = 0; i < EACH_GROUP_MAX_NUM; i++)
{ {
if(g_xWITConfig.my_IOTDev[i].bIsUsed == true && g_xWITConfig.my_IOTDev[i].bIsTimeout == true) if(g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsUsed && (FALSE == g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].bIsPlugIn) )
{ {
pxObj = cJSON_CreateObject(); cJSON_AddStringToObject(pxObj, "dev", g_xWITConfig.g_wireRopeGroup[group].wireRopeDevs[i].alias);
sprintf(addr, "%d", i);
cJSON_AddStringToObject(pxObj, "addr", addr);
cJSON_AddNumberToObject(pxObj, "ts", now_ts);
cJSON_AddItemToArray(pxRoot, pxObj);
} }
} }
cJSON_AddNumberToObject(pxObj, "ts", now_ts);
cJSON_AddItemToArray(pxRoot, pxObj);
bPackageStatus = cJSON_PrintPreallocated(pxRoot, (char *)pucSendBuffer, ulBufferSize, false); bPackageStatus = cJSON_PrintPreallocated(pxRoot, (char *)pucSendBuffer, ulBufferSize, false);
if (pxRoot != NULL) if (pxRoot != NULL)
...@@ -2686,11 +2744,16 @@ bool WIT_SendIOTError(void) ...@@ -2686,11 +2744,16 @@ bool WIT_SendIOTError(void)
return bStatus; return bStatus;
} }
#endif
bool WIT_SendGatewayFeature(void) bool WIT_SendGatewayFeature(void)
{ {
cJSON *pxRoot = NULL; cJSON *pxRoot = NULL;
cJSON *pxObj = NULL; cJSON *pxObj = NULL;
// cJSON *pxSenArr = NULL;
// cJSON *pxDevArr = NULL;
// cJSON *pxSenObj[WIREROPE_GROUP_MAX_NUM][EACH_GROUP_MAX_NUM];
unsigned char *pucSendBuffer = NULL; unsigned char *pucSendBuffer = NULL;
unsigned long ulBufferSize = 0; unsigned long ulBufferSize = 0;
cJSON_bool bPackageStatus = false; cJSON_bool bPackageStatus = false;
...@@ -2706,7 +2769,8 @@ bool WIT_SendGatewayFeature(void) ...@@ -2706,7 +2769,8 @@ bool WIT_SendGatewayFeature(void)
cJSON_AddStringToObject(pxObj, "addr", "0"); cJSON_AddStringToObject(pxObj, "addr", "0");
cJSON_AddNumberToObject(pxObj, "ts", now_ts); cJSON_AddNumberToObject(pxObj, "ts", now_ts);
cJSON_AddNumberToObject(pxObj, "period", g_xWITConfig.ulPeriod); cJSON_AddNumberToObject(pxObj, "dPeriod", g_xWITConfig.ulDataPeriod);
cJSON_AddNumberToObject(pxObj, "fPeriod", g_xWITConfig.ulFeaturePeriod);
cJSON_AddNumberToObject(pxObj, "exist", online_num); cJSON_AddNumberToObject(pxObj, "exist", online_num);
cJSON_AddNumberToObject(pxObj, "workTime", worktime); cJSON_AddNumberToObject(pxObj, "workTime", worktime);
cJSON_AddItemToArray(pxRoot, pxObj); cJSON_AddItemToArray(pxRoot, pxObj);
...@@ -3439,9 +3503,9 @@ static void WIT_TimerHandler(TimerHandle_t xTimer) ...@@ -3439,9 +3503,9 @@ static void WIT_TimerHandler(TimerHandle_t xTimer)
g_bDLISend = true; g_bDLISend = true;
g_bDLICalculate = true; g_bDLICalculate = true;
} }
else if (xTimer == g_xDataSendTimer) else if (xTimer == g_xFeatureTimer)
{ {
g_bDataSend_TimerArr = true; g_bFeatureTimerArr = true;
} }
else if(xTimer == g_xErrorSendTimer) else if(xTimer == g_xErrorSendTimer)
{ {
...@@ -3507,6 +3571,14 @@ static void WIT_TimerHandler(TimerHandle_t xTimer) ...@@ -3507,6 +3571,14 @@ static void WIT_TimerHandler(TimerHandle_t xTimer)
} }
} }
} }
for(unsigned short i = 0; i < WIREROPE_GROUP_MAX_NUM; i++)
{
if (xTimer == g_xDataSendTimer[i])
{
g_xWITConfig.g_wireRopeGroup[i].DataSendFlag = true;
}
}
} }
/** /**
...@@ -3626,17 +3698,24 @@ static void WIT_GatewayConfigureHandler(MessageData *pxData) ...@@ -3626,17 +3698,24 @@ static void WIT_GatewayConfigureHandler(MessageData *pxData)
DJSON xSSObj; DJSON xSSObj;
DJSON xCfgArray; DJSON xCfgArray;
DJSON xCfgObj; DJSON xCfgObj;
DJSON xDevArray; DJSON xGroupArray;
DJSON xSensorArray;
DJSON xDevObj; DJSON xDevObj;
DJSON xSensorObj;
DJSON xItem; DJSON xItem;
unsigned long ulSSArraySize = 0; unsigned long ulSSArraySize = 0;
unsigned char ucAddr = 0; unsigned char ucAddr = 0;
char cAddr[8] = {0}; char cAddr[8] = {0};
char cDev[16] = {0}; char cDevIDSN[16] = {0};
uint8_t dev_num = 0; char cSensorAlias[ALIAS_MAX_NAME_LENGTH] = {0};
uint8_t group_id = 0;
uint8_t group_num = 0;
uint8_t sensor_num = 0;
bool bStatus = true; bool bStatus = true;
bool bNeedUpdateFTimer = false; bool bNeedUpdateFeatureTimer = false;
bool bNeedUpdateDataTimer = false;
bool bNeedUpdateCfg = false; bool bNeedUpdateCfg = false;
xRoot.pcItemPointer = pxData->message->payload; xRoot.pcItemPointer = pxData->message->payload;
...@@ -3681,117 +3760,180 @@ static void WIT_GatewayConfigureHandler(MessageData *pxData) ...@@ -3681,117 +3760,180 @@ static void WIT_GatewayConfigureHandler(MessageData *pxData)
xCfgObj = DJSON_GetArrayItem(&xCfgArray, 0); xCfgObj = DJSON_GetArrayItem(&xCfgArray, 0);
strncpy(g_xGatewayResponse.cAddr, cAddr, 4); strncpy(g_xGatewayResponse.cAddr, cAddr, 4);
ucAddr = atoi(cAddr); ucAddr = atoi(cAddr);
if (ucAddr <= IOTDEV_MAX_NUM)
{
Parameter *pxParm;
Threshold *pxThr;
// 网关配置 // 网关配置
if (ucAddr == 0) if (ucAddr == 0)
{ {
// 数据与特征发送周期 // Data 发送周期
xItem = DJSON_GetObjectItem(&xCfgObj, "period"); xItem = DJSON_GetObjectItem(&xCfgObj, "dPeriod");
if (xItem.eItemType == DJSON_Number) if (xItem.eItemType == DJSON_Number)
{ {
unsigned long ulSetPeriod; unsigned long ulSetPeriod;
ulSetPeriod = DJSON_GetValueInt(&xItem) * 1000; // MS -> S ulSetPeriod = DJSON_GetValueInt(&xItem) * 1000;
g_xWITConfig.ulPeriod = ulSetPeriod; g_xWITConfig.ulDataPeriod = ulSetPeriod;
g_xWITConfig.ulFeaturePeriodCopy = ulSetPeriod; bNeedUpdateDataTimer = true;
bNeedUpdateFTimer = true;
bNeedUpdateCfg = true; bNeedUpdateCfg = true;
} }
// IOT阈值设定 // Feature 发送周期
xItem = DJSON_GetObjectItem(&xCfgObj, "threshold"); xItem = DJSON_GetObjectItem(&xCfgObj, "fPeriod");
if (xItem.eItemType == DJSON_Number) if (xItem.eItemType == DJSON_Number)
{ {
unsigned long ulSetThreshold; unsigned long ulSetPeriod;
ulSetPeriod = DJSON_GetValueInt(&xItem) * 1000;
g_xWITConfig.ulFeaturePeriod = ulSetPeriod;
bNeedUpdateFeatureTimer = true;
bNeedUpdateCfg = true;
}
}
else
{
bStatus = false;
goto judge_status;
}
// 解析设备[id --- sn,id], 绑定设备
xGroupArray = DJSON_GetObjectItem(&xCfgObj, "dev");
if (xGroupArray.eItemType == DJSON_Array)
{
group_num = DJSON_GetArraySize(&xGroupArray); // 计算设备数量,以设备划分组
printf("Gateway has Group %d\n", group_num);
if(group_num > WIREROPE_GROUP_MAX_NUM || 0 == group_num)
{
bStatus = false;
goto judge_status;
}
}
else
{
printf("xSensorArray is not DJSON_Array\n");
bStatus = false;
goto judge_status;
}
iot_Sensor_Clear(true);
for(int j = 0; j < group_num; j++)
{
xDevObj = DJSON_GetArrayItem(&xGroupArray, j);
if (xDevObj.eItemType == DJSON_Object)
{
xItem = DJSON_GetObjectItem(&xDevObj, "gId");
if (xItem.eItemType == DJSON_Number)
{
group_id = DJSON_GetValueInt(&xItem);
}
else{
printf("gId is not DJSON_Number\n");
bStatus = false;
goto judge_status;
}
ulSetThreshold = DJSON_GetValueDouble(&xItem) * 10; // 4% 下发 40,0x00 0x28 printf("gId=%d\n",group_id);
if(ulSetThreshold > 1000 || ulSetThreshold < 0) if(group_id > EACH_GROUP_MAX_NUM || group_id <= 0)
{ {
printf("Set Threshold Error.\n");
bStatus = false; bStatus = false;
goto judge_status; goto judge_status;
} }
printf("Threshold %ld\n", ulSetThreshold);
if(IOTConfigFunction()) xSensorArray = DJSON_GetObjectItem(&xDevObj, "sensors");
if (xSensorArray.eItemType == DJSON_Array)
{ {
bStatus = true; sensor_num = DJSON_GetArraySize(&xSensorArray);
printf("IOTConfigFunction true\n"); printf("group[%d] sensor_num = %d\n", group_id, sensor_num);
if(sensor_num > EACH_GROUP_MAX_NUM || 0 == sensor_num)
{
bStatus = false;
goto judge_status;
}
} }
else else
{ {
printf("xSensorArray is not DJSON_Array\n");
bStatus = false; bStatus = false;
goto judge_status; goto judge_status;
} }
// g_beingConfigIOT.ConfigFlag = true;
g_xWITConfig.my_IOTThreshold = ulSetThreshold; for(int i = 0; i < sensor_num; i++)
bNeedUpdateCfg = true;
}
// 解析设备[id --- sn,id]
xDevArray = DJSON_GetObjectItem(&xCfgObj, "dev");
dev_num = DJSON_GetArraySize(&xDevArray); // 计算设备数量
// printf("dev has %d\n", dev_num);
if(dev_num > IOTDEV_MAX_NUM)
{
dev_num = IOTDEV_MAX_NUM;
printf("dev_num over than IOTDEV_MAX_NUM\n");
bStatus = false;
goto judge_status;
}
if (xDevArray.eItemType == DJSON_Array)
{
for(int i = 0; i < dev_num; i++)
{ {
xDevObj = DJSON_GetArrayItem(&xDevArray, i); xSensorObj = DJSON_GetArrayItem(&xSensorArray, i);
if (xDevObj.eItemType == DJSON_Object) if(xSensorObj.eItemType == DJSON_Object)
{ {
xItem = DJSON_GetObjectItem(&xDevObj, "addr"); xItem = DJSON_GetObjectItem(&xSensorObj, "id");
if (xItem.eItemType == DJSON_String) if (xItem.eItemType == DJSON_String)
{ {
DJSON_GetValueString(&xItem, cAddr, sizeof(cAddr)); DJSON_GetValueString(&xItem, cDevIDSN, sizeof(cDevIDSN));
g_xWITConfig.my_IOTDev[i].addr = atoi(cAddr); stringIdSnToShorts(cDevIDSN, &g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].id, &g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].sn);
} if(0 != g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].id || 0 != g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].sn)
xItem = DJSON_GetObjectItem(&xDevObj, "id");// 解析sn,id
if (xItem.eItemType == DJSON_String)
{
DJSON_GetValueString(&xItem, cDev, sizeof(cDev));
stringIdSnToShorts(cDev, &g_xWITConfig.my_IOTDev[i].id, &g_xWITConfig.my_IOTDev[i].sn);
if(0 == g_xWITConfig.my_IOTDev[i].id || 0 == g_xWITConfig.my_IOTDev[i].sn)
{ {
printf("Config id/sn Fail\n"); printf("ID: 0X%04X; SN: 0X%04X\n", g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].sn);
g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].addr = group_id;
g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].bIsUsed = true;
g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].bIsPlugIn = false; // 此处是否插入标志为false
g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].rope_value = 0;
printf("dev[%d] Configured\n", i);
}
else{
printf("Config ID/SN Fail\n");
bStatus = false;
goto judge_status; goto judge_status;
} }
printf("ID: 0X%04X; SN: 0X%04X\n", g_xWITConfig.my_IOTDev[i].id, g_xWITConfig.my_IOTDev[i].sn); }
g_xWITConfig.my_IOTDev[i].bIsUsed = true;
g_xWITConfig.my_IOTDev[i].bIsTimeout = true; xItem = DJSON_GetObjectItem(&xSensorObj, "alias");
g_xWITConfig.my_IOTDev[i].bIsPlugIn = false; // 此处是否插入标志为false if (xItem.eItemType == DJSON_String)
g_xWITConfig.my_IOTDev[i].rope_value = 0; {
printf("dev[%d] Configured\n", i); DJSON_GetValueString(&xItem, cSensorAlias, ALIAS_MAX_NAME_LENGTH);
snprintf(g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].alias, sizeof(g_xWITConfig.g_wireRopeGroup[group_id-1].wireRopeDevs[i].alias),"%s",cSensorAlias);
} }
} }
else
{
printf("xSensorObj is not DJSON_String\n");
bStatus = false;
goto judge_status;
}
} }
} }
} }
bNeedUpdateCfg = true;
// 更新定时器 // 更新定时器
if (bNeedUpdateFTimer == true) if (bNeedUpdateDataTimer == true)
{ {
xTimerChangePeriod(g_xDataSendTimer, g_xWITConfig.ulPeriod, 1000); for(uint8_t k = 0; k < WIREROPE_GROUP_MAX_NUM; k++)
xTimerChangePeriod(xSensorDataFreshTimer, g_xWITConfig.ulPeriod, 1000); {
printf("xTimerChangePeriod g_xDataSendTimer %ld\n", g_xWITConfig.ulPeriod); if(g_xDataSendTimer[k] != NULL)
{
xTimerChangePeriod(g_xDataSendTimer[k], g_xWITConfig.ulDataPeriod, 1000);
}
else
{
bStatus = false;
goto judge_status;
}
}
bNeedUpdateDataTimer = false;
} }
if(bNeedUpdateFeatureTimer == true)
{
//g_xFeatureTimer
if(g_xFeatureTimer != NULL)
xTimerChangePeriod(g_xFeatureTimer, g_xWITConfig.ulFeaturePeriod, 1000);
bNeedUpdateFeatureTimer = false;
}
// 更新数据 开始定时器 // 更新数据 开始定时器
if (bNeedUpdateCfg == true) if (bNeedUpdateCfg == true)
xTimerStart(g_xCfgUpTimer, 1000); xTimerStart(g_xCfgUpTimer, 1000);
}
judge_status: judge_status:
...@@ -7309,6 +7451,7 @@ static unsigned char WIT_GetConfigureNumberCount(void) ...@@ -7309,6 +7451,7 @@ static unsigned char WIT_GetConfigureNumberCount(void)
static void WIT_PreTaskInit(void) static void WIT_PreTaskInit(void)
{ {
int i; int i;
char cTimerName[64] = {0};
// modbus init // modbus init
// eMBInit(MB_Master, 0x1, WIT_MB_MASTER_PORT, WIT_MB_BAUDRATE, WIT_MB_PARITY); // eMBInit(MB_Master, 0x1, WIT_MB_MASTER_PORT, WIT_MB_BAUDRATE, WIT_MB_PARITY);
// eMBEnable(WIT_MB_MASTER_PORT); // eMBEnable(WIT_MB_MASTER_PORT);
...@@ -7326,10 +7469,25 @@ static void WIT_PreTaskInit(void) ...@@ -7326,10 +7469,25 @@ static void WIT_PreTaskInit(void)
g_xDLICalTimer = xTimerCreate("DLI Timer", WIT_DATA_LIST_RESTORE_TIME, pdTRUE, NULL, WIT_TimerHandler); g_xDLICalTimer = xTimerCreate("DLI Timer", WIT_DATA_LIST_RESTORE_TIME, pdTRUE, NULL, WIT_TimerHandler);
if (g_xDLICalTimer != NULL) if (g_xDLICalTimer != NULL)
xTimerStart(g_xDLICalTimer, 1000); xTimerStart(g_xDLICalTimer, 1000);
g_xDataSendTimer = xTimerCreate("IOT DataSend Timer", 10000, pdTRUE, NULL, WIT_TimerHandler);
if (g_xDataSendTimer != NULL) for(unsigned short i = 0; i < WIREROPE_GROUP_MAX_NUM; i++)
xTimerStart(g_xDataSendTimer, 5000); {
g_xErrorSendTimer = xTimerCreate("IOT ErrorSend Timer", 10000, pdTRUE, NULL, WIT_TimerHandler); snprintf(cTimerName, sizeof(cTimerName), "IOT[%d] DataSend Timer", i);
g_xDataSendTimer[i] = xTimerCreate(cTimerName, g_xWITConfig.ulDataPeriod, pdTRUE, NULL, WIT_TimerHandler);
if (g_xDataSendTimer[i] != NULL)
{
xTimerStart(g_xDataSendTimer[i], 5000);
g_xWITConfig.g_wireRopeGroup[i].pxDataSendTimer = g_xDataSendTimer[i];
}
}
g_xFeatureTimer = xTimerCreate("FeatureUploadTimer", g_xWITConfig.ulFeaturePeriod, pdTRUE, NULL, WIT_TimerHandler);
if (g_xFeatureTimer != NULL)
xTimerStart(g_xFeatureTimer, 1000);
g_xErrorSendTimer = xTimerCreate("IOT ErrorSend Timer", g_xWITConfig.ulFeaturePeriod, pdTRUE, NULL, WIT_TimerHandler);
xSensorTimeoutTimer = xTimerCreate("xSTOTimer", 60000, pdTRUE, NULL, vSensorHeartBeatTimerCallback);
if (g_xErrorSendTimer!= NULL) if (g_xErrorSendTimer!= NULL)
xTimerStart(g_xErrorSendTimer, 5000); xTimerStart(g_xErrorSendTimer, 5000);
...@@ -7345,9 +7503,9 @@ static void WIT_PreTaskInit(void) ...@@ -7345,9 +7503,9 @@ static void WIT_PreTaskInit(void)
g_xCheckInTimer = xTimerCreate("CI Timer", WIT_DATA_UPDATE_CHECKIN_TIME, pdFALSE, NULL, WIT_TimerHandler); g_xCheckInTimer = xTimerCreate("CI Timer", WIT_DATA_UPDATE_CHECKIN_TIME, pdFALSE, NULL, WIT_TimerHandler);
if (g_xGatewayFeatureTimer != NULL) if (g_xGatewayFeatureTimer != NULL)
xTimerStart(g_xGatewayFeatureTimer, 1000); xTimerStart(g_xGatewayFeatureTimer, 1000);
g_xSensorFeatureTimer = xTimerCreate("SF Timer", g_xWITConfig.ulPeriod, pdFALSE, NULL, WIT_TimerHandler); g_xSensorFeatureTimer = xTimerCreate("SF Timer", g_xWITConfig.ulFeaturePeriod, pdFALSE, NULL, WIT_TimerHandler);
g_xWaveTimer = xTimerCreate("W Timer", g_xWITConfig.ulWavePeriod, pdFALSE, NULL, WIT_TimerHandler); g_xWaveTimer = xTimerCreate("W Timer", g_xWITConfig.ulWavePeriod, pdFALSE, NULL, WIT_TimerHandler);
g_xAlarmTimer = xTimerCreate("A Timer", g_xWITConfig.ulPeriod, pdFALSE, NULL, WIT_TimerHandler); g_xAlarmTimer = xTimerCreate("A Timer", g_xWITConfig.ulFeaturePeriod, pdFALSE, NULL, WIT_TimerHandler);
// Create signal // Create signal
for (i = 0; i < WIT_MODBUS_PORT_COUNT; i++) for (i = 0; i < WIT_MODBUS_PORT_COUNT; i++)
...@@ -9129,7 +9287,7 @@ char *sent_mqtt_ip(void) ...@@ -9129,7 +9287,7 @@ char *sent_mqtt_ip(void)
return (char *)&g_xWITConfig.cTargetIP; return (char *)&g_xWITConfig.cTargetIP;
} }
#if CONFIG_W5100S_SPI_ETHERNET > 0 // #if CONFIG_W5100S_SPI_ETHERNET > 0
unsigned long get_status_dhcp(void) unsigned long get_status_dhcp(void)
{ {
return g_xWITConfig.g_xConfigIp.dhcp; return g_xWITConfig.g_xConfigIp.dhcp;
...@@ -9140,149 +9298,185 @@ wiz_NetInfo *get_gw_eth_info(void) ...@@ -9140,149 +9298,185 @@ wiz_NetInfo *get_gw_eth_info(void)
return &g_xWITConfig.g_xConfigIp; return &g_xWITConfig.g_xConfigIp;
} }
#endif // #endif
void iot_Sensor_Init(void)
{
for (int i = 0; i < IOTDEV_MAX_NUM; i++)
{
// g_xWITConfig.my_IOTDev[i].addr = 0;
// g_xWITConfig.my_IOTDev[i].id = 0;
// g_xWITConfig.my_IOTDev[i].sn = 0;
// g_xWITConfig.my_IOTDev[i].bIsUsed = false;
g_xWITConfig.my_IOTDev[i].bIsTimeout = true;
g_xWITConfig.my_IOTDev[i].bIsPlugIn = false;
g_xWITConfig.my_IOTDev[i].rope_value = 0;
g_xWITConfig.my_IOTDev[i].beingWaiting = false;
g_xWITConfig.my_IOTDev[i].status = 0;
}
}
void iot_Sensor_Print(void)
{
for (int i = 0; i < IOTDEV_MAX_NUM; i++)
{
printf("my_IOTDev[%d].addr = %d\n", i, g_xWITConfig.my_IOTDev[i].addr);
printf("my_IOTDev[%d].id = %d\n", i, g_xWITConfig.my_IOTDev[i].id);
printf("my_IOTDev[%d].sn = %d\n", i, g_xWITConfig.my_IOTDev[i].sn);
printf("my_IOTDev[%d].bIsTimeout = %d\n", i, g_xWITConfig.my_IOTDev[i].bIsTimeout);
printf("my_IOTDev[%d].bIsUsed = %d\n", i, g_xWITConfig.my_IOTDev[i].bIsUsed);
printf("my_IOTDev[%d].bIsPlugIn = %d\n", i, g_xWITConfig.my_IOTDev[i].bIsPlugIn);
printf("my_IOTDev[%d].rope_value = %d\n", i, g_xWITConfig.my_IOTDev[i].rope_value);
printf("my_IOTDev[%d].beingWaiting = %d\n", i, g_xWITConfig.my_IOTDev[i].beingWaiting);
printf("my_IOTDev[%d].status = %d\n", i, g_xWITConfig.my_IOTDev[i].status);
}
}
void iot_Sensor_Clear(bool all)
// 修改:增加二值信号量,回复响应状态
void vSensorDataFreshTimerCallback()
{ {
xSemaphoreDataResp = xSemaphoreCreateBinary(); for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
for (int i = 0; i < IOTDEV_MAX_NUM; i++)
{ {
if (g_xWITConfig.my_IOTDev[i].bIsUsed && g_xWITConfig.my_IOTDev[i].bIsPlugIn) g_xWITConfig.g_wireRopeGroup[j].DataSendFlag = false;
g_xWITConfig.g_wireRopeGroup[j].pxDataSendTimer = NULL;
for (int i = 0; i < EACH_GROUP_MAX_NUM; i++)
{ {
Get_SensorIOT_Data(g_xWITConfig.my_IOTDev[i].id, g_xWITConfig.my_IOTDev[i].sn); if(all)
if (xSemaphoreDataResp != NULL && xSemaphoreTake(xSemaphoreDataResp, pdMS_TO_TICKS(500)) == pdTRUE)
{ {
// printf("data resp success\n"); g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].addr = 0;
} g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id = 0;
else g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn = 0;
{ g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].alarmRecord = 0;
printf("data resp fail, maybe timeout or be below the threshold\n"); g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed = false;
memset(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].alias, 0, ALIAS_MAX_NAME_LENGTH);
} }
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn = false;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = false;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingStopWaiting = false;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingConfigWaiting = false;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].g_ts = 0;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].rope_value = 0;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].level = LEVEL_UNKNOWN;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = IDEL;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].emergency = false;
queueInitFifo(&g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].fifo_tail, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].fifo_data, &g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsFull);
} }
vTaskDelay(pdMS_TO_TICKS(10));
} }
} }
void iot_Sensor_Print(void)
bool IOTConfigFunction()
{ {
bool status = true; for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
// 创建二值信号量
xSemaphoreWaitResp = xSemaphoreCreateBinary();
// 遍历每个传感器
for (int i = 0; i < IOTDEV_MAX_NUM; i++)
{ {
if (g_xWITConfig.my_IOTDev[i].bIsUsed && g_xWITConfig.my_IOTDev[i].bIsPlugIn) //配置阈值条件:传感器是否使用,传感器是否插上 printf("-------------------G[%d]-----------------\n", j);
for (int i = 0; i < EACH_GROUP_MAX_NUM; i++)
{ {
g_xWITConfig.my_IOTDev[i].beingWaiting = true; printf("g_wireRopeGroup[%d].wireRopeDevs[%d].addr = %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].addr);
Set_Threshold(g_xWITConfig.my_IOTDev[i].id, g_xWITConfig.my_IOTDev[i].sn, g_xWITConfig.my_IOTThreshold); printf("g_wireRopeGroup[%d].wireRopeDevs[%d].id = %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id);
if (xSemaphoreWaitResp != NULL && xSemaphoreTake(xSemaphoreWaitResp, pdMS_TO_TICKS(500)) == pdTRUE) { printf("g_wireRopeGroup[%d].wireRopeDevs[%d].sn = %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
//printf("config success\n"); printf("g_wireRopeGroup[%d].wireRopeDevs[%d].bIsUsed = %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed);
g_xWITConfig.my_IOTDev[i].beingWaiting = false; printf("g_wireRopeGroup[%d].wireRopeDevs[%d].bIsPlugIn = %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn);
} printf("g_wireRopeGroup[%d].wireRopeDevs[%d].rope_value = %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].rope_value);
else{ printf("g_wireRopeGroup[%d].wireRopeDevs[%d].status_machine= %d\n", j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine);
status = false;
printf("config threshold fail\n");
g_xWITConfig.my_IOTDev[i].beingWaiting = false;
}
//定时器等待配置之后得到传感器响应
} }
vTaskDelay(pdMS_TO_TICKS(10));
} }
}
// 删除二值信号量
vSemaphoreDelete(xSemaphoreWaitResp);
xSemaphoreWaitResp = NULL;
return status; // bool IOTConfigFunction()
} // {
// bool status = true;
// // 创建二值信号量
// xSemaphoreWaitResp = xSemaphoreCreateBinary();
// // 遍历每个传感器
// for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
// {
// for (int i = 0; i < EACH_GROUP_MAX_NUM; i++)
// {
// if (g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed && g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn) //配置阈值条件:传感器是否使用,传感器是否插上
// {
// g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = true;
// // Set_Threshold(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn, g_xWITConfig.my_IOTThreshold);
// if (xSemaphoreWaitResp != NULL && xSemaphoreTake(xSemaphoreWaitResp, pdMS_TO_TICKS(500)) == pdTRUE) {
// //printf("config success\n");
// g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = false;
// }
// else{
// status = false;
// printf("config threshold fail\n");
// g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = false;
// }
// //定时器等待配置之后得到传感器响应
// }
// vTaskDelay(pdMS_TO_TICKS(10));
// }
// }
// // 删除二值信号量
// vSemaphoreDelete(xSemaphoreWaitResp);
// xSemaphoreWaitResp = NULL;
// return status;
// }
// 定时器回调函数, 用于检查心跳包是否超时 // 定时器回调函数, 用于检查心跳包是否超时
void vSensorTimerCallback() void vSensorHeartBeatTimerCallback()
{ {
unsigned long last_ts = 0; unsigned long last_ts = 0;
unsigned long now_ts = 0; unsigned long now_ts = 0;
unsigned long diff_ts = 0; unsigned long diff_ts = 0;
// printf("TOT Enter\n");
// 遍历每个传感器 // 遍历每个传感器
for (int i = 0; i < IOTDEV_MAX_NUM; i++) for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
{ {
if (g_xWITConfig.my_IOTDev[i].bIsUsed && g_xWITConfig.my_IOTDev[i].bIsPlugIn) for (int i = 0; i < EACH_GROUP_MAX_NUM; i++)
{ {
last_ts = g_ts[i]; if (g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed && g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn)
now_ts = xTaskGetTickCount();
diff_ts = now_ts - last_ts;
if(diff_ts < 0)
{ {
printf("ticks[now:%ld][old:%ld]\n", now_ts, last_ts); last_ts = g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].g_ts;
return; now_ts = xTaskGetTickCount();
} diff_ts = now_ts - last_ts;
// printf("my_IOTDev[%d] diff_ts[%ld]\n", i, diff_ts); if(diff_ts < 0)
if (diff_ts > pdMS_TO_TICKS(70000 * 1)) // 70s
{
// printf("Sensor[%d] is disconnected\n", i);
g_xWITConfig.my_IOTDev[i].bIsTimeout = true; // next send error to mqtt broker
if(g_xWITConfig.my_IOTDev[i].bIsPlugIn)
{ {
g_bErrorUpdateFlag = true; printf("ticks[now:%ld][old:%ld]\n", now_ts, last_ts);
g_xWITConfig.my_IOTDev[i].bIsPlugIn = false; continue;
}
// printf("g_wireRopeGroup[j].wireRopeDevs[%d] diff_ts[%ld]\n", i, diff_ts);
if (diff_ts > pdMS_TO_TICKS(65000 * 1)) // 65 sec 检查是否超时
{
printf("Sensor[%d] is disconnected\n", i);
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn = FALSE;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = MISSING;
}
else{
printf("Sensor[%d] is online\n", i);
} }
}
else{
// printf("Sensor[%d] is online\n", i);
} }
} }
vTaskDelay(pdMS_TO_TICKS(10)); vTaskDelay(pdMS_TO_TICKS(1));
} }
} }
LEVEL get_level(unsigned short data)
{
// data ---> [0,1000] ~ [0%,100%]
if(data >= 0 && data <= 70)
{
return LEVEL_0;
}
else if(data > 70 && data <= 120)
{
return LEVEL_1;
}
else if(data > 120 && data <= 170)
{
return LEVEL_2;
}
else if(data > 170)
{
return LEVEL_3;
}
else{
return LEVEL_UNKNOWN;
}
}
void Message_Deal_Function(void) void Message_Deal_Function(void)
{ {
unsigned char sensor_cmd = 0;
unsigned char sensor_work_status = 99;
unsigned char *sensor_work_param = NULL;
LEVEL now_level = LEVEL_UNKNOWN;
sIOT01B_Format g_data_frame; sIOT01B_Format g_data_frame;
Tcp_Message tcp_message_deal = {0}; Tcp_Message tcp_message_deal = {0};
bool ret = 0; bool ret = 0;
unsigned long _ts = 0; unsigned long _ts = 0;
vTaskDelay(pdMS_TO_TICKS(1000)); // 等待xSensorTimeoutTimer创建完成
xSensorTimer = xTimerCreate("SensorTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vSensorTimerCallback); // 确保xSensorTimeoutTimer定时器已经创建
xSensorDataFreshTimer = xTimerCreate("SensorDataFreshtimer", pdMS_TO_TICKS(60000*3), pdTRUE, NULL, vSensorDataFreshTimerCallback); while(xSensorTimeoutTimer == NULL)
{
xSensorTimeoutTimer = xTimerCreate("xSTOTimer", 10000, pdTRUE, NULL, vSensorHeartBeatTimerCallback);
}
xTimerStart(xSensorTimeoutTimer, 0);
printf("Message_Deal_Function start.\n");
xTimerStart(xSensorTimer, 0);
xTimerStart(xSensorDataFreshTimer, 0);
while(1) while(1)
{ {
...@@ -9294,8 +9488,9 @@ void Message_Deal_Function(void) ...@@ -9294,8 +9488,9 @@ void Message_Deal_Function(void)
ret = data_frame_parsing(tcp_message_deal.message, tcp_message_deal.rec_len, &g_data_frame); ret = data_frame_parsing(tcp_message_deal.message, tcp_message_deal.rec_len, &g_data_frame);
if(ret == true) if(ret == true)
{ {
g_slaver_status = g_data_frame.ucCmd; sensor_cmd = g_data_frame.ucCmd;
printf("CMD = [0X%02X]\n", g_slaver_status); if(sensor_cmd == WIREROPE_CMD_WAIT_AWAKE)
printf("CMD = [0X%02X]\n", sensor_cmd);
} }
else else
{ {
...@@ -9303,122 +9498,409 @@ void Message_Deal_Function(void) ...@@ -9303,122 +9498,409 @@ void Message_Deal_Function(void)
continue; continue;
} }
switch (g_slaver_status) switch (sensor_cmd)
{ {
case SLAVE_POWER_ON_CONTACK: case WIREROPE_CMD_WAIT_AWAKE: // 上电检测信号
{ {
_ts = xTaskGetTickCount(); _ts = xTaskGetTickCount();
for(int i = 0; i< IOTDEV_MAX_NUM; i++) for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
{ {
if(g_xWITConfig.my_IOTDev[i].bIsUsed && (g_data_frame.ulDevID == g_xWITConfig.my_IOTDev[i].id) && (g_data_frame.ulDevSN == g_xWITConfig.my_IOTDev[i].sn)) for(int i = 0; i< EACH_GROUP_MAX_NUM; i++)
{ {
ACK_0AH(g_data_frame.ulDevID, g_data_frame.ulDevSN); if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed && (g_data_frame.ulDevID == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id) && (g_data_frame.ulDevSN == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn))
Start_Detect(g_data_frame.ulDevID, g_data_frame.ulDevSN); {
//printf("IOT[%d] POWER ON\n", i); printf("G[%d]wireRope[%d] POWER ON\n", j, i);
g_ts[i] = _ts; // update time g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].g_ts = _ts; // 更新时间
g_xWITConfig.my_IOTDev[i].bIsPlugIn = true; // 第一次检测到设备上电,意味着设备已经接入网关 g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn = TRUE; // 已经配置的传感器中,且已经超时的,这里意味着传感器断开之后,又重新连接网关
g_xWITConfig.my_IOTDev[i].bIsTimeout = false; g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = POWER_ON;
}
} }
} }
break; break;
} }
case SLAVE_ACK: case WIREROPE_CMD_ACK_OR_NACK: // 传感器应答
{
_ts = xTaskGetTickCount(); _ts = xTaskGetTickCount();
for(int i = 0; i< IOTDEV_MAX_NUM; i++) for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
{ {
if(g_xWITConfig.my_IOTDev[i].bIsUsed && g_data_frame.ulDevID == g_xWITConfig.my_IOTDev[i].id && g_data_frame.ulDevSN == g_xWITConfig.my_IOTDev[i].sn) for(int i = 0; i< EACH_GROUP_MAX_NUM; i++)
{ {
g_ts[i] = _ts; // update time if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed && g_data_frame.ulDevID == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id && g_data_frame.ulDevSN == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn)
printf("SLAVE_ACK: %d\n", *g_data_frame.data);
if(g_xWITConfig.my_IOTDev[i].beingWaiting && *g_data_frame.data == 1)
{ {
if(xSemaphoreWaitResp != NULL) g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].g_ts = _ts; // update time
// printf("WIREROPE_CMD_ACK_OR_NACK Data = %d\n", *g_data_frame.data);
// if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting) //传感器回答响应
// {
// if(xSemaphoreDataResp != NULL)
// {
// if(*g_data_frame.data == 1)
// xSemaphoreGive(xSemaphoreDataResp);
// }
// g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = false;
// }
if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingStopWaiting)
{
if(xSemaphoreStopResp != NULL)
{
if(*g_data_frame.data == 1)
xSemaphoreGive(xSemaphoreStopResp);
}
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingStopWaiting = false;
}
if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingConfigWaiting)
{ {
xSemaphoreGive(xSemaphoreWaitResp); if(xSemaphoreConfigResp != NULL)
Stop_Detect(g_data_frame.ulDevID, g_data_frame.ulDevSN); {
vTaskDelay(10); if(*g_data_frame.data == 1)
ACK_0AH(g_data_frame.ulDevID, g_data_frame.ulDevSN); xSemaphoreGive(xSemaphoreConfigResp);
Start_Detect(g_data_frame.ulDevID, g_data_frame.ulDevSN); }
// printf("Give Response\n"); g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingConfigWaiting = false;
} }
} }
}
}
} }
break; break;
}
case SLAVE_HEARTBEAT: case WIREROPE_CMD_HEARTBEAT:
{ {
_ts = xTaskGetTickCount(); _ts = xTaskGetTickCount();
for(int i = 0; i< IOTDEV_MAX_NUM; i++) for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
{ {
if(g_xWITConfig.my_IOTDev[i].bIsUsed) for(int i = 0; i< EACH_GROUP_MAX_NUM; i++)
{ {
g_ts[i] = _ts; // update time if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed)
printf("HeartBeat... g_ts[%d] = %ld\n", i, g_ts[i]);
if(g_xWITConfig.my_IOTDev[i].bIsTimeout == true || g_xWITConfig.my_IOTDev[i].bIsPlugIn == false)
{ {
g_xWITConfig.my_IOTDev[i].bIsPlugIn = true; // 已经配置的传感器中,且已经超时的,这里意味着传感器断开之后,又重新连接网关 g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].g_ts = _ts; // update time
g_xWITConfig.my_IOTDev[i].bIsTimeout = false; if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn == FALSE)
printf("dev[%d] online again now..... \n", i); {
printf("heartbeat G[%d]S[%d]:Online\n",j,i);
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn = true; // 已经配置的传感器中,且已经超时的,这里意味着传感器断开之后,又重新连接网关
}
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = DETECTING;
} }
}
}
} }
break; break;
} }
case SLAVE_LINE_DETECT_DATA: case WIREROPE_CMD_DATA_PERCENT:
{ {
_ts = xTaskGetTickCount(); _ts = xTaskGetTickCount();
// save line // save line
unsigned short data = 0; unsigned short data = 0;
IOT01B_Get_Collect_Data(&g_data_frame, &data); IOT01B_Get_Collect_Data(&g_data_frame, &data);
//printf("Get Data: %f%%\n", data / 10.0); for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
for(int i = 0; i < IOTDEV_MAX_NUM; i++)
{ {
//printf("g_xWITConfig.my_IOTDev[%d].bIsUsed = %d\n", i, g_xWITConfig.my_IOTDev[i].bIsUsed); for(int i = 0; i < EACH_GROUP_MAX_NUM; i++)
if(g_xWITConfig.my_IOTDev[i].bIsUsed && g_data_frame.ulDevID == g_xWITConfig.my_IOTDev[i].id && g_data_frame.ulDevSN == g_xWITConfig.my_IOTDev[i].sn)
{ {
// printf("g_xWITConfig.my_IOTDev[%d].bIsUsed = %d\n", i, g_xWITConfig.my_IOTDev[i].bIsUsed); if((g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed) && (g_data_frame.ulDevID == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id) && (g_data_frame.ulDevSN == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn))
g_ts[i] = _ts; // update time
g_xWITConfig.my_IOTDev[i].rope_value = data;
// printf("g_xWITConfig.my_IOTDev[%d].rope_value = %d\n", i, g_xWITConfig.my_IOTDev[i].rope_value);
if(xSemaphoreDataResp != NULL)
{ {
xSemaphoreGive(xSemaphoreDataResp); g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].g_ts = _ts; // update time
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].rope_value = data; // update data
if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting) // 有等待的请求
{
if(xSemaphoreDataResp!= NULL)
{
now_level = get_level(data);
// printf("data = %d%%\n", data);
enqueueDataFifo(&g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].fifo_tail, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].fifo_data, data, &g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsFull);
if(now_level > LEVEL_0)
{
setAlarm(&g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].alarmRecord);
}
// 判断Level变化
// printf("-------\n");
// printf("data:%d\n",data);
// printf("now: %d\n", now_level);
// printf("old: %d\n", g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].level);
if((now_level > LEVEL_0) && (now_level != g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].level) && (g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].level != LEVEL_UNKNOWN))
{
printf("G[%d]S[%d] Level变化, 紧急上发\n",j, i);
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].emergency = true;
// g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = PUSH;
// printf("G[%d]S[%d] :: %d\n",j, i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine);
if(g_xWITConfig.g_wireRopeGroup[j].pxDataSendTimer != NULL)
xTimerChangePeriod(g_xWITConfig.g_wireRopeGroup[j].pxDataSendTimer, g_xWITConfig.ulDataPeriod, 1000);
}
xSemaphoreGive(xSemaphoreDataResp);
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = false;
// 更新Level
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].level = now_level;
}
else{
printf("xNULLx\n");
}
}
else // 已知传感器处于发送数据状态,如果此时没有bewaiting标志位,应停止其发送数据,将其状态机置为DETECTING,下一次将继续采集,同时重置物理状态(已插入),
{
if(FALSE == g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn)
{
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn = TRUE;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = DETECTING;
Stop_Detect(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
printf("dateStream G[%d]S[%d]:Online\n",j,i);
}
}
// printf("g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[%d].rope_value = %d\n", i, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].rope_value);
// g_DataUpdateFlag = true; // MQTT Upload Data
// Stop_Detect(g_data_frame.ulDevID, g_data_frame.ulDevSN);
} }
g_DataUpdateFlag = true; // MQTT Upload Data
Stop_Detect(g_data_frame.ulDevID, g_data_frame.ulDevSN);
} }
} }
break; break;
} }
case WIREROPE_CMD_WORK_STATUS_MESSAGE:
sensor_work_status = *g_data_frame.data;
if(sensor_work_status < 0 || sensor_work_status > 3)
{
printf("sensor_work status exception");
}
else if(sensor_work_status == NO_WORKING || sensor_work_status == WORKING)
{
if(xSemaphoreWaitResp != NULL)
{
xSemaphoreGive(xSemaphoreWaitResp);
printf("WIREROPE_CMD_WORK_STATUS_MESSAGE Give xSemaphoreWaitResp\n");
}
else
{
printf("WIREROPE_CMD_WORK_STATUS_MESSAGE xSemaphoreWaitResp NULL\n");
}
}
break;
case WIREROPE_CMD_WORK_PARAM:
sensor_work_param = *g_data_frame.data;
printf("g_data_frame uclen: %d\n", g_data_frame.uclen);
for(unsigned short i =0; i < g_data_frame.uclen; i++)
{
printf("data[%d]= 0x%x\n",i, *(g_data_frame.data+i));
}
break;
default: default:
break; break;
} }
//Set_Threshold(g_data_frame.ulDevID, g_data_frame.ulDevSN, 280); 设置门阈值 //Set_Threshold(g_data_frame.ulDevID, g_data_frame.ulDevSN, 280); 设置门阈值
} }
vTaskDelay(10); vTaskDelay(1);
} }
} }
unsigned long getIotConnectNum(void) unsigned long getIotConnectNum(void)
{ {
unsigned long ulExistNum = 0; unsigned long ulExistNum = 0;
for(int i = 0 ; i < IOTDEV_MAX_NUM; i++ ) for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
{ {
if(g_xWITConfig.my_IOTDev[i].bIsPlugIn == true) for(int i = 0 ; i < EACH_GROUP_MAX_NUM; i++ )
{ {
ulExistNum++; if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn == true)
{
ulExistNum++;
}
} }
} }
return ulExistNum; return ulExistNum;
} }
void Sensor_Status_Machine(void)
{
printf("status machine starts...\n");
while(1)
{
for(int j = 0; j < WIREROPE_GROUP_MAX_NUM; j++)
{
for(int i = 0; i < EACH_GROUP_MAX_NUM; i++)
{
// if(i == 0 && j == 0)
// {
// printf("statusMachine:%d\n",g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine);
// }
switch (g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine)
{
case IDEL:
{
// printf("G[%d]S[%d] IDEL\n", j, i);
vTaskDelay(10);
; // 什么都不做
break;
}
case POWER_ON: // PowerOn ---> CONFIG_THR
{
// 应答
xSemaphoreWaitResp = xSemaphoreCreateBinary();
// printf("ack %04x:%04x\n",g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
ACK_0AH(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
InquireStatus(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
if ( xSemaphoreWaitResp != NULL && xSemaphoreTake(xSemaphoreWaitResp, pdMS_TO_TICKS(100)) == pdTRUE)
{
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsPlugIn = TRUE; // 第一次检测到设备上电,意味着设备已经接入网关
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = CONFIG_THR;
printf("1 上电应答完成\n");
}
else
{
printf("1 上电应答未完成\n");
}
vSemaphoreDelete(xSemaphoreWaitResp);
xSemaphoreWaitResp = NULL;
break;
}
case CONFIG_THR:
{
// 发送配置门阈值,等待ACK,配置完成之后,传感器响应数据1表示成功,否则失败
xSemaphoreConfigResp = xSemaphoreCreateBinary();
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingConfigWaiting = true;
Set_Threshold(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn, 1);// 强制配置阈值为1
if (xSemaphoreConfigResp != NULL && xSemaphoreTake(xSemaphoreConfigResp, pdMS_TO_TICKS(100)) == pdTRUE) {
printf("2 配置阈值完成\n");
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = DETECTING;
}
else
{
printf("2 配置阈值未完成\n");
}
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingConfigWaiting = false;
if(xSemaphoreConfigResp != NULL)
vSemaphoreDelete(xSemaphoreConfigResp);
xSemaphoreConfigResp = NULL;
break;
}
case DETECTING:
{
xSemaphoreDataResp = xSemaphoreCreateBinary();
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = true; // 开启等待标志位
Start_Detect(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
if (xSemaphoreDataResp != NULL && xSemaphoreTake(xSemaphoreDataResp, pdMS_TO_TICKS(200)) == pdTRUE) {
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = STOP;
// printf("3 \n");
}
else
{
// printf("3 NULL\n");
}
// 无论采集与否,RESET变量
if(xSemaphoreDataResp != NULL)
{
vSemaphoreDelete(xSemaphoreDataResp);
}
xSemaphoreDataResp = NULL;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingDataWaiting = false;
break;
}
case STOP:
{
xSemaphoreStopResp = xSemaphoreCreateBinary();
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingStopWaiting = true;
Stop_Detect(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].id, g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].sn);
if (xSemaphoreStopResp != NULL)
{
if(xSemaphoreTake(xSemaphoreStopResp, pdMS_TO_TICKS(200)) == pdTRUE)
{
if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].emergency == 1)
{
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = PUSH;
}
else
{
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = DETECTING;
}
// printf("4 \n");
}
else{
printf("4 停止工作未完成\n");
}
}
else
{
printf("4 xSemaphoreStopResp NULL\n");
}
// 无论采集与否,RESET变量
if(xSemaphoreStopResp != NULL)
vSemaphoreDelete(xSemaphoreStopResp);
xSemaphoreStopResp = NULL;
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].beingStopWaiting = false;
if(g_xWITConfig.g_wireRopeGroup[j].DataSendFlag == TRUE && g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].bIsUsed)
{
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = PUSH;
g_xWITConfig.g_wireRopeGroup[j].DataSendFlag = false;
}
break;
}
case PUSH:
{
// Push
printf("Push\n");
if(g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].emergency == TRUE)
{
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].emergency = false;
}
if(WIT_SendIOTData(j) == TRUE)
{
printf("PUSH %f\n", g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].rope_value / 10.0);
g_xWITConfig.g_wireRopeGroup[j].wireRopeDevs[i].status_machine = DETECTING;
}
else{
printf("Push Null\n");
vTaskDelay(10);
}
break;
}
case MISSING:
{
// Push Mqtt Message
if(g_bErrorSend_TimerArr == true)
{
if(WIT_MissingErrorSend(j) == true)
{
g_bErrorSend_TimerArr = false;
printf("missing send fail\n");
}
else
{
g_bErrorSend_TimerArr = false;
printf("missing send fail\n");
}
}
break;
}
default:
break;
}
}
}
vTaskDelay(1);
}
}
#ifndef WIT_H #ifndef WIT_H
#define WIT_H #define WIT_H
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/timers.h"
#include "wit_config.h" #include "wit_config.h"
#include "stdbool.h" #include "stdbool.h"
#if CONFIG_W5100S_SPI_ETHERNET > 0 #include "../Telecom/eth/W5100S/wizchip_conf.h"
#include "../Telecom/eth/W5100S/wizchip_conf.h"
#endif
#include "mb.h" #include "mb.h"
#include "ts_iot01b.h" #include "ts_iot01b.h"
...@@ -120,25 +123,73 @@ typedef struct { ...@@ -120,25 +123,73 @@ typedef struct {
#endif #endif
typedef enum {
IDEL = 0, // 空闲,必须第一位,防止清空flash之后,初始化错误
POWER_ON = 1, // 上电应答
CONFIG_THR = 2, // 配置阈值状态
DETECTING = 3 , // 检测获取数据工作
PUSH = 4, // 正常上报MQTT
MISSING = 5, // 失联
STOP =6
}IOT_STATUS;
typedef enum{
LEVEL_UNKNOWN = 0, // 未知状态
LEVEL_0, // 轻度损耗
LEVEL_1, // 中度损伤
LEVEL_2, // 重度损伤
LEVEL_3, // 停止使用
}LEVEL;
typedef enum{
NO_WORKING = 0,
WORKING,
SENSOR_FINE,
SENSOR_ERROR
}IOT_WORK_STATUS_RESP;
#define ALIAS_MAX_NAME_LENGTH 8
//配置数据 (配置存储到内部FLASH --- 阈值与存储数据存储到外部FLASH) //配置数据 (配置存储到内部FLASH --- 阈值与存储数据存储到外部FLASH)
typedef struct typedef struct
{ {
unsigned short addr; unsigned short addr;
unsigned short id; unsigned short id;
unsigned short sn; unsigned short sn;
bool bIsTimeout; char alias[ALIAS_MAX_NAME_LENGTH];
bool bIsUsed; bool bIsUsed;
bool bIsPlugIn; bool bIsPlugIn;
unsigned int rope_value; bool beingDataWaiting;
bool beingWaiting; bool beingStopWaiting;
unsigned char status; bool beingConfigWaiting;
}IOTDev; unsigned long g_ts;
unsigned short rope_value;
LEVEL level;
bool emergency;
IOT_STATUS status_machine;
unsigned short fifo_tail;
unsigned short fifo_data[FIFO_SIZE];
bool bIsFull;
unsigned long alarmRecord;
}wireRopeDev;
typedef struct
{
wireRopeDev wireRopeDevs[EACH_GROUP_MAX_NUM];
TimerHandle_t pxDataSendTimer;
bool DataSendFlag;
}wireRopeGroup;
typedef struct typedef struct
{ {
char cModuleName[WIT_MODULE_NAME_MAX_SIZE]; //模块名称 char cModuleName[WIT_MODULE_NAME_MAX_SIZE]; //模块名称
unsigned long ulModuleNameValue; //检查模块名是否配置过 unsigned long ulModuleNameValue; //检查模块名是否配置过
unsigned long ulPeriod; //发送周期 unsigned long ulDataPeriod; //发送周期
unsigned long ulFeaturePeriod; //发送周期
unsigned long ulConfigureValue; //检查以上内容是否配置过 unsigned long ulConfigureValue; //检查以上内容是否配置过
unsigned long ulSerialNumber; //序列号 unsigned long ulSerialNumber; //序列号
unsigned long ulPackageID; //包ID unsigned long ulPackageID; //包ID
...@@ -181,12 +232,9 @@ typedef struct ...@@ -181,12 +232,9 @@ typedef struct
sSenTable xSenTable[WIT_SENSOR_MAX_CONNECT_NUM * WIT_SENSOR_GROUP_MAX_NUM]; sSenTable xSenTable[WIT_SENSOR_MAX_CONNECT_NUM * WIT_SENSOR_GROUP_MAX_NUM];
unsigned long ulBleBinSize; unsigned long ulBleBinSize;
unsigned long ulSenParmInfoValue; unsigned long ulSenParmInfoValue;
#if CONFIG_W5100S_SPI_ETHERNET > 0 wiz_NetInfo g_xConfigIp;
wiz_NetInfo g_xConfigIp;
unsigned long ulEthNetInfoValue; //检查以上信息是否配置过 unsigned long ulEthNetInfoValue; //检查以上信息是否配置过
#endif wireRopeGroup g_wireRopeGroup[WIREROPE_GROUP_MAX_NUM];
IOTDev my_IOTDev[IOTDEV_MAX_NUM];
unsigned long my_IOTThreshold;
#endif #endif
} WIT_Configure; } WIT_Configure;
...@@ -254,11 +302,12 @@ bool WIT_GetMotorWork(unsigned char ucIndex); ...@@ -254,11 +302,12 @@ bool WIT_GetMotorWork(unsigned char ucIndex);
unsigned long *get_ulPort(void); unsigned long *get_ulPort(void);
char *sent_mqtt_ip(void); char *sent_mqtt_ip(void);
#endif #endif
#if CONFIG_W5100S_SPI_ETHERNET > 0 // #if CONFIG_W5100S_SPI_ETHERNET > 0
unsigned long get_status_dhcp(void); unsigned long get_status_dhcp(void);
wiz_NetInfo *get_gw_eth_info(void); wiz_NetInfo *get_gw_eth_info(void);
#endif // #endif
void Message_Deal_Function(void); void Message_Deal_Function(void);
unsigned long getIotConnectNum(void); unsigned long getIotConnectNum(void);
void Sensor_Status_Machine(void);
#endif #endif
...@@ -11,8 +11,8 @@ extern "C" ...@@ -11,8 +11,8 @@ extern "C"
#define TAG "Witium" #define TAG "Witium"
//软件版本号 //软件版本号
#define WIT_VERSION "1.00" #define WIT_VERSION "1.01"
#define WIT_VERSION_HEX 0x0100 #define WIT_VERSION_HEX 0x0101
//此宏直接在下载正式程序用 //此宏直接在下载正式程序用
#define WIT_FIXED_MODULE_NAME 1 #define WIT_FIXED_MODULE_NAME 1
...@@ -84,6 +84,9 @@ extern "C" ...@@ -84,6 +84,9 @@ extern "C"
#define WIT_MAX_TIME (0xFFFFFED8) // MOTT上发最大时长 #define WIT_MAX_TIME (0xFFFFFED8) // MOTT上发最大时长
#define WIT_ALARM_COUNT_THRESHOLD 5 // 预警N次才上报预警 #define WIT_ALARM_COUNT_THRESHOLD 5 // 预警N次才上报预警
#define WIT_FEATURE_PERIOD (15 * 60 * 1000)
#define WIT_DATA_PERIOD (15 * 60 * 1000)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -100,7 +100,15 @@ bool ACK_0AH(unsigned short id, unsigned short sn) ...@@ -100,7 +100,15 @@ bool ACK_0AH(unsigned short id, unsigned short sn)
unsigned char cmd = 0x0A; unsigned char cmd = 0x0A;
unsigned char data_size = 0x01; unsigned char data_size = 0x01;
unsigned char data[1] = {0x01}; unsigned char data[1] = {0x01};
printf("Send 0x0A\n"); return Response_Package(id, sn, cmd, data_size, data);
}
//68 00 3F 00 01 68 0C 01 02 1F 16
bool InquireStatus(unsigned short id, unsigned short sn)
{
unsigned char cmd = 0x0C;
unsigned char data_size = 0x01;
unsigned char data[1] = {0x02};
return Response_Package(id, sn, cmd, data_size, data); return Response_Package(id, sn, cmd, data_size, data);
} }
...@@ -109,7 +117,7 @@ bool Start_Detect(unsigned short id, unsigned short sn) ...@@ -109,7 +117,7 @@ bool Start_Detect(unsigned short id, unsigned short sn)
unsigned char cmd = 0x04; unsigned char cmd = 0x04;
unsigned char data_size = 0x01; unsigned char data_size = 0x01;
unsigned char data[1] = {0x00}; unsigned char data[1] = {0x00};
printf("Send Start_Detect\n"); // printf("Send Start_Detect\n");
return Response_Package(id, sn, cmd, data_size, data); return Response_Package(id, sn, cmd, data_size, data);
} }
...@@ -123,7 +131,7 @@ bool Stop_Detect(unsigned short id, unsigned short sn) ...@@ -123,7 +131,7 @@ bool Stop_Detect(unsigned short id, unsigned short sn)
unsigned char cmd = 0x06; unsigned char cmd = 0x06;
unsigned char data_size = 0x01; unsigned char data_size = 0x01;
unsigned char data[1] = {0x00}; unsigned char data[1] = {0x00};
printf("Send Stop_Detect\n"); // printf("Send Stop_Detect\n");
return Response_Package(id, sn, cmd, data_size, data); return Response_Package(id, sn, cmd, data_size, data);
} }
...@@ -194,7 +202,7 @@ bool data_frame_parsing(unsigned char *buf, int len, sIOT01B_Format* data) ...@@ -194,7 +202,7 @@ bool data_frame_parsing(unsigned char *buf, int len, sIOT01B_Format* data)
// basic check // basic check
if(data->ucHeader != 0x68 || data->ucTail != 0x16) if(data->ucHeader != 0x68 || data->ucTail != 0x16)
{ {
printf("Header or Tail Error.\n"); // printf("Header[%2x] & Tail[%2x] Error.\n", data->ucHeader, data->ucTail);
return false; return false;
} }
...@@ -231,3 +239,84 @@ eIOT01B_Info IOT01B_Get_Collect_Data(sIOT01B_Format *df, unsigned short *value) ...@@ -231,3 +239,84 @@ eIOT01B_Info IOT01B_Get_Collect_Data(sIOT01B_Format *df, unsigned short *value)
return EIOT01B_OK; return EIOT01B_OK;
} }
void queueInitFifo(unsigned short *fifo_tail, unsigned short fifo_data[], bool *isFull)
{
(*fifo_tail) = 0;
for(unsigned short i = 0; i < FIFO_SIZE; i++)
{
fifo_data[i] = 0;
}
(*isFull) = false;
}
unsigned short calculateAverage(unsigned short fifo_data[], bool isFull)
{
if(isFull != true) return 0;
unsigned short sum = 0;
for (int i = 0; i < FIFO_SIZE; i++)
{
sum += fifo_data[i];
}
printf("sum: %d\n", sum);
return sum / FIFO_SIZE;
}
void enqueueDataFifo(unsigned short *fifo_tail, unsigned short *fifo_data, int data, bool *isFull)
{
fifo_data[(*fifo_tail)] = data;
(*fifo_tail) = (*fifo_tail) + 1;
if ((*fifo_tail) >= FIFO_SIZE)
{
(*fifo_tail) = 0;
*isFull = true;
// printf("\n");
// for(int i = 0; i< FIFO_SIZE; i++)
// {
// printf("%d ", fifo_data[i]);
// }
// printf("\n");
}
}
unsigned short maxDataWireRope(unsigned short fifo_data[], bool isFull)
{
if(isFull != true) return 0;
int max = 0;
for (int i = 0; i < FIFO_SIZE; i++)
{
if (fifo_data[i] > max)
{
max = fifo_data[i];
}
}
return max;
}
int getAlarm( int alarmRecord, bool isFull)
{
if(isFull != true) return 0;
return (alarmRecord);
}
void setAlarm( unsigned long *alarmRecord)
{
if((*alarmRecord) >= 999999)
{
(*alarmRecord) = 999999;
}
else
{
(*alarmRecord)++;
}
}
...@@ -3,16 +3,19 @@ ...@@ -3,16 +3,19 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
// Slave Command // Slave Command
#define SLAVE_POWER_ON_CONTACK 0x01 #define WIREROPE_CMD_WAIT_AWAKE 0x01
#define SLAVE_ACK 0x03 #define WIREROPE_CMD_ACK_OR_NACK 0x03
#define SLAVE_HEARTBEAT 0x05 #define WIREROPE_CMD_HEARTBEAT 0x05
#define SLAVE_LINE_DETECT_DATA 0x07 #define WIREROPE_CMD_DATA_PERCENT 0x07
#define SLAVE_RES_SYS_PARAMETER 0x09 #define WIREROPE_CMD_SYSTEM_PARAM 0x09
#define WIREROPE_CMD_WORK_STATUS_MESSAGE 0x0B
#define WIREROPE_CMD_WORK_PARAM 0x0D
// Master Command // Master Command
#define MASTER_ACK 0x0A #define MASTER_ACK 0x0A
#define MASTER_START_DETECT 0x04 #define MASTER_START_DETECT 0x04
// Frame Define // Frame Define
...@@ -21,10 +24,14 @@ ...@@ -21,10 +24,14 @@
#define FRAME_TAIL 0x16 #define FRAME_TAIL 0x16
#define TCP_MESSAGE_MAX_LEN 256 #define TCP_MESSAGE_MAX_LEN 280
#define EACH_GROUP_MAX_NUM 4
#define WIREROPE_GROUP_MAX_NUM 2
#define IOTDEV_MAX_NUM 10
#define FIFO_SIZE 100
typedef enum { typedef enum {
EIOT01B_OK = 0, EIOT01B_OK = 0,
...@@ -65,12 +72,13 @@ eIOT01B_Info IOT01B_Set_Stop_Collect(unsigned long ulDevId); ...@@ -65,12 +72,13 @@ eIOT01B_Info IOT01B_Set_Stop_Collect(unsigned long ulDevId);
eIOT01B_Info IOT01B_Get_Collect_Data(sIOT01B_Format *df, unsigned short *value); eIOT01B_Info IOT01B_Get_Collect_Data(sIOT01B_Format *df, unsigned short *value);
eIOT01B_Info IOT01B_Data_Parsing(void *ulData, unsigned short usSize); eIOT01B_Info IOT01B_Data_Parsing(void *ulData, unsigned short usSize);
void Message_Deal_Start_Task(void); void message_deal_start_task(void);
void message_hex(unsigned char *buf, unsigned short len); void message_hex(unsigned char *buf, unsigned short len);
bool Message_Send(uint8_t *smessage, int len); bool Message_Send(uint8_t *smessage, int len);
bool data_frame_parsing(unsigned char *buf, int len, sIOT01B_Format* data); bool data_frame_parsing(unsigned char *buf, int len, sIOT01B_Format* data);
eIOT01B_Info IOT01B_Data_Parsing(void *ulData, unsigned short usSize); eIOT01B_Info IOT01B_Data_Parsing(void *ulData, unsigned short usSize);
bool ACK_0AH(unsigned short id, unsigned short sn); bool ACK_0AH(unsigned short id, unsigned short sn);
bool InquireStatus(unsigned short id, unsigned short sn);
bool Set_Threshold(unsigned short id, unsigned short sn, unsigned short threshold); bool Set_Threshold(unsigned short id, unsigned short sn, unsigned short threshold);
bool Start_Detect(unsigned short id, unsigned short sn); bool Start_Detect(unsigned short id, unsigned short sn);
bool Stop_Detect(unsigned short id, unsigned short sn); bool Stop_Detect(unsigned short id, unsigned short sn);
...@@ -78,6 +86,12 @@ void Get_SensorIOT_Data(unsigned short id, unsigned short sn); ...@@ -78,6 +86,12 @@ void Get_SensorIOT_Data(unsigned short id, unsigned short sn);
void IOT_SetConfig(); void IOT_SetConfig();
// unsigned long getIotConnectNum(void); // unsigned long getIotConnectNum(void);
void queueInitFifo(unsigned short *fifo_tail, unsigned short fifo_data[], bool *isFull);
unsigned short calculateAverage(unsigned short fifo_data[], bool isFull) ;
unsigned short maxDataWireRope(unsigned short fifo_data[], bool isFull) ;
int getAlarm( int alarmRecord, bool isFull);
void setAlarm( unsigned long *alarmRecord);
void enqueueDataFifo(unsigned short *fifo_tail, unsigned short *fifo_data, int data, bool *isFull) ;
......
...@@ -36,6 +36,8 @@ extern uint8 pub_buf[1024]; ...@@ -36,6 +36,8 @@ extern uint8 pub_buf[1024];
void ReadData(void); void ReadData(void);
static void make_basic_config_setting_json_callback(int8 *buf, CONFIG_MSG config_msg) static void make_basic_config_setting_json_callback(int8 *buf, CONFIG_MSG config_msg)
{ {
sprintf(buf, "settingsCallback({\"ver\":\"%X.%02X\",\ sprintf(buf, "settingsCallback({\"ver\":\"%X.%02X\",\
...@@ -224,9 +226,9 @@ void WriteData(CONFIG_MSG *config, uint16_t length) ...@@ -224,9 +226,9 @@ void WriteData(CONFIG_MSG *config, uint16_t length)
wiz_NetInfo *eth_info = NULL; wiz_NetInfo *eth_info = NULL;
unsigned long *value = NULL; unsigned long *value = NULL;
char *mqtt_ip = NULL; char *mqtt_ip = NULL;
#if CONFIG_W5100S_SPI_ETHERNET > 0 // #if CONFIG_W5100S_SPI_ETHERNET > 0
eth_info = get_gw_eth_info(); eth_info = get_gw_eth_info();
#endif // #endif
value = get_ulPort(); value = get_ulPort();
mqtt_ip = sent_mqtt_ip(); mqtt_ip = sent_mqtt_ip();
if(eth_info == NULL || value == NULL || mqtt_ip == NULL) if(eth_info == NULL || value == NULL || mqtt_ip == NULL)
...@@ -268,9 +270,9 @@ void ReadData(void) ...@@ -268,9 +270,9 @@ void ReadData(void)
ConfigMsg.mqttport[0] = *value % 256; ConfigMsg.mqttport[0] = *value % 256;
ConfigMsg.mqttport[1] = *value / 256; ConfigMsg.mqttport[1] = *value / 256;
#if CONFIG_W5100S_SPI_ETHERNET > 0 // #if CONFIG_W5100S_SPI_ETHERNET > 0
ConfigMsg.dhcpconfig = get_status_dhcp(); ConfigMsg.dhcpconfig = get_status_dhcp();
#endif // #endif
ConfigMsg.dhcpconfig -= 1; ConfigMsg.dhcpconfig -= 1;
printf("IP, GW and Subnet updated successfully in gWIZNETINFO.\r\n"); printf("IP, GW and Subnet updated successfully in gWIZNETINFO.\r\n");
......
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
#include "socket.h" #include "socket.h"
#include "wizchip_conf.h" #include "wizchip_conf.h"
#include "w5100s_conf.h" #include "w5100s_conf.h"
#include "eth.h" #if CONFIG_W5100S_SPI_ETHERNET > 0
#include "eth.h"
#endif
#include "ts_iot01b.h" #include "ts_iot01b.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
...@@ -34,28 +36,24 @@ uint16 local_port = 11111; // 定义本地端口11111 ...@@ -34,28 +36,24 @@ uint16 local_port = 11111; // 定义本地端口11111
uint8_t g_IOTBuffer[200]; uint8_t g_IOTBuffer[200];
Tcp_Message g_Tcp_Message; Tcp_Message g_Tcp_Message;
extern QueueHandle_t xMessageQueueHandle; extern QueueHandle_t xMessageQueueHandle;
TimerHandle_t xSensorTimer = NULL; TimerHandle_t xSensorTimeoutTimer = NULL;
TimerHandle_t xSensorDataFreshTimer = NULL; TimerHandle_t xSensorDataUpdateTimer = NULL;
TCP_STATUS g_TcpServerStatus = TCP_SERVER_SOCKET_CLOSE; TCP_STATUS g_TcpServerStatus = TCP_SERVER_SOCKET_CLOSE;
extern TimerHandle_t g_xDataSendTimer[];
void do_tcp_server(void) void do_tcp_server(void)
{ {
uint8_t count =0; uint8_t ret;
my_socket(2, Sn_MR_TCP, 11111, 0); uint16 len;
portBASE_TYPE ret_pB;
// my_socket(2, Sn_MR_TCP, 11111, 0);
while(1) while(1)
{ {
uint16 len = 0; len = 0;
uint8_t ret = getSn_SR(SOCK_TCPS); ret = getSn_SR(SOCK_TCPS);
count++;
if(count == 500)
{
count = 0;
printf(">>>>>getSn_SR %d\n",ret);
}
// printf(" getSn_SR %d\n",ret);
switch(ret) // 获取socket的状态 switch(ret) // 获取socket的状态
{ {
case SOCK_CLOSED: // socket处于关闭状态 case SOCK_CLOSED: // socket处于关闭状态
...@@ -82,18 +80,24 @@ void do_tcp_server(void) ...@@ -82,18 +80,24 @@ void do_tcp_server(void)
len = getSn_RX_RSR(SOCK_TCPS); // 定义len为已接收数据的长度 len = getSn_RX_RSR(SOCK_TCPS); // 定义len为已接收数据的长度
if(len > 0) if(len > 0)
{ {
if(len > 256) if(len > 280)
{ {
len =256; len = 265;
} }
recv(SOCK_TCPS, g_Tcp_Message.message, len); // 接收来自Client的数据 recv(SOCK_TCPS, g_Tcp_Message.message, len); // 接收来自Client的数据
g_Tcp_Message.rec_len = len; // 添加字符串结束符 g_Tcp_Message.rec_len = len; // 添加字符串结束符
// printf("sock receive len[%d]: %s\n", g_Tcp_Message.rec_len, g_Tcp_Message.message); // printf("sock receive len[%d]: %s\n", g_Tcp_Message.rec_len, g_Tcp_Message.message);
if (xMessageQueueHandle != NULL) if (xMessageQueueHandle != NULL)
{ {
xQueueSend( xMessageQueueHandle, &g_Tcp_Message, 0);
// disconnect(SOCK_TCPS); // 断开当前TCP连接 ret_pB = xQueueSend( xMessageQueueHandle, &g_Tcp_Message, 0); // 将数据发送到消息队列中
// close(SOCK_TCPS); // 关闭当前所使用socket if(ret_pB == pdTRUE)
{
// printf("xQSend OK\n");
}
else
printf("xQSend Exx\n");
} }
else else
{ {
...@@ -129,7 +133,7 @@ bool Message_Send(uint8_t *smessage, int len) ...@@ -129,7 +133,7 @@ bool Message_Send(uint8_t *smessage, int len)
} }
else else
{ {
printf("TCP SERVER DISCONNECT!!! \nTCP_SERVER_STATUS is [%d]\n", g_TcpServerStatus); // printf("No Client ESTABLISHED [%x]\n", g_TcpServerStatus);
return false; return false;
} }
} }
...@@ -137,14 +141,18 @@ bool Message_Send(uint8_t *smessage, int len) ...@@ -137,14 +141,18 @@ bool Message_Send(uint8_t *smessage, int len)
void tcp_server_task_start(void) void tcp_server_task_start(void)
{ {
printf("tcp_server_task START\n"); printf("1 start\n");
xTaskCreate(do_tcp_server, "tcp_server_task", 2048, NULL, 5, NULL); xTaskCreate(do_tcp_server, "tcp_server_task", 2048, NULL, 5, NULL);
} }
void Message_Deal_Start_Task(void) void message_deal_start_task(void)
{ {
printf("Message_Deal_Task START\n"); printf("2 start\n");
xMessageQueueHandle = xQueueCreate(40, sizeof(Tcp_Message));
xTaskCreate(Message_Deal_Function, "Message_Deal_Function", 8192, NULL, 5, NULL); while(xMessageQueueHandle == NULL)
{
xMessageQueueHandle = xQueueCreate(100, sizeof(Tcp_Message));
}
xTaskCreate(Sensor_Status_Machine, "StatusMachine", 5120, NULL, 5, NULL);
xTaskCreate(Message_Deal_Function, "MessageDealFunction", 10240, NULL, 5, NULL);
} }
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
#include "driver/gpio.h" #include "driver/gpio.h"
#include "spi.h" #include "spi.h"
#include "esp_rom_gpio.h" #include "esp_rom_gpio.h"
#include "eth.h" #if CONFIG_W5100S_SPI_ETHERNET > 0
#include "eth.h"
#endif
#include "wit.h" #include "wit.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
...@@ -74,7 +76,9 @@ void ETHERNET_Init(bool *bModuleStatus, bool *bCableStatus) ...@@ -74,7 +76,9 @@ void ETHERNET_Init(bool *bModuleStatus, bool *bCableStatus)
vTaskDelay(2000 / portTICK_PERIOD_MS); vTaskDelay(2000 / portTICK_PERIOD_MS);
} }
// hw_ping_init(); // hw_ping_init();
if_eth_task_init(); #if CONFIG_W5100S_SPI_ETHERNET > 0
if_eth_task_init();
#endif
} }
/** /**
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
/* Private macros ------------------------------------------------------------*/ /* Private macros ------------------------------------------------------------*/
#define CELLUAR_POWER_KEY_PIN 1 #define CELLUAR_POWER_KEY_PIN 1
#define CELLUAR_STATUS_PIN 38 #define CELLUAR_STATUS_PIN 38
#define POWER_KEY_PIN 46 #define POWER_KEY_PIN 3
#define CELLUAR_POWER_BASE_TIME 500 //500ms #define CELLUAR_POWER_BASE_TIME 500 //500ms
...@@ -26,8 +26,8 @@ void Celluar_ConfigureGPIO(void) ...@@ -26,8 +26,8 @@ void Celluar_ConfigureGPIO(void)
esp_rom_gpio_pad_select_gpio(CELLUAR_STATUS_PIN); esp_rom_gpio_pad_select_gpio(CELLUAR_STATUS_PIN);
gpio_set_direction(CELLUAR_STATUS_PIN, GPIO_MODE_INPUT); // gpio_set_direction(CELLUAR_STATUS_PIN, GPIO_MODE_INPUT); //
// POWER_KEY_PIN // POWER_KEY_PIN
// esp_rom_gpio_pad_select_gpio(POWER_KEY_PIN); // 选择个GPIO esp_rom_gpio_pad_select_gpio(POWER_KEY_PIN); // 选择个GPIO
// gpio_set_direction(POWER_KEY_PIN, GPIO_MODE_OUTPUT); // 把这个GPIO作为输出 gpio_set_direction(POWER_KEY_PIN, GPIO_MODE_OUTPUT); // 把这个GPIO作为输出
gpio_set_level(POWER_KEY_PIN, 1); gpio_set_level(POWER_KEY_PIN, 1);
// // EC20 PWR设置 // // EC20 PWR设置
......
...@@ -98,7 +98,7 @@ void Celluar_ConfigureUSB(void (*pFuncIdleCallBack)(void), void (*pFuncRxneCallB ...@@ -98,7 +98,7 @@ void Celluar_ConfigureUSB(void (*pFuncIdleCallBack)(void), void (*pFuncRxneCallB
{ {
usbh_cdc_driver_install(&config); usbh_cdc_driver_install(&config);
/* Waiting for USB device connected */ /* Waiting for USB device connected */
usbh_cdc_wait_connect(portMAX_DELAY); usbh_cdc_wait_connect(5000);
g_xUSBHandler.pFuncRxneHandler = pFuncRxneCallBack; g_xUSBHandler.pFuncRxneHandler = pFuncRxneCallBack;
g_xUSBHandler.pFuncIdleHandler = pFuncIdleCallBack; g_xUSBHandler.pFuncIdleHandler = pFuncIdleCallBack;
gUSBSemaphore = xSemaphoreCreateBinary(); gUSBSemaphore = xSemaphoreCreateBinary();
......
...@@ -21,25 +21,22 @@ ...@@ -21,25 +21,22 @@
#include "freertos/queue.h" #include "freertos/queue.h"
#include "wit.h" #include "wit.h"
#if CONFIG_W5100S_SPI_ETHERNET > 0
#include "httputil.h" #include "httputil.h"
#endif
#define MAIN_TASK_STK (configMINIMAL_STACK_SIZE * 10) #define MAIN_TASK_STK (configMINIMAL_STACK_SIZE * 10)
#define MAIN_TASK_PRIORITY (tskIDLE_PRIORITY + 3) #define MAIN_TASK_PRIORITY (tskIDLE_PRIORITY + 3)
#if CONFIG_W5100S_SPI_ETHERNET > 0
#define HTTP_TASK_STK (configMINIMAL_STACK_SIZE * 20) #define HTTP_TASK_STK (configMINIMAL_STACK_SIZE * 20)
#define HTTP_TASK_PRIORITY (tskIDLE_PRIORITY + 2) #define HTTP_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
#endif
#define TWDT_TIMEOUT_S 3 // TWDT超时时间 #define TWDT_TIMEOUT_S 3 // TWDT超时时间
#define TASK_RESET_PERIOD_S 2 // 任务重置周期时间 #define TASK_RESET_PERIOD_S 2 // 任务重置周期时间
static void Main_Task(void *pvParameter); static void Main_Task(void *pvParameter);
#if CONFIG_W5100S_SPI_ETHERNET > 0 static void Http_Task(void *pvParameter);
static void Http_Task(void *pvParameter);
#endif
static int bWITInitFin = 0; static int bWITInitFin = 0;
...@@ -48,9 +45,7 @@ void app_main(void) ...@@ -48,9 +45,7 @@ void app_main(void)
int rc = 0; int rc = 0;
ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(nvs_flash_init());
xTaskCreate(Main_Task, "Main Task", MAIN_TASK_STK, NULL, MAIN_TASK_PRIORITY, NULL); xTaskCreate(Main_Task, "Main Task", MAIN_TASK_STK, NULL, MAIN_TASK_PRIORITY, NULL);
#if CONFIG_W5100S_SPI_ETHERNET > 0
rc = xTaskCreate(Http_Task, "Http Task", HTTP_TASK_STK, NULL, HTTP_TASK_PRIORITY, NULL); rc = xTaskCreate(Http_Task, "Http Task", HTTP_TASK_STK, NULL, HTTP_TASK_PRIORITY, NULL);
#endif
printf("rc is %d\r\n", rc); printf("rc is %d\r\n", rc);
} }
...@@ -68,11 +63,10 @@ static void Main_Task(void *pvParameter) ...@@ -68,11 +63,10 @@ static void Main_Task(void *pvParameter)
vTaskDelay(20 / portTICK_PERIOD_MS); vTaskDelay(20 / portTICK_PERIOD_MS);
} }
} }
#if CONFIG_W5100S_SPI_ETHERNET > 0
static void Http_Task(void *pvParameter) static void Http_Task(void *pvParameter)
{ {
//Init //Init
// printf("Enter Http Task\n");
while(1) while(1)
{ {
if(bWITInitFin == 1) if(bWITInitFin == 1)
...@@ -82,4 +76,4 @@ static void Http_Task(void *pvParameter) ...@@ -82,4 +76,4 @@ static void Http_Task(void *pvParameter)
vTaskDelay(1 / portTICK_PERIOD_MS); vTaskDelay(1 / portTICK_PERIOD_MS);
} }
} }
#endif
...@@ -471,8 +471,8 @@ CONFIG_PARTITION_TABLE_MD5=y ...@@ -471,8 +471,8 @@ CONFIG_PARTITION_TABLE_MD5=y
# Witium Gateway Configure # Witium Gateway Configure
# #
# CONFIG_ESP32_WIFI is not set # CONFIG_ESP32_WIFI is not set
# CONFIG_EC200S_USB_4G is not set CONFIG_EC200S_USB_4G=y
CONFIG_W5100S_SPI_ETHERNET=y # CONFIG_W5100S_SPI_ETHERNET is not set
# CONFIG_RS485_RTU is not set # CONFIG_RS485_RTU is not set
CONFIG_BLUETOOTH=y CONFIG_BLUETOOTH=y
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment