Zigbee广播通信就是网络内的任何一个节点设备发送出广播数据,网络中的任何设备都可以接收到所发送的数据。
ztack层次结构:
PHY:从无线物理信道上为MAC层提供数据收发的服务
MAC:保证协议数据正确收发
NWK:兼容IEEE802.15.4标准,并为上层提数据NLDE和管理NLME服务
APS :为ZDO访问NWK提供了通路
AF :为各个用户自定义的应用对象提供了模板式的活动空间
APL :基于zigbee协议栈,建立在ZDO基础之上的具体应用
zigbee无线通信:
信道:tools目录中的f8wConfig.cfg中定义:
//-DDEFAULT_CHANLIST=0x04000000 // 26 - 0x1A
//-DDEFAULT_CHANLIST=0x02000000 // 25 - 0x19
//-DDEFAULT_CHANLIST=0x01000000 // 24 - 0x18
//-DDEFAULT_CHANLIST=0x00800000 // 23 - 0x17
//-DDEFAULT_CHANLIST=0x00400000 // 22 - 0x16
//-DDEFAULT_CHANLIST=0x00200000 // 21 - 0x15
//-DDEFAULT_CHANLIST=0x00100000 // 20 - 0x14
//-DDEFAULT_CHANLIST=0x00080000 // 19 - 0x13
//-DDEFAULT_CHANLIST=0x00040000 // 18 - 0x12
//-DDEFAULT_CHANLIST=0x00020000 // 17 - 0x11
//-DDEFAULT_CHANLIST=0x00010000 // 16 - 0x10
//-DDEFAULT_CHANLIST=0x00008000 // 15 - 0x0F
//-DDEFAULT_CHANLIST=0x00004000 // 14 - 0x0E
//-DDEFAULT_CHANLIST=0x00002000 // 13 - 0x0D
//-DDEFAULT_CHANLIST=0x00001000 // 12 - 0x0C
-DDEFAULT_CHANLIST=0x00000800 // 11 - 0x0B
panID,16bit网络地址,只有panID相同的设备才能组成一个网络
/* Define the default PAN ID.
*
* Setting this to a value other than 0xFFFF causes
* ZDO_COORD to use this value as its PAN ID and
* Routers and end devices to join PAN with this ID
*/
-DZDAPP_CONFIG_PAN_ID=0xFF00
hortAddr,由父节点分配的16bit短地址,用于设备之间通信
endPoint,是一个1-240的具体设备端点号,与应用相关
zigbee网络中有3种通信设备:
全功能设备FFD协调器
全功能设备FFD路由器
半功能设备RFD终端
有且只能有一个协调器,协调器,路由器,终端节点由软件决定
1.基于端点进行通信,所以首先需要注册端点
*功能:注册一个断点
*参数:@epDesc 描述端点信息的结构体指针
* typedef struct
* {
* uint8 endPoint;//端点号1-240
* uint8 *task_id; //当前任务ID
* SimpleDescriptionFormat_t *simpleDesc;//端点简单描述符
* afNetworkLatencyReq_t latencyReq;//网络延迟信标设置,一般默认参数noLatencyReqs
* } endPointDesc_t;
typedef struct
{
uint8 EndPoint;//端点号1-240
uint16 AppProfId;//profile应用规范ID,表示应用领域比如建筑、农业、家居等
uint16 AppDeviceId;//应用设备ID
uint8 AppDevVer:4;//应用设备版本号
uint8 Reserved:4; //保留
uint8 AppNumInClusters;//输入簇命令个数,如打开、关闭、停止等操作命令
cId_t *pAppInClusterList;//输入簇命令ID列表
uint8 AppNumOutClusters;//输出簇命令个数
cId_t *pAppOutClusterList;//输出簇命令ID列表
} SimpleDescriptionFormat_t;
*/
afStatus_t afRegister( endPointDesc_t *epDesc )
/*无线消息发送函数*/
afStatus_t AF_DataRequest( afAddrType_t *dstAddr,//目标地址
endPointDesc_t *srcEP,//源端点
uint16 cID, //簇命令ID,对于接收方可以通过该命令来区分数据包
uint16 len, //发送数据的长度
uint8 *buf, //发送数据的起始地址
uint8 *transID,//事务ID,是包计数器,用于信息确认
uint8 options, //告知AF层做的一些预处理,一般为AF_DISCV_ROUTE
uint8 radius )//转发数据最多经过的路由深度(AF_DEFAULT_RADIUS)
typedef struct
{
union
{
uint16 shortAddr;
ZLongAddr_t extAddr;
} addr;
afAddrMode_t addrMode;
uint8 endPoint;
uint16 panId; // used for the INTER_PAN feature
} afAddrType_t;
实例
MytaskApp.c
#include "MytaskApp.h"
#include "led.h"
#include <stdio.h>
#include <string.h>
#include "wen_shi.h"
uint8 MytaskApp_TaskID;
uint8 MytaskApp_TransID; // This is the unique message ID (counter)
devStates_t MytaskApp_NwkState;
afAddrType_t My_DstAddr;
void MytaskApp_MessageMSGCB( afIncomingMSGPacket_t *pkt );
const SimpleDescriptionFormat_t MytaskApp_SimpleDesc =
{
21, // 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;
};
endPointDesc_t MytaskApp_epDesc = {
.endPoint = 21,
.task_id = &MytaskApp_TaskID,
.simpleDesc = (SimpleDescriptionFormat_t *)&MytaskApp_SimpleDesc,
.latencyReq = noLatencyReqs,
};
void MytaskApp_Init( uint8 task_id )
{
MytaskApp_TaskID = task_id;
//My_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//地址初始化
My_DstAddr.addrMode = (afAddrMode_t)afAddr16Bit; //单播
My_DstAddr.endPoint = 21;
//My_DstAddr.addr.shortAddr = 0xFFFF;
My_DstAddr.addr.shortAddr = 0x0;
//osal_start_timerEx( MytaskApp_TaskID, MYEVENT_DHT11, 500);
IO_Init(); //点灯 初始化
MT_UartInit(); //串口初始化
MT_UartRegisterTaskID(MytaskApp_TaskID); //串口初始化
afRegister( &MytaskApp_epDesc );
//HalUARTWrite(0,"Hello jiange\n",13); //(串口0,'字符',字符个数。)
}
unsigned short MytaskApp_ProcessEvent( unsigned char task_id, unsigned short events )
{
afIncomingMSGPacket_t *MSGpkt;
(void)task_id; // Intentionally unreferenced parameter
if(events & MYEVENT_DHT11)
{
led_on();
osal_start_timerEx( MytaskApp_TaskID, MYEVENT_DHT11, 1000);
return events ^ MYEVENT_DHT11;
}
if ( events & SYS_EVENT_MSG )
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( MytaskApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
// Received when a messages is received (OTA) for this endpoint
case AF_INCOMING_MSG_CMD:
MytaskApp_MessageMSGCB( MSGpkt );
break;
// Received whenever the device changes state in the network
case ZDO_STATE_CHANGE:
MytaskApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
if ( //(MytaskApp_NwkState == DEV_ZB_COORD)//|| //协调器不给自己点播
(MytaskApp_NwkState == DEV_ROUTER)
|| (MytaskApp_NwkState == DEV_END_DEVICE) )
//)
{
// Start sending the periodic message in a regular interval.
osal_start_timerEx( MytaskApp_TaskID,
SEND_TEMP_HUMI,
2000 );
}
break;
default:
break;
}
// Release the memory
osal_msg_deallocate( (uint8 *)MSGpkt );
// Next - if one is available
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( MytaskApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
if ( events & SEND_TEMP_HUMI )
{
#if 1
uint8 buff[32];
DHT11();
sprintf(buff,"%d:%d", ucharRH_data_H,ucharT_data_H);
AF_DataRequest( &My_DstAddr, &MytaskApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
(uint16) (strlen(buff) + 1),//字节数
buff,//指针头
&MytaskApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
#endif
#if 0
AF_DataRequest( &My_DstAddr, &MytaskApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
6,//字节数
"hello\0",//指针头
&MytaskApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );
#endif
osal_start_timerEx( MytaskApp_TaskID,SEND_TEMP_HUMI,1000);
return events ^ SEND_TEMP_HUMI;
}
return 0;
}
void MytaskApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
printf("%s\n", pkt->cmd.Data);
//printf("end_addr:%#x\n",pkt->srcAddr.addr.shortAddr);
}
MytaskApp.h
#ifndef MYTASKAPP_H
#define MYTASKAPP_H
#include "led.h"
#include "hal_types.h"
#include "OSAL_Timers.h"
#include "SampleApp.h"
#include "MT_UART.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZComDef.h"
#define MYEVENT_DHT11 0x1
#define SEND_TEMP_HUMI 0x2
extern void MytaskApp_Init( unsigned char task_id );
extern unsigned short MytaskApp_ProcessEvent( unsigned char task_id, unsigned short events );
//extern void MytaskApp_MessageMSGCB( afIncomingMSGPacket_t *pkt );
extern const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS];
#endif