文章目录
一、介绍
- 硬件:4块CC2530模块,2个DS18B20温度传感器、1个DHT11传感器、一个抽水器(可无)。
- 软件:基于C#开发上位机(.NET Framework 4.8)、ZStack-2.5.1a协议栈。
- 由协调器及各节点组建星型网,采用点播通讯(P2P),结合上位机的限制最多可以接入6个终端节点。
- 硬件效果图
二、上位机数据采集
(一)采集数据
打开串口,显示情况和波动。
(二)拓扑结构
显示节点的数据和其短地址。
(三)新节点加入网络
(四)节点事件
节点4湿度高于 70%,进行提示,拓扑图隐藏该节点,与此同时抽水器停止工作。
三、实现分析
SampleApp.c中的SampleApp_ProcessMSGCmd函数内可以修改,最好是switch ( pkt->clusterId )case分别编写终端和协调器逻辑,当时是用id的奇偶判断是不好。
(一)配置文件
- 在f8wConfig.cfg中
设置信道 : -DDEFAULT_CHANLIST=
(似乎11信道容易受WIFI信号影响)
设置PAN_ID: -DZDAPP_CONFIG_PAN_ID= - 在SampleApp.h
数据发送间隔 3 s
#define SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT 3000
(二)发送
- 在 SampleApp.c 中
//无线发送到协调器
if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_P2P_CLUSTERID,
len, // 数据长度
str, // 数据内容
&SampleApp_MsgID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
}
}
(三)短地址和数据获取
短地址 pkt->srcAddr.addr.shortAddr 是网络随机分配的,
MAC地址 pkt->macDestAddr 更适合作为数据存储的主键,
数据 uint8 temperature=pkt->cmd.Data[0]; uint8 humidity=pkt->cmd.Data[1];
- 在AF.h中
typedef struct
{
union
{
uint16 shortAddr; // (短地址)
ZLongAddr_t extAddr;
} addr;
afAddrMode_t addrMode;
uint8 endPoint;
uint16 panId; // used for the INTER_PAN feature
} afAddrType_t;
typedef struct
{
osal_event_hdr_t hdr; /* OSAL Message header */
uint16 groupId; /* Message's group ID - 0 if not set */
uint16 clusterId; /* Message's cluster ID (簇ID) */
afAddrType_t srcAddr; /* Source Address, if endpoint is STUBAPS_INTER_PAN_EP,
it's an InterPAN message */
uint16 macDestAddr; /* MAC header destination short address (MAC地址) */
uint8 endPoint; /* destination endpoint */
uint8 wasBroadcast; /* TRUE if network destination was a broadcast address */
uint8 LinkQuality; /* The link quality of the received data frame (网络连接质量) */
uint8 correlation; /* The raw correlation value of the received data frame */
int8 rssi; /* The received RF power in units dBm (信号能量) */
uint8 SecurityUse; /* deprecated */
uint32 timestamp; /* receipt timestamp from MAC */
uint8 nwkSeqNum; /* network header frame sequence number */
afMSGCommandFormat_t cmd; /* Application Data */
} afIncomingMSGPacket_t;
- 在 SampleApp.h 中
void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
{
uint8 buff[50]={
0};
switch ( pkt->clusterId )
{
// 接收终端上传的温湿度数据
case SAMPLEAPP_P2P_CLUSTERID:
{
// 取出温湿度数据
uint8 temperature = pkt->cmd.Data[0];
uint8 humidity= pkt->cmd.Data[0];
// 取出短地址
uint16 shortAddress = pkt->srcAddr.addr.shortAddr;
// 处理和数据打包上传串口略····
}
break;
// 接收温度
case SAMPLEAPP_DS18B20_CLUSTER:
{
// 略····
}
break;
default:
break;
(四)数据包分析
串口数据打包格式:
长度:14;
校验位:C4;
功能码:02;
数据:3B 31 3B 54 3A 32 37 2E 30 20 43 3B 34 34 34 31 3B
结尾:0D 0A 。
补充
- 数据部分的3B作为数据内容分隔符(;):(功能码)+(;)+(采集数据)+(;)+(短地址)+(;)。
- 并以换行符为结尾 \r\n 比较省事
(五)上位机
// 是否打开串口
bool isOpenSP;
// 记录数
private int recordNum = 0;
// 记录各个节点的检测结果 用于 拓扑图
private string[] pointsTopology = {
"", "", "", "", "", ""};
// 绘图
private Graphics graphics;
private void mainForm_Load