基于Zigbee的SHT10温湿度数据采集系统+python串口读取——Zigbee终端节点主要代码解析
1)温湿度传感器用采用的是SHT10,之前用的是DHT11采集温湿度数据,结果精度不行,所以换成SHT10传感器。
2)我组的是星状网络,一个Zigbee协调器,12个Zigbee终端节点。
3)当Zigbee路由节点上电后,会使用NLME_GetShortAddr()函数获取自身16位的网络地址信息,然后AF_DataRequest给协调器。
void SampleApp_to_xie_addr(void)//ok
{
uint16 NLMEAddr;
if(AddrID<=9)
{
//1~9
NLMEAddr =NLME_GetShortAddr();//获得16位的网络地址信息
NLME_addr[0]=0;
NLME_addr[1]=AddrID;
NLME_addr[2]=(unsigned char)((NLMEAddr>>8)&0xff);
NLME_addr[3]=(unsigned char)(NLMEAddr&0xff);
if ( AF_DataRequest( &SampleApp_P2P_DstAddr,
&SampleApp_epDesc,
SAMPLEAPP_P3P1_CLUSTERID,
4,
(byte * )&NLME_addr,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
}
else
{
// Error occurred in request to send.
}
}
}
4)Zigbee终端节点的/接收消息事件 处理函数是用来读取,Zigbee协调器发来“数据请求”,当Zigbee终端节点收到“数据请求”时,就会返回自己采集到的SHT10温湿度值,我这里命名为函数SampleApp_Send_P2P_Message();
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
switch ( pkt->clusterId )
{
case SAMPLEAPP_P2P_CLUSTERID:
{
//初始化D2
P1SEL&=0Xfd;//1111 1101
P1DIR|=0x02; //P11定义为输出0000 0010
P1_1 =0;
SampleApp_Send_P2P_Message();
}
break;
case SAMPLEAPP_PERIODIC_CLUSTERID:
break;
}
}
5)Zigbee终端节点的数据发送函数的代码
void SampleApp_Send_P2P_Message( void )
{
int temp;
uint16 huim;
//uint8 dataLen;
huim = MeasureHuim();
temp = MeasureTemp();
if(AddrID<10)
{
Data[0] = 0+ '0';
Data[1] = AddrID + '0';
Data[2] = ' ';
}
else if(AddrID>=10)
{
Data[0] =1 + '0';
Data[1] = (AddrID%10) + '0';
Data[2] = ' ';
}
Data[3] = huim/1000 + '0';
Data[4] = huim%1000/100 + '0';
Data[5] = '.';
Data[6] = huim%100/10 + '0';
Data[7] = huim%10 + '0';
Data[8] = ' ';
if(temp >= 0)
{
Data[9] = temp/1000 + '0';
Data[10] = temp%1000/100 + '0';
Data[11] = '.';
Data[12] = temp%100/10 + '0';
Data[13] = temp%10 + '0';
Data[14] = ' ';
}
//HalUARTWrite(0,Data,dataLen );
if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
//SAMPLEAPP_P2P1_CLUSTERID,
//SAMPLEAPP_P2P2_CLUSTERID,
//SAMPLEAPP_P2P3_CLUSTERID,
//SAMPLEAPP_P2P4_CLUSTERID,
//SAMPLEAPP_P2P5_CLUSTERID,
SAMPLEAPP_P2P6_CLUSTERID,
//SAMPLEAPP_P2P7_CLUSTERID,
//SAMPLEAPP_P2P8_CLUSTERID,
//SAMPLEAPP_P2P9_CLUSTERID,
//SAMPLEAPP_P2Pa_CLUSTERID,//10
//SAMPLEAPP_P2Pb_CLUSTERID,//11
//SAMPLEAPP_P2Pc_CLUSTERID,//12
//SAMPLEAPP_P2Pd_CLUSTERID,//13
//SAMPLEAPP_P2Pe_CLUSTERID,//14
//SAMPLEAPP_P2Pf_CLUSTERID,//15
//SAMPLEAPP_P2Pg_CLUSTERID,//16
//SAMPLEAPP_P2Ph_CLUSTERID,//17
//SAMPLEAPP_P2Pi_CLUSTERID,//18
15,
(byte * )&Data,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
//初始化D1
P1SEL&=0Xfe;//1111 1110
P1DIR|=0x01;//P10定义为输出0000 0001
P1_0 = 0;
}
else
{
// Error occurred in request to send.
}
}
这些代码只是SampleApp.c文件的主要代码,其余比较重要的OSAL,ZMAIN,后期有时间再整理吧~
6)使用串口调试助手看看效果~
发送01返回,终端节点1到终端节点6的温度和湿度,不是按顺序输出的:
发送02返回,终端节点7到终端节点12的温度和湿度:
发送03返回,终端节点13到终端节点18的温度和湿度:
7)后面用的python实现的串口读写程序,然后对之前代码,做了些小小的改动温度加上了正负,因为天气比较冷,到了0下了,波特率还是115200,这里我就只用Zigbee终端节点两个节点测试一下~