uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )

本文深入探讨了一个特定应用任务事件处理器的实现细节,详细解释了如何处理不同类型的事件,如消息、键盘输入和网络状态变化,以及周期性消息的定时发送机制。通过实例分析,展示了事件提取、消息处理和系统事件管理的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



/*

* @fnSampleApp_ProcessEvent

* @brief Generic Application Task event processor. This function

* is called to process all events for the task. Events

* include timers, messages and any other user defined events.

* @paramtask_id - The OSAL assigned task ID.任务ID

* @param events - events to process. This is a bit map and can

* contain more than one event. 处理的事件,这是一个位图

* @return none

*/

uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )

{

afIncomingMSGPacket_t *MSGpkt;

//系统事件号 SYS_EVENT_MSG = 0x8000

if ( events & SYS_EVENT_MSG )

{

//检索收到的命令,没有收到返回NULL

MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );

while ( MSGpkt ) //如果不为空时,判断消息的类型

{

switch ( MSGpkt->hdr.event ) //这里是判断SYS_EVENT_MSG事件类型,不同的SYS_EVENT_MSG类型需要不同的处理。

{

/* Received when a key is pressed这里判断是否是键盘事件,如果键盘事件就调用键盘处理函数。
如果一个OSAL任务已经被登记组侧,那么任何键盘事件都将接受一个KEY_CHANGE事件信息。可能有如下几种方式得到键盘事件信息
1)、HAL检测到键盘按下(中断或者查询检测)
2)、HALOSAL任务检测到一个键盘状态改变调用回叫函数产生
3)、OSAL键盘改变回叫函数发送一个OSAL系统事件信息(KEY_CHANGE)。*/

case KEY_CHANGE:

SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );

break;

// Received when a messages is received (OTA) for this endpoint 收到信息事件

case AF_INCOMING_MSG_CMD:

SampleApp_MessageMSGCB( MSGpkt ); //执行信息回调函数

break;

// Received whenever the device changes state in the network 网络中的设备状态发生改变时,产生的事件消息

case ZDO_STATE_CHANGE:

SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); //获得当前的状态

if ( (SampleApp_NwkState == DEV_ZB_COORD) //判断其类型

|| (SampleApp_NwkState == DEV_ROUTER)

|| (SampleApp_NwkState == DEV_END_DEVICE) )

{

// Start sending the periodic message in a regular interval. 发送周期信息

osal_start_timerEx(SampleApp_TaskID, 

SAMPLEAPP_SEND_PERIODIC_MSG_EVT,

SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );

}

else

{

// Device is no longer in the network

}

break;

default:

break;

}

// Release the memory 释放内存

osal_msg_deallocate( (uint8 *)MSGpkt );

// Next - if one is available 得到任务中下一个等待处理的事件

MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );

}

// return unprocessed events 返回没有处理的事件

return (events ^ SYS_EVENT_MSG);

}

// Send a message out - This event is generated by a timer

// (setup in SampleApp_Init()). 向外发送信息,该事件是由定时器产生

if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )

{

// Send the periodic message 发送周期信息

SampleApp_SendPeriodicMessage();

// Setup to send message again in normal period (+ a little jitter) 第三个参数是定时的时间

osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,

(SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );

// return unprocessed events 返回没有处理的事件

return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);

}

// Discard unknown events

return 0;

}

说明:(1)afIncomingMSGPacket_t *MSGpkt; 其中afIncomingMSGPacket_t是一个结构体,这个结构体中包括传输的帧的格式。

typedefstruct

{

osal_event_hdr_thdr;

uint16groupId;

uint16clusterId;

afAddrType_tsrcAddr;

byteendPoint;

bytewasBroadcast;

byteLinkQuality;

byteSecurityUse;

uint32 timestamp;

afMSGCommandFormat_tcmd;

} afIncomingMSGPacket_t;

(2) 这里的SYS_EVENT_MSG是系统事件,也是协议栈已经定义好的系统事件,在文件ZcomDef.h中,事件号是一个16bit的常量,使用叫作独热码(one-hot code)编码,也是一个位表示一个事件,方便进行event的提取,这样一个task最多可以有16eventSYS_EVENT_MSG已经占用了0x8000,故自定义的个事件只能有15个,事件的提取和清除可以用简单的位操作指令实现,事件的提取可以用位与操作 events & SYS_EVENT_MSG,事件的清除可以用异或操作实现,evets ^ SYS_EVENT_MSG ,系统事件包括了各种系统消息(message,系统事件中的消息号是一个8bit常量,也就是一事件可以包括255个消息,定义在ZcomDef.h中。

#define SYS_EVENT_MSG 0x8000 // A message is waiting event

/*

* Global System Messages

*/

#define SPI_INCOMING_ZTOOL_PORT 0x21 // Raw data from ZTool Port (not implemented)

#define SPI_INCOMING_ZAPP_DATA 0x22 // Raw data from the ZAPP port (see serialApp.c)

#define MT_SYS_APP_MSG 0x23 // Raw data from an MT Sys message

#define MT_SYS_APP_RSP_MSG 0x24 // Raw data output for an MT Sys message

#define AF_DATA_CONFIRM_CMD 0xFD // Data confirmation

#define AF_INCOMING_MSG_CMD 0x1A // Incoming MSG type message

#define AF_INCOMING_KVP_CMD 0x1B // Incoming KVP type message

#define AF_INCOMING_GRP_KVP_CMD 0x1C // Incoming Group KVP type message

#define KEY_CHANGE 0xC0 // Key Events

#define ZDO_NEW_DSTADDR 0xD0 // ZDO has received a new DstAddr for this app

#define ZDO_STATE_CHANGE 0xD1 // ZDO has changed the device's network state

#define ZDO_MATCH_DESC_RSP_SENT 0xD2 // ZDO match descriptor response was sent

#define ZDO_CB_MSG 0xD3 // ZDO incoming message callback

用户自己定义的系统事件的消息范围为0xE0—0xFF,下面是几个比较常用的系统事件的消息。

 AF_DATA_CONFIRM_CMD
调用AF_DataRequest()函数数据请求成功的指示。Zsuccess确认数据请求传输成功,如果数据请求设置AF_ACK_REQUEST标志位,那么,只有最终目的地址成功接收后,Zsuccess确认才返回。如果如果数据请求没有设置AF_ACK_REQUEST标志位,那么,数据请求只要成功传输到下跳节点就返回Zsuccess确认信息。

 AF_INCOMING_MSG_CMD 收到MSG消息,通知任务进行处理

 KEY_CHANGE 按键处理事件

 ZDO_NEW_DSTADDR 新的目标地址接收到指示自动匹配请求绑定时经常使用

 ZDO_STATE_CHANGE 设备状态变化指示

3osal_start_timerEx( SampleApp_TaskID,

SAMPLEAPP_SEND_PERIODIC_MSG_EVT,

SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );

osal_start_timerEx()的作用是启动一系统定时器,当其溢出 SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT)时,会触发tasSampleApp_TaskID)的事件 SAMPLEAPP_SEND_PERIODIC_MSG_EVT)。看到事件SAMPLEAPP_SEND_PERIODIC_MSG_EVT了,它是用户定义的事件,分析之

if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )

{

// Send the periodic message

SampleApp_SendPeriodicMessage();

// Setup to send message again(为什么重新发送) in normal period (+ a little jitter)

osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,

(SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );

// return unprocessed events

return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); }

以上就是SAMPLEAPP_SEND_PERIODIC_MSG_EVT的处理函数,先分析总的流程,SampleApp_SendPeriodicMessage();发送信息,osal_start_timerEx()重新启动一系统定时 器,同样是指向taskSampleApp_TaskID)的事件(SAMPLEAPP_SEND_PERIODIC_MSG_EVT),返回时要注意清除当前事件 (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT),否则会反复处理同一个事件,陷入死循环。

SampleApp.h中定义

#define SAMPLEAPP_SEND_PERIODIC_MSG_EVT 0x0001

在这个例子中,调用了osal_start_timerEx()函数来定时产生发送周期信息事件,而定时器的运行是设备一旦加入网络就不停的运行,用函数SampleApp_SendPeriodicMessage();发送周期信息,而用函数osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,

(SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );

来继续运行定时器定时发送一个周期信息,osal_start_timerEx()函数,第一个参数是处理事件的任务ID号,第二个参数是事件的类型,也就是事件是一个什么事件,第三个参数是需要定时时间,发送周期信息的时间周期。

 

4void SampleApp_SendPeriodicMessage(void)定时器发送周期信息的函数里调用了AF_DataRequest()函数用来发送数据,发送数据的过程是把数据从应用层传到网络层,再传到MAC,再传到物理层,最后通过OTA发送出去,接收的过程是相反的过程,在接收到消息后在应用层的反应就是,会发送一个AF_INCOMING_MSG_CMD消息事件,case AF_INCOMING_MSG_CMD:

SampleApp_MessageSGCB(MSGpkt);

Break;

这里表示收到某个信息,然后在里面调用了收到信息后的处理函数。SampleApp_MessageSGCB(MSGpkt);

void SampleApp_SendPeriodicMessage(void)这个函数主要就是调用发送数据的函数AF_DataRequest()对数据进行发送。

/*

* @fnSampleApp_SendPeriodicMessage

* @brief Send the periodic message.

* @param none

* @return none

*/

voidSampleApp_SendPeriodicMessage( void )

{

if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,

SAMPLEAPP_PERIODIC_CLUSTERID,

1,

(uint8*)&SampleAppPeriodicCounter,

&SampleApp_TransID,

AF_DISCV_ROUTE,

AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )

{

}

else

{

// Error occurred in request to send.

}

}

5case AF_INCOMING_MSG_CMD: 
SampleApp_MessageMSGCB( MSGpkt ); 
break; 
这里表示收到某个信息,然后在里面调用了收到信息的信息处理函数SampleApp_MessageMSGCB( MSGpkt )

/*

* @fnSampleApp_MessageMSGCB

* @brief Data message processor callback. This function processes

* any incoming data - probably from other devices. So, based

* on cluster ID, perform the intended action.

* @param none

* @return none

*/

voidSampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )

{

uint16flashTime;

switch ( pkt->clusterId )

{

case SAMPLEAPP_PERIODIC_CLUSTERID:

break;

case SAMPLEAPP_FLASH_CLUSTERID:

flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );

HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );

break; 

}

}

这里判断了两种信息:周期信息;闪灯信息,根据它们的簇ID号的不同做出不同的处理。不同的信息就相当于收到了不同的命令,然后根据不同的命令做出了不同的处理

 

 

以上代码是在IAR环境下超声波代码下面是main函数代码#include "iocc2530.h" #include "AF.h" #include "DNUI_SampleApp.h" #include "OnBoard.h" #include "string.h" #include "hal_uart.h" #include "OSAL.h" #include "ZGlobals.h" #include "aps_groups.h" #include "ZDApp.h" #include "OnBoard.h" /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "stdio.h" #include "Temperature.h" #include "SmartUltra.h" #include "hal_uart.h" #include "SmartDHT11.h" #include "SmartLightSensor.h" // 氝樓嫖鏗換覜け芛恅璃 #include "ultrasonic.h" // 添加超声波传感器支持 #include "hal_mcu.h" typedef unsigned char uint8; typedef unsigned short uint16; cId_t DNUI_SampleApp_ClusterIDS[DNUI_SAMPLEAPP_CLUSTER_NUM] = { DNUI_SAMPLEAPP_DATATEST_CLUSTER_ID }; SimpleDescriptionFormat_t DNUI_Sample_SimpleDesc = { DNUI_SAMPLEAPP_ENDPOINT, DNUI_SAMPLEAPP_PROFILE_ID, DNUI_SAMPLEAPP_DEVICE_ID, DNUI_SAMPLEAPP_DEVICE_VER, 0, DNUI_SAMPLEAPP_CLUSTER_NUM, (cId_t*)DNUI_SampleApp_ClusterIDS, DNUI_SAMPLEAPP_CLUSTER_NUM, (cId_t*)DNUI_SampleApp_ClusterIDS }; uint8 DNUI_SampleAppTaskID; devStates_t DNUI_SampleApp_NwkState; //悵湔厙釐袨怓 uint8 DNUI_SampleApp_TransID; //悵湔秏洘ID afAddrType_t Broadcast_Addr, Coor_Addr; //狟俴杅擂華硊 endPointDesc_t DNUI_SampleApp_epDesc; //傷萸鏡扴睫 uint8 DHT11DataBuf[20]; uint8 DHT11BufLen = 0; // 氝樓嫖桽ッ僅遣喳Е uint8 LightDataBuf[10]; uint8 LightBufLen = 0; void DNUI_SampleApp_Init(uint8 task_id); uint16 DNUI_SampleApp_ProcessEvent(uint8 task_id, uint16 events); void DNUI_SampleApp_Init(uint8 task_id) { //--start--基本变量初始化--- DNUI_SampleAppTaskID = task_id; DNUI_SampleApp_NwkState = DEV_INIT; DNUI_SampleApp_TransID = 0; //--end--基本变量初始化--- //--start--注册端点描述符-- DNUI_SampleApp_epDesc.endPoint = DNUI_SAMPLEAPP_ENDPOINT; DNUI_SampleApp_epDesc.task_id = &DNUI_SampleAppTaskID; DNUI_SampleApp_epDesc.latencyReq = noLatencyReqs; DNUI_SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t*)&DNUI_Sample_SimpleDesc; afRegister(&DNUI_SampleApp_epDesc); //--end--注册端点描述符-- UartInit(NULL); SmartLightSensor_Init(); //--start--设置协调器地址-- Coor_Addr.addrMode = (afAddrMode_t)Addr16Bit; Coor_Addr.endPoint = DNUI_SAMPLEAPP_ENDPOINT; Coor_Addr.addr.shortAddr = 0x0; // 协调器地址 // 添加超声波模块初始化 // 初始化引脚 // 在DNUI_SampleApp_Init中添加 P1SEL &= ~0x02; // 确保P1.1是GPIO模式 P1DIR &= ~0x02; // P1.1设为输入 P1INP &= ~0x02; // 使能上拉电阻 P2INP &= ~0x20; // 选择上拉而非下拉 (bit5=0) // 配置定时器1 (分频128, 16MHz/128 = 125kHz -> 8us/计数) T1CTL = 0x0E; // 分频128, 自由运行模式 T1CCTL0 = 0x00; // 通道0比较模式禁用 //--end--设置协调器地址-- } uint16 DNUI_SampleApp_ProcessEvent(uint8 task_id, uint16 events) { afIncomingMSGPacket_t* MSGpkt; uint16 distance = Measure_Distance(); if (events & SYS_EVENT_MSG) { MSGpkt = (afIncomingMSGPacket_t*)osal_msg_receive(task_id); while (MSGpkt) { switch (MSGpkt->hdr.event) { case AF_INCOMING_MSG_CMD: if (DNUI_SampleApp_NwkState == DEV_ZB_COORD) { //--start--衪覃け諉彶杅擂腔揭燴軀憮 // 眻諉蛌楷埻宎杅擂善揹諳 UartSendString(0, MSGpkt->cmd.Data, MSGpkt->cmd.DataLength); // 褫恁ㄩ氝樓遙俴睫 uint8 newline[2] = {'\r', '\n'}; UartSendString(0, newline, 2); // LED硌尨 HalLedSet(HAL_LED_1, HAL_LED_MODE_TOGGLE); } else if (DNUI_SampleApp_NwkState == DEV_END_DEVICE) { //--start--笝傷扢掘諉彶杅擂腔揭燴軀憮 } else if (DNUI_SampleApp_NwkState == DEV_ROUTER) { //--start--繚蚕け諉彶杅擂腔揭燴軀憮 } break; case ZDO_STATE_CHANGE://郪厙 DNUI_SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if (DNUI_SampleApp_NwkState == DEV_END_DEVICE)//笝傷誹萸樓?厙釐 { //--start--笝傷扢掘諉彶杅擂腔揭燴軀憮 osal_start_timerEx(task_id,DNUI_SAMPLEAPP_ENDDEVICE_PERIODIC_MSG_EVT,1000);//笚ヽ俶腔隅奀¦昢 HalLedSet(HAL_LED_1,HAL_LED_MODE_ON); SmartDHT11_Init(task_id); } else if (DNUI_SampleApp_NwkState == DEV_ZB_COORD) { //--start--衪覃け諉彶杅擂腔揭燴軀憮 HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); } else if (DNUI_SampleApp_NwkState == DEV_ROUTER) { //--start--繚蚕け諉彶杅擂腔揭燴軀憮 } break; default: break; } osal_msg_deallocate((uint8*)MSGpkt); MSGpkt = (afIncomingMSGPacket_t*)osal_msg_receive(DNUI_SampleAppTaskID); } return (events ^ SYS_EVENT_MSG); } if(events & DNUI_SAMPLEAPP_ENDDEVICE_PERIODIC_MSG_EVT) { // 黍?DHT11 uint8 status = SmartDHT11_GetVal(DHT11DataBuf, &DHT11BufLen); // 黍?嫖桽ッ僅 uint16 lightValue = SmartLightSensor_GetVal(); uint8 lightBuf[10]; uint8 lightLen = sprintf((char*)lightBuf, "%d", lightValue); // 添加超声波距离测量 uint16 distance = Measure_Distance(); // 获取超声波距离 uint8 distBuf[10]; uint8 distLen = sprintf((char*)distBuf, "%d", distance); // 组合所有传感器数据 (格式: "温度,湿度,光照,距离") uint8 combinedBuf[50]; uint8 combinedLen ; if(distance == 0xFFFF) { combinedLen = sprintf((char*)combinedBuf, "%d,%d,%d,ERR", DHT11DataBuf[0], DHT11DataBuf[1], lightValue); } else { combinedLen = sprintf((char*)combinedBuf, "%d,%d,%d,%d", DHT11DataBuf[0], DHT11DataBuf[1], lightValue, distance); } AF_DataRequest(&Coor_Addr, &DNUI_SampleApp_epDesc, DNUI_SAMPLEAPP_DATATEST_CLUSTER_ID, combinedLen, combinedBuf, &DNUI_SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); osal_start_timerEx(task_id, DNUI_SAMPLEAPP_ENDDEVICE_PERIODIC_MSG_EVT, 1000); return (events ^ DNUI_SAMPLEAPP_ENDDEVICE_PERIODIC_MSG_EVT); } return 0; } void UartSendString(uint8 port, uint8 *buf, uint8 len) { // 妏蚚Z-Stack腔HalUARTWrite滲杅妗珋揹諳楷冞 HalUARTWrite(port, buf, len); } 现在超声波接受不到数据解决这个问题
最新发布
07-10
/************************************************************************************************** Filename: SampleApp.c Revised: $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $ Revision: $Revision: 19453 $ Description: Sample Application (no Profile). **************************************************************************************************/ /********************************************************************* * INCLUDES */ #include <stdio.h> #include <string.h> #include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "MT_UART.h" #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "sht11.h" /********************************************************************* * CONSTANTS */ /********************************************************************* * GLOBAL VARIABLES */ const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] = { SAMPLEAPP_PERIODIC_CLUSTERID, SAMPLEAPP_FLASH_CLUSTERID, SAMPLEAPP_LED_CLUSTERID, SAMPLEAPP_TEMP_CLUSTERID, }; const SimpleDescriptionFormat_t SampleApp_SimpleDesc = { SAMPLEAPP_ENDPOINT, SAMPLEAPP_PROFID, SAMPLEAPP_DEVICEID, SAMPLEAPP_DEVICE_VERSION, SAMPLEAPP_FLAGS, SAMPLEAPP_MAX_CLUSTERS, (cId_t *)SampleApp_ClusterList, SAMPLEAPP_MAX_CLUSTERS, (cId_t *)SampleApp_ClusterList }; endPointDesc_t SampleApp_epDesc; /********************************************************************* * LOCAL VARIABLES */ uint8 SampleApp_TaskID; devStates_t SampleApp_NwkState; uint8 SampleApp_TransID; afAddrType_t SampleApp_Periodic_DstAddr; afAddrType_t SampleApp_Flash_DstAddr; aps_Group_t SampleApp_Group; uint8 SampleAppPeriodicCounter = 0; uint8 SampleAppFlashCounter = 0; /********************************************************************* * LOCAL FUNCTIONS */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ); void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void SampleApp_SendPeriodicMessage( void ); void SampleApp_SendFlashMessage( uint16 flashTime ); void SampleApp_Process_SensorInt(void); void SampleApp_ProcessIntMessage( afIncomingMSGPacket_t *pkt ); void Delay(uint16 n); void SensorIO_Init(void); /********************************************************************* * PUBLIC FUNCTIONS */ void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; SampleApp_Periodic_DstAddr.addrMode = AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; SampleApp_Flash_DstAddr.addrMode = afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; afRegister( &SampleApp_epDesc ); RegisterForKeys( SampleApp_TaskID ); MT_UartRegisterTaskID( SampleApp_TaskID ); SampleApp_Group.ID = 0x0001; osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); Sht11Init(); #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); #endif } uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { case KEY_CHANGE: SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt ); break; case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if ( (SampleApp_NwkState == DEV_ZB_COORD) || (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ) { HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); } break; default: break; } osal_msg_deallocate( (uint8 *)MSGpkt ); MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); } return (events ^ SYS_EVENT_MSG); } if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) { SampleApp_SendPeriodicMessage(); osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } return 0; } void SetLedStatus(unsigned char *buf) { if(*buf==0x00){ P1_0=0; } else if(*buf==0x01){ P1_0=1; } else if(*buf==0x03){ P1_2=1; } else if(*buf==0x04){ P1_2=0; } } void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { if ( keys & HAL_KEY_SW_6 ) { SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION ); } if ( keys & HAL_KEY_SW_2 ) { aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } } } void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; unsigned char *buf; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: buf = pkt->cmd.Data; HalUARTWrite(0,"\r\nTemp:", 7); HalUARTWrite(0, buf, 7); HalUARTWrite(0," Humi:", 10); HalUARTWrite(0, buf+7, 7); break; case SAMPLEAPP_LED_CLUSTERID: buf =pkt->cmd.Data; SetLedStatus(buf); break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; } } void SampleApp_SendFlashMessage( uint16 flashTime ) { uint8 buffer[3]; buffer[0] = (uint8)(SampleAppFlashCounter++); buffer[1] = LO_UINT16( flashTime ); buffer[2] = HI_UINT16( flashTime ); if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 3, buffer, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { } } void SampleApp_SendPeriodicMessage( void ) { char temp_buf[7]; char humi_buf[7]; char i; char buf[14]; float humi,temp; if(GetHumiAndTemp(&humi,&temp) == 0) { sprintf(humi_buf, (char *)"%f", humi); sprintf(temp_buf, (char *)"%f", temp); for(i=0; i<7; i++) { buf[i] = temp_buf[i]; buf[i+7] = humi_buf[i]; } AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 14, (unsigned char*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); } } void SampleApp_ProcessMTMessage(afIncomingMSGPacket_t *msg) { byte len = msg->hdr.status; uint8 *msgPtr = ((unsigned char *)msg+2); HalUARTWrite ( 0, msgPtr, len); uint8 status; if(strncmp(msgPtr, "on", 2) == 0){ status = 0x00; HalUARTWrite ( 0, "\r led on\r", 12); } else if(strncmp(msgPtr, "off", 3) == 0){ status = 0x01; HalUARTWrite ( 0, "\r led off\r", 13); } else if(strncmp(msgPtr, "beepon", 6) == 0){ status = 0x03; HalUARTWrite ( 0, "\rset beep on\r", 13); } else if(strncmp(msgPtr, "beepoff", 7) == 0){ status = 0x04; HalUARTWrite ( 0, "\rset beep off\r", 13); } if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_LED_CLUSTERID, 1, &status, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { } } 代码是这样的 xcom上从硬件传来的数据是乱码
06-23
/************************************************************************************************** Filename: SampleApp.h Revised: $Date: 2007-10-27 17:22:23 -0700 (Sat, 27 Oct 2007) $ Revision: $Revision: 15795 $ Description: This file contains the Sample Application definitions. Copyright 2007 Texas Instruments Incorporated. All rights reserved. IMPORTANT: Your use of this Software is limited to those specific rights granted under the terms of a software license agreement between the user who downloaded the software, his/her employer (which must be your employer) and Texas Instruments Incorporated (the "License"). You may not use this Software unless you agree to abide by the terms of the License. The License limits your use, and you acknowledge, that the Software may not be modified, copied or distributed unless embedded on a Texas Instruments microcontroller or used solely and exclusively in conjunction with a Texas Instruments radio frequency transceiver, which is integrated into your product. Other than for the foregoing purpose, you may not use, reproduce, copy, prepare derivative works of, modify, distribute, perform, display or sell this Software and/or its documentation for any purpose. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. Should you have any questions regarding your right to use this Software, contact Texas Instruments Incorporated at www.TI.com. **************************************************************************************************/ #ifndef SAMPLEAPP_H #define SAMPLEAPP_H #ifdef __cplusplus extern "C" { #endif /********************************************************************* * INCLUDES */ #include "ZComDef.h" /********************************************************************* * CONSTANTS */ // These constants are only for example and should be changed to the // device's needs #define SAMPLEAPP_ENDPOINT 20 #define SAMPLEAPP_PROFID 0x0F08 #define SAMPLEAPP_DEVICEID 0x0001 #define SAMPLEAPP_DEVICE_VERSION 0 #define SAMPLEAPP_FLAGS 0 #define SAMPLEAPP_MAX_CLUSTERS 2 #define SAMPLEAPP_PERIODIC_CLUSTERID 1 #define SAMPLEAPP_FLASH_CLUSTERID 2 #define SAMPLEAPP_LEDCTL_CLUSTERID 3 // Send Message Timeout #define SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT 5000 // Every 5 seconds // Application Events (OSAL) - These are bit weighted definitions. #define SAMPLEAPP_SEND_PERIODIC_MSG_EVT 0x0001 // Group ID for Flash Command #define SAMPLEAPP_FLASH_GROUP 0x0001 // Flash Command Duration - in milliseconds #define SAMPLEAPP_FLASH_DURATION 1000 /********************************************************************* * MACROS */ /********************************************************************* * FUNCTIONS */ /* * Task Initialization for the Generic Application */ extern void SampleApp_Init( uint8 task_id ); /* * Task Event Processor for the Generic Application */ extern UINT16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ); /********************************************************************* *********************************************************************/ #ifdef __cplusplus } #endif #endif /* SAMPLEAPP_H */ /************************************************************************************************** Filename: SampleApp.c Revised: $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $ Revision: $Revision: 19453 $ Description: Sample Application (no Profile). Copyright 2007 Texas Instruments Incorporated. All rights reserved. IMPORTANT: Your use of this Software is limited to those specific rights granted under the terms of a software license agreement between the user who downloaded the software, his/her employer (which must be your employer) and Texas Instruments Incorporated (the "License"). You may not use this Software unless you agree to abide by the terms of the License. The License limits your use, and you acknowledge, that the Software may not be modified, copied or distributed unless embedded on a Texas Instruments microcontroller or used solely and exclusively in conjunction with a Texas Instruments radio frequency transceiver, which is integrated into your product. Other than for the foregoing purpose, you may not use, reproduce, copy, prepare derivative works of, modify, distribute, perform, display or sell this Software and/or its documentation for any purpose. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. Should you have any questions regarding your right to use this Software, contact Texas Instruments Incorporated at www.TI.com. **************************************************************************************************/ /********************************************************************* This application isn't intended to do anything useful, it is intended to be a simple example of an application's structure. This application sends it's messages either as broadcast or broadcast filtered group messages. The other (more normal) message addressing is unicast. Most of the other sample applications are written to support the unicast message model. Key control: SW1: Sends a flash command to all devices in Group 1. SW2: Adds/Removes (toggles) this device in and out of Group 1. This will enable and disable the reception of the flash command. *********************************************************************/ /********************************************************************* * INCLUDES */ #include <stdio.h> #include <string.h> #include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "MT_UART.h" //add by 1305106 #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include"sht11.h" /********************************************************************* * MACROS */ /********************************************************************* * CONSTANTS */ /********************************************************************* * TYPEDEFS */ /********************************************************************* * GLOBAL VARIABLES */ // This list should be filled with Application specific Cluster IDs. const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] = { SAMPLEAPP_PERIODIC_CLUSTERID, SAMPLEAPP_FLASH_CLUSTERID, }; const SimpleDescriptionFormat_t SampleApp_SimpleDesc = { SAMPLEAPP_ENDPOINT, // int Endpoint; SAMPLEAPP_PROFID, // uint16 AppProfId[2]; SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2]; SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4; SAMPLEAPP_FLAGS, // int AppFlags:4; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList // uint8 *pAppInClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in SampleApp_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t SampleApp_epDesc; /********************************************************************* * EXTERNAL VARIABLES */ /********************************************************************* * EXTERNAL FUNCTIONS */ /********************************************************************* * LOCAL VARIABLES */ uint8 SampleApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // SampleApp_Init() is called. devStates_t SampleApp_NwkState; uint8 SampleApp_TransID; // This is the unique message ID (counter) afAddrType_t SampleApp_Periodic_DstAddr; afAddrType_t SampleApp_Flash_DstAddr; aps_Group_t SampleApp_Group; uint8 SampleAppPeriodicCounter = 0; uint8 SampleAppFlashCounter = 0; /********************************************************************* * LOCAL FUNCTIONS */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ); void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void SampleApp_SendPeriodicMessage( void ); void SampleApp_SendFlashMessage( uint16 flashTime ); /********************************************************************* * NETWORK LAYER CALLBACKS */ /********************************************************************* * PUBLIC FUNCTIONS */ /********************************************************************* * @fn SampleApp_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; // Device hardware initialization can be added here or in main() (Zmain.c). // If the hardware is application specific - add it here. // If the hardware is other parts of the device add it in main(). #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, // the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to // start the device. ZDOInitDevice(0); #endif // Setup for the periodic message's destination address // Broadcast to everyone SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // Setup for the flash command's destination address - Group 1 SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; // Fill out the endpoint description. SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); MT_UartRegisterTaskID( SampleApp_TaskID ); //add by 1305106 // By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001; osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); #endif } /********************************************************************* * @fn SampleApp_ProcessEvent * * @brief Generic Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none */ uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { // Received when a key is pressed case KEY_CHANGE: SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; // Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt ); break;; // Received whenever the device changes state in the network case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if ( (SampleApp_NwkState == DEV_ZB_COORD) || (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ) { // Start sending the periodic message in a regular interval. HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); } else { // Device is no longer in the network } break; default: break; } osal_msg_deallocate( (uint8 *)MSGpkt ); // Release the memory MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); // Next - if one is available } return (events ^ SYS_EVENT_MSG); // return unprocessed events } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) { SampleApp_SendPeriodicMessage(); // Send the periodic message // Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); // return unprocessed events } return 0; // Discard unknown events } /********************************************************************* * Event Generation Functions */ /********************************************************************* * @fn SampleApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_6 ) { /* This key sends the Flash Command is sent to Group 1. * This device will not receive the Flash Command from this * device (even if it belongs to group 1). */ SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION ); } if ( keys & HAL_KEY_SW_2 ) { /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesn't belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { // Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { // Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } } } /********************************************************************* * LOCAL FUNCTIONS */ /********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */ void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; unsigned char *buf; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: buf = pkt->cmd.Data; HalUARTWrite(0,"\r\nTemp:", 7); HalUARTWrite(0, buf, 7); HalUARTWrite(0," Humi:", 6); HalUARTWrite(0, buf+7, 7); break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; } } /********************************************************************* * @fn SampleApp_SendPeriodicMessage * * @brief Send the periodic message. * * @param none * * @return none */ void SampleApp_SendPeriodicMessage( void ) { char temp_buf[7]; char humi_buf[7]; char i; char buf[14]; float humi,temp; if(GetHumiAndTemp(&humi,&temp) == 0) { sprintf(humi_buf, (char *)"%f", humi); sprintf(temp_buf, (char *)"%f", temp); for(i=0; i<7; i++) { buf[i] = temp_buf[i]; buf[i+7] = humi_buf[i]; } AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 14, (unsigned char*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); } } /********************************************************************* * @fn SampleApp_SendFlashMessage * * @brief Send the flash message to group 1. * * @param flashTime - in milliseconds * * @return none */ void SampleApp_SendFlashMessage( uint16 flashTime ) { uint8 buffer[3]; buffer[0] = (uint8)(SampleAppFlashCounter++); buffer[1] = LO_UINT16( flashTime ); buffer[2] = HI_UINT16( flashTime ); if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 3, buffer, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { // Error occurred in request to send. } } /********************************************************************* *********************************************************************/ 一、系统分层功能 感知层:用实验箱的两个传感器模块 传感器 1:温湿度传感器,采集温度、湿度数据。 传感器 2:红外对射 ,采集触发状态。 网络层: Zigbee 通信:传感器 1、2 的数据,经 Zigbee 节点传给协调器,协调器通过串口发 PC 端。 数据处理:解析串口数据,分主题发 MQTT 网络,或存数据库 。 应用层:用 Python 做终端,实现界面、串口、数据库交互(可加 MQTT),完成这些功能: 二、具体功能 数据上传: 远程设备实时收传感器 1 数据,格式:学号姓名缩写+温度+湿度 。 远程设备实时收传感器 2 数据,格式:学号姓名缩写+传感器名+interrupt 。 Mysql 数据库:建不同数据表存两个传感器数据,表含 学号姓名缩写、传感器名、数据值、传感器状态 等字段 。 命令下发: 发 学号姓名缩写+Num1Led+on ,传感器 1 连的 Zigbee 模块 LED2 亮;发 …+off 则灭 。 发 学号姓名缩写+Num2Led+on ,传感器 2 连的 Zigbee 模块 LED2 亮;发 …+off 则灭 。 数据联动: 传感器 1:温度>25 且湿度>60,其连的 Zigbee 模块 LED2 闪烁,远程设备、数据库表显示 “温湿度异常”;恢复后显示 “异常解除” 。 传感器 2:触发超 5 次,其连的 Zigbee 模块 LED2 闪烁,远程设备、数据库表显示 “传感状态异常”;恢复后显示 “传感异常解除” 。 强制解除:发 学号姓名缩写+Num1Led+relie ,传感器 1 连的 LED2 停闪,设备和表显示 “强制异常解除温湿度” ;发 …+Num2Led+relie 同理。 数据展示:Python 界面里,串口控制,实时显示传感器 1 的 学号姓名缩写、温度、湿度 数据,呈现底层传感器联动状态 。 根据所给代码和要求,完成功能,给出完整的代码
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值