找回密码
 注册
查看: 508|回复: 2
打印 上一主题 下一主题

环境CC2530检测系统(ZigBee)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-10-21 13:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
ZigBee选用CC2530射频模块,温湿度、紫外线、空气质量三种传感器各使用一个CC2530模块进行数据采集和传输,
要能实现与手机APP通讯,还需要设计ZigBee到蓝牙或者WiFi网络的转换网关,选用WiFi通讯方式。为了节省成本,
把三个传感器节点的其中一个设计为兼容网关功能,另外两个节点的数据都汇聚到网关ZigBee处,ZigBee将数据发送到
WiFi模块,最后通过WiFi信号上传到APP,显示各传感器数据。LCD液晶显示屏也设计到网关节点处,方便显示各种传感器数据。
主要硬件:
CC2530射频模块
温湿度、紫外线、空气质量三种传感器
LCD液晶屏
WiFi模块

温湿度节点原理图:


空气质量节点原理图:


紫外线+网关节点原理图:


PM2.5节点源码:

  • /*********************************************************************
  • * INCLUDES
  • */
  • #include <stdio.h>
  • #include <string.h>
  • #include "AF.h"
  • #include "OnBoard.h"
  • #include "OSAL_Tasks.h"
  • #include "SampleApp.h"
  • #include "ZDApp.h"
  • #include "ZDObject.h"
  • #include "ZDProfile.h"
  • #include "hal_drivers.h"
  • #include "hal_key.h"
  • #if defined ( LCD_SUPPORTED )
  •   #include "hal_lcd.h"
  • #endif
  • #include "hal_led.h"
  • #include "hal_uart.h"
  • #include "hal_adc.h"
  • #include "MT_UART.h"
  • #include "MT_APP.h"
  • #include "MT.h"
  • #include "pm2.5.h"
  • #if !defined( SERIAL_APP_TX_MAX )
  • #define SERIAL_APP_TX_MAX  80
  • #endif
  • /*********************************************************************
  • * MACROS
  • */
  • /*********************************************************************
  • * CONSTANTS
  • */
  • #if !defined( SAMPLE_APP_PORT )
  • #define SAMPLE_APP_PORT  0
  • #endif
  • #if !defined( SAMPLE_APP_BAUD )
  •   #define SAMPLE_APP_BAUD  HAL_UART_BR_115200
  • #endif
  • // When the Rx buf space is less than this threshold, invoke the Rx callback.
  • #if !defined( SAMPLE_APP_THRESH )
  • #define SAMPLE_APP_THRESH  64
  • #endif
  • #if !defined( SAMPLE_APP_RX_SZ )
  • #define SAMPLE_APP_RX_SZ  128
  • #endif
  • #if !defined( SAMPLE_APP_TX_SZ )
  • #define SAMPLE_APP_TX_SZ  128
  • #endif
  • // Millisecs of idle time after a byte is received before invoking Rx callback.
  • #if !defined( SAMPLE_APP_IDLE )
  • #define SAMPLE_APP_IDLE  6
  • #endif
  • // Loopback Rx bytes to Tx for throughput testing.
  • #if !defined( SAMPLE_APP_LOOPBACK )
  • #define SAMPLE_APP_LOOPBACK  FALSE
  • #endif
  • // This is the max byte count per OTA message.
  • #if !defined( SAMPLE_APP_TX_MAX )
  • #define SAMPLE_APP_TX_MAX  80
  • #endif
  • #define SAMPLE_APP_RSP_CNT  4
  • // This list should be filled with Application specific Cluster IDs.
  • const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
  • {
  •   SAMPLEAPP_P2P_CLUSTERID,
  •   SAMPLEAPP_PERIODIC_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;
  •   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  •   (cId_t *)SampleApp_ClusterList,  //  byte *pAppInClusterList;
  •   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumOutClusters;
  •   (cId_t *)SampleApp_ClusterList   //  byte *pAppOutClusterList;
  • };
  • endPointDesc_t SampleApp_epDesc =
  • {
  •   SAMPLEAPP_ENDPOINT,
  • &SampleApp_TaskID,
  •   (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
  •   noLatencyReqs
  • };
  • /*********************************************************************
  • * TYPEDEFS
  • */
  • /*********************************************************************
  • * GLOBAL VARIABLES
  • */
  • devStates_t SampleApp_NwkState;   
  • uint8 SampleApp_TaskID;           // Task ID for internal task/event processing.
  • /*********************************************************************
  • * EXTERNAL VARIABLES
  • */
  • /*********************************************************************
  • * EXTERNAL FUNCTIONS
  • */
  • /*********************************************************************
  • * LOCAL VARIABLES
  • */
  • uint16 PM25_TH=500;
  • uint8 buff[20]={0};
  • static uint8 SampleApp_MsgID;
  • afAddrType_t SampleApp_Periodic_DstAddr; //广播
  • afAddrType_t SampleApp_Flash_DstAddr;    //组播
  • afAddrType_t SampleApp_P2P_DstAddr;      //点播
  • static afAddrType_t SampleApp_TxAddr;
  • static uint8 SampleApp_TxSeq;
  • static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
  • uint8 SerialApp_TxBuf[SERIAL_APP_TX_MAX+1];
  • uint8 SerialApp_TxLen;
  • static uint8 SampleApp_TxLen;
  • static afAddrType_t SampleApp_RxAddr;
  • static uint8 SampleApp_RxSeq;
  • static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];
  • /*********************************************************************
  • * LOCAL FUNCTIONS
  • */
  • static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  • void SampleApp_CallBack(uint8 port, uint8 event);
  • static void SampleApp_Send_P2P_Message( void );
  • static void packDataAndSend(uint8 fc, uint8* data, uint8 len);
  • /*********************************************************************
  • * @fn      SampleApp_Init
  • *
  • * @brief   This is called during OSAL tasks' initialization.
  • *
  • * @param   task_id - the Task ID assigned by OSAL.
  • *
  • * @return  none
  • */
  • void SampleApp_Init( uint8 task_id )
  • {
  •   halUARTCfg_t uartConfig;
  •   SampleApp_TaskID = task_id;
  •   SampleApp_RxSeq = 0xC3;
  •   SampleApp_NwkState = DEV_INIT;      
  •   MT_UartInit();                  //串口初始化
  •   MT_UartRegisterTaskID(task_id); //注册串口任务
  •   afRegister( (endPointDesc_t *)&SampleApp_epDesc );
  •   RegisteRForKeys( task_id );
  • #ifdef ZDO_COORDINATOR
  •   //协调器初始化
  •   //逢蜂鸣器初始化
  •   P0SEL &= ~0x80;                 //设置P07为普通IO口
  •   P0DIR |= 0x80;                 //P07定义为输出口
  •   //默认蜂鸣器不响
  •   P0_7=1;  
  • #endif
  •   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;
  •   SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播
  •   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  •   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器
  • }
  • /*********************************************************************
  • * @fn      SampleApp_ProcessEvent
  • *
  • * @brief   Generic Application Task event processor.
  • *
  • * @param   task_id  - The OSAL assigned task ID.
  • * @param   events   - Bit map of events to process.
  • *
  • * @return  Event flags of all unprocessed events.
  • */
  • UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
  • {
  •   (void)task_id;  // Intentionally unreferenced parameter
  •   if ( events & SYS_EVENT_MSG )
  •   {
  •     afIncomingMSGPacket_t *MSGpkt;
  •     while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
  •     {
  •       switch ( MSGpkt->hdr.event )
  •       {
  •       case AF_INCOMING_MSG_CMD:
  •         SampleApp_ProcessMSGCmd( 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))
  •         {
  •             osal_start_timerEx( SampleApp_TaskID,
  •                               SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  •                               SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );//采集PM2.5
  •         }
  •         break;
  •       default:
  •         break;
  •       }
  •       osal_msg_deallocate( (uint8 *)MSGpkt );
  •     }
  •     return ( events ^ SYS_EVENT_MSG );
  •   }
  •   //定时器时间到
  •   if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  •   {
  •     // PM2.5采集
  •     SampleApp_Send_P2P_Message();
  •     // Setup to send message again in normal period (+ a little jitter)
  •     osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  •         2000);
  •     // return unprocessed events
  •     return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  •   }
  •   return ( 0 );  // Discard unknown events.
  • }
  • /*********************************************************************
  • * @fn      SerialApp_ProcessMSGCmd
  • *
  • * @brief   Data message processor callback. This function processes
  • *          any incoming data - probably from other devices. Based
  • *          on the cluster ID, perform the intended action.
  • *
  • * @param   pkt - pointer to the incoming message packet
  • *
  • * @return  TRUE if the 'pkt' parameter is being used and will be freed later,
  • *          FALSE otherwise.
  • */
  • void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  • {
  •   switch ( pkt->clusterId )
  •   {
  •     // 接收终端上传的温度数据
  •     case SAMPLEAPP_P2P_CLUSTERID:
  •     break;
  •     case 2://报警阈值信息下发的簇
  •       break;
  •     default:
  •       break;
  •   }
  • }
  • /*********************************************************************
  • * @fn      SampleApp_CallBack
  • *
  • * @brief   Send data OTA.
  • *
  • * @param   port - UART port.
  • * @param   event - the UART port event flag.
  • *
  • * @return  none
  • */
  • void SampleApp_CallBack(uint8 port, uint8 event)
  • {
  •   (void)port;
  •   if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  • #if SAMPLE_APP_LOOPBACK
  •       (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
  • #else
  •       !SampleApp_TxLen)
  • #endif
  •   {
  •     SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
  •                                                       SAMPLE_APP_TX_MAX-SampleApp_TxLen);
  •   }
  • }
  • /*********************************************************************
  • * @fn      SampleApp_Send_P2P_Message
  • *
  • * @brief   point to point.
  • *
  • * @param   none
  • *
  • * @return  none
  • */
  • void SampleApp_Send_P2P_Message( void )
  • {
  •   uint16 Pm25;
  •   uint8 str[10]={0};
  •   int len=0;
  •   Pm25=GetPm25();     //取PM25数据
  •   str[0] = 3;         //增加一个ID,如果是多个终端就增加这个值
  •   str[1] = LO_UINT16( Pm25 );
  •   str[2] = HI_UINT16( Pm25 );
  •   len=3;
  •   P1SEL &= ~0x13;                //设置0、1、4为普通IO口
  •   P1DIR |= 0x13;                 //0、1、4定义为输出口
  •   /******阈值报警处理*****/
  •   if( Pm25>PM25_TH )
  •   {
  •     P1_0=0;//板子上LED灯D1亮表示粉尘浓度报警
  •   }
  •   else
  •   {
  •     P1_0=1;
  •   }
  •   //无线发送到协调器
  •   if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
  •                        SAMPLEAPP_P2P_CLUSTERID,
  •                        len,
  •                        str,
  •                        &SampleApp_MsgID,
  •                        AF_DISCV_ROUTE,
  •                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  •   {
  •   }
  •   else
  •   {
  •     // Error occurred in request to send.
  •   }
  • }
  • uint8 CheckSum(uint8 *pdata, uint8 len)
  • {
  •         uint8 i;
  •         uint8 check_sum=0;
  •         for(i=0; i<len; i++)
  •         {
  •                 check_sum += pdata;
  •         }
  •         return check_sum;
  • }
  • //数据打包发送
  • /**
  • *fc:功能码
  • *data:上传的数据
  • *len:数据长度
  • 格式:len,校验,fc,内容,$,@,
  • */
  • void packDataAndSend(uint8 fc, uint8* data, uint8 len)
  • {
  •     osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);
  •     //数据包长度
  •     SampleApp_TxBuf[0]=3+len;
  •     //功能码
  •     SampleApp_TxBuf[2]=fc;
  •     //发送的数据
  •     if(len>0)
  •     {
  •         osal_mEMCpy(SampleApp_TxBuf+3, data, len);
  •     }
  •     //校验和,从fc开始,
  •     SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);
  •     //数据结尾
  •     SampleApp_TxBuf[3+len]='
  •     SampleApp_TxBuf[4+len]='@';
  •     SampleApp_TxBuf[5+len]='\r';
  •     SampleApp_TxBuf[6+len]='\n';
  •     //发送长度
  •     SampleApp_TxLen=7+len;
  •     //接着发数据包
  •     HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
  • }
  • 温湿度节点源码:
  • /*********************************************************************
  • * INCLUDES
  • */
  • #include <stdio.h>
  • #include <string.h>
  • #include "AF.h"
  • #include "OnBoard.h"
  • #include "OSAL_Tasks.h"
  • #include "SampleApp.h"
  • #include "ZDApp.h"
  • #include "ZDObject.h"
  • #include "ZDProfile.h"
  • #include "hal_drivers.h"
  • #include "hal_key.h"
  • #if defined ( LCD_SUPPORTED )
  •   #include "hal_lcd.h"
  • #endif
  • #include "hal_led.h"
  • #include "hal_uart.h"
  • #include "hal_adc.h"
  • #include "dht11.h"
  • /*********************************************************************
  • * MACROS
  • */
  • /*********************************************************************
  • * CONSTANTS
  • */
  • #if !defined( SAMPLE_APP_PORT )
  • #define SAMPLE_APP_PORT  0
  • #endif
  • #if !defined( SAMPLE_APP_BAUD )
  •   #define SAMPLE_APP_BAUD  HAL_UART_BR_115200
  • #endif
  • // When the Rx buf space is less than this threshold, invoke the Rx callback.
  • #if !defined( SAMPLE_APP_THRESH )
  • #define SAMPLE_APP_THRESH  64
  • #endif
  • #if !defined( SAMPLE_APP_RX_SZ )
  • #define SAMPLE_APP_RX_SZ  128
  • #endif
  • #if !defined( SAMPLE_APP_TX_SZ )
  • #define SAMPLE_APP_TX_SZ  128
  • #endif
  • // Millisecs of idle time after a byte is received before invoking Rx callback.
  • #if !defined( SAMPLE_APP_IDLE )
  • #define SAMPLE_APP_IDLE  6
  • #endif
  • // Loopback Rx bytes to Tx for throughput testing.
  • #if !defined( SAMPLE_APP_LOOPBACK )
  • #define SAMPLE_APP_LOOPBACK  FALSE
  • #endif
  • // This is the max byte count per OTA message.
  • #if !defined( SAMPLE_APP_TX_MAX )
  • #define SAMPLE_APP_TX_MAX  80
  • #endif
  • #define SAMPLE_APP_RSP_CNT  4
  • // This list should be filled with Application specific Cluster IDs.
  • const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
  • {
  •   SAMPLEAPP_P2P_CLUSTERID,
  •   SAMPLEAPP_PERIODIC_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;
  •   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  •   (cId_t *)SampleApp_ClusterList,  //  byte *pAppInClusterList;
  •   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumOutClusters;
  •   (cId_t *)SampleApp_ClusterList   //  byte *pAppOutClusterList;
  • };
  • endPointDesc_t SampleApp_epDesc =
  • {
  •   SAMPLEAPP_ENDPOINT,
  • &SampleApp_TaskID,
  •   (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
  •   noLatencyReqs
  • };
  • /*********************************************************************
  • * TYPEDEFS
  • */
  • /*********************************************************************
  • * GLOBAL VARIABLES
  • */
  • devStates_t SampleApp_NwkState;   
  • uint8 SampleApp_TaskID;           // Task ID for internal task/event processing.
  • /*********************************************************************
  • * EXTERNAL VARIABLES
  • */
  • /*********************************************************************
  • * EXTERNAL FUNCTIONS
  • */
  • /*********************************************************************
  • * LOCAL VARIABLES
  • */
  • uint8 Temp_Hth=30,Temp_Lth=10;
  • uint8 Humi_Hth=80,Humi_Lth=10;
  • uint8 AO_Hth=70,AO_Lth=10;
  • uint8 buff[20]={0};
  • uint8 Motor_state=0x05,LED_state=1;
  • static uint8 SampleApp_MsgID;
  • afAddrType_t SampleApp_Periodic_DstAddr; //广播
  • afAddrType_t SampleApp_Flash_DstAddr;    //组播
  • afAddrType_t SampleApp_P2P_DstAddr;      //点播
  • static afAddrType_t SampleApp_TxAddr;
  • static uint8 SampleApp_TxSeq;
  • static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
  • static uint8 SampleApp_TxLen;
  • static afAddrType_t SampleApp_RxAddr;
  • static uint8 SampleApp_RxSeq;
  • static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];
  • /*********************************************************************
  • * LOCAL FUNCTIONS
  • */
  • static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  • void SampleApp_CallBack(uint8 port, uint8 event);
  • static void SampleApp_Send_P2P_Message( void );
  • static void packDataAndSend(uint8 fc, uint8* data, uint8 len);
  • /*********************************************************************
  • * @fn      SampleApp_Init
  • *
  • * @brief   This is called during OSAL tasks' initialization.
  • *
  • * @param   task_id - the Task ID assigned by OSAL.
  • *
  • * @return  none
  • */
  • void SampleApp_Init( uint8 task_id )
  • {
  •   halUARTCfg_t uartConfig;
  •   SampleApp_TaskID = task_id;
  •   SampleApp_RxSeq = 0xC3;
  •   SampleApp_NwkState = DEV_INIT;      
  •   MT_UartInit();                  //串口初始化
  •   MT_UartRegisterTaskID(task_id); //注册串口任务
  •   afRegister( (endPointDesc_t *)&SampleApp_epDesc );
  •   RegisterForKeys( task_id );
  • #ifdef ZDO_COORDINATOR
  •   //协调器初始化
  •   //逢蜂鸣器初始化
  •   P0SEL &= ~0x80;                 //设置P07为普通IO口
  •   P0DIR |= 0x80;                 //P07定义为输出口
  •   //默认蜂鸣器不响
  •   P0_7=1;  
  • #endif
  •   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;
  •   SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播
  •   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  •   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器
  • }
  • /*********************************************************************
  • * @fn      SampleApp_ProcessEvent
  • *
  • * @brief   Generic Application Task event processor.
  • *
  • * @param   task_id  - The OSAL assigned task ID.
  • * @param   events   - Bit map of events to process.
  • *
  • * @return  Event flags of all unprocessed events.
  • */
  • UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
  • {
  •   (void)task_id;  // Intentionally unreferenced parameter
  •   if ( events & SYS_EVENT_MSG )
  •   {
  •     afIncomingMSGPacket_t *MSGpkt;
  •     while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
  •     {
  •       switch ( MSGpkt->hdr.event )
  •       {
  •       case AF_INCOMING_MSG_CMD:
  •         SampleApp_ProcessMSGCmd( 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) )
  •         {
  •             //连网成功后,启动一个定时器
  •             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 );
  •     }
  •     return ( events ^ SYS_EVENT_MSG );
  •   }
  •   //定时器时间到
  •   if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  •   {
  •     // DHT11采集
  •     SampleApp_Send_P2P_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);
  •     // return unprocessed events
  •     return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  •   }
  •   return ( 0 );  // Discard unknown events.
  • }
  • /*********************************************************************
  • * @fn      SerialApp_ProcessMSGCmd
  • *
  • * @brief   Data message processor callback. This function processes
  • *          any incoming data - probably from other devices. Based
  • *          on the cluster ID, perform the intended action.
  • *
  • * @param   pkt - pointer to the incoming message packet
  • *
  • * @return  TRUE if the 'pkt' parameter is being used and will be freed later,
  • *          FALSE otherwise.
  • */
  • void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  • {
  •   switch ( pkt->clusterId )
  •   {
  •   // 接收终端上传的温度数据
  •   case SAMPLEAPP_P2P_CLUSTERID:
  • #ifdef ZDO_COORDINATOR
  •     {
  •         uint8 id=pkt->cmd.Data[0];//终端id
  •         uint8 t=pkt->cmd.Data[1]; //终端温度
  •         uint8 h=pkt->cmd.Data[2]; //终端湿度
  •         if(id==1)
  •         {
  •           sprintf(buff, "%d T&H:%d %d", id, t, h);
  •           HalLcdWriteString(buff, HAL_LCD_LINE_2); //LCD显示
  •         }
  •         else if(id==2)
  •         {
  •           sprintf(buff, "%d T&H:%d %d", id, t, h);
  •           HalLcdWriteString(buff, HAL_LCD_LINE_3); //LCD显示
  •         }
  •         else if(id==3)
  •         {
  •           sprintf(buff, "%d T&H:%d %d", id, t, h);
  •           HalLcdWriteString(buff, HAL_LCD_LINE_4); //LCD显示
  •         }
  •         //蜂鸣器报警
  •         if(t>35)//温度高于35度报警
  •         {
  •           P0_7=0;        
  •         }
  •         else if(h<30)//湿度低于30报警
  •         {
  •           P0_7=0;
  •         }
  •         else
  •         {
  •           P0_7=1;//不报警
  •         }
  •       //串口输出可以看得见的字符,就把"#if 0"改为"#if 1"
  •       //默认是输出16进制数据,使用我们写的PC工具或者手机软件查看
  • #if 0   
  •         HalUARTWrite(0, buff, osal_strlen(buff));           //串口输出提示信息
  •         HalUARTWrite(0, "\r\n",2);
  • #else        
  •         //打包无线发送的数据        
  •         buff[0]=id;//终端id
  •         buff[1]=t; //终端温度
  •         buff[2]=h; //终端湿度
  •         //打包数据用于发送到wifi网关,或者串口
  •         packDataAndSend(FUN_CODE_UPDATA_DATA, buff, 3);
  • #endif
  •     }
  • #endif
  •     break;
  •   case 2://报警阈值信息下发的簇
  •           Temp_Hth=pkt->cmd.Data[0];  //温度上限
  •           Humi_Hth=pkt->cmd.Data[1];  //湿度上限
  •           AO_Hth=pkt->cmd.Data[2];    //光照上限
  •           Temp_Lth=pkt->cmd.Data[3];  //温度下限
  •           Humi_Lth=pkt->cmd.Data[4];  //湿度下限
  •           AO_Lth=pkt->cmd.Data[5];    //光照下限
  •       break;
  •     default:
  •       break;
  •   }
  • }
  • /*********************************************************************
  • * @fn      SampleApp_CallBack
  • *
  • * @brief   Send data OTA.
  • *
  • * @param   port - UART port.
  • * @param   event - the UART port event flag.
  • *
  • * @return  none
  • */
  • void SampleApp_CallBack(uint8 port, uint8 event)
  • {
  •   (void)port;
  •   if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  • #if SAMPLE_APP_LOOPBACK
  •       (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
  • #else
  •       !SampleApp_TxLen)
  • #endif
  •   {
  •     SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
  •                                                       SAMPLE_APP_TX_MAX-SampleApp_TxLen);
  •   }
  • }
  • /*********************************************************************
  • * @fn      SampleApp_Send_P2P_Message
  • *
  • * @brief   point to point.
  • *
  • * @param   none
  • *
  • * @return  none
  • */
  • void SampleApp_Send_P2P_Message( void )
  • {
  •   uint8 str[10]={0};
  •   int len=0;
  •   DHT11();             //获取温湿度
  •   str[0] = 1;         //增加一个ID,如果是多个终端就增加这个值
  •   str[1] = wendu;  //温度
  •   str[2] = shidu;  //湿度
  •   len=3;
  •   P1SEL &= ~0x13;                //设置0、1、4为普通IO口
  •   P1DIR |= 0x13;                 //0、1、4定义为输出口
  •   /******阈值报警处理*****/
  •   if((str[1]>=Temp_Lth)&&(str[1]<=Temp_Hth))
  •   {
  •     P1_0=1;
  •   }
  •   else
  •   {
  •     P1_0=0;//板子上LED灯D1亮表示温度报警
  •   }
  •   if((str[2]>=Humi_Lth)&&(str[2]<=Humi_Hth))
  •   {
  •     P1_1=1;
  •   }
  •   else
  •   {
  •     P1_1=0;//板子上LED灯D2亮表示湿度报警
  •   }
  •   //无线发送到协调器
  •   if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
  •                        SAMPLEAPP_P2P_CLUSTERID,
  •                        len,
  •                        str,
  •                        &SampleApp_MsgID,
  •                        AF_DISCV_ROUTE,
  •                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  •   {
  •   }
  •   else
  •   {
  •     // Error occurred in request to send.
  •   }
  • }
  • uint8 CheckSum(uint8 *pdata, uint8 len)
  • {
  •         uint8 i;
  •         uint8 check_sum=0;
  •         for(i=0; i<len; i++)
  •         {
  •                 check_sum += pdata;
  •         }
  •         return check_sum;
  • }
  • //数据打包发送
  • /**
  • *fc:功能码
  • *data:上传的数据
  • *len:数据长度
  • 格式:len,校验,fc,内容,$,@,
  • */
  • void packDataAndSend(uint8 fc, uint8* data, uint8 len)
  • {
  •     osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);
  •     //数据包长度
  •     SampleApp_TxBuf[0]=3+len;
  •     //功能码
  •     SampleApp_TxBuf[2]=fc;
  •     //发送的数据
  •     if(len>0)
  •     {
  •         osal_memcpy(SampleApp_TxBuf+3, data, len);
  •     }
  •     //校验和,从fc开始,
  •     SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);
  •     //数据结尾
  •     SampleApp_TxBuf[3+len]='
  •     SampleApp_TxBuf[4+len]='@';
  •     SampleApp_TxBuf[5+len]='\r';
  •     SampleApp_TxBuf[6+len]='\n';
  •     //发送长度
  •     SampleApp_TxLen=7+len;
  •     //接着发数据包
  •     HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
  • }
  • 紫外线+网关源码:
  • /*********************************************************************
  • * INCLUDES
  • */
  • #include <stdio.h>
  • #include <string.h>
  • #include "AF.h"
  • #include "OnBoard.h"
  • #include "OSAL_Tasks.h"
  • #include "SampleApp.h"
  • #include "ZDApp.h"
  • #include "ZDObject.h"
  • #include "ZDProfile.h"
  • #include "hal_drivers.h"
  • #include "hal_key.h"
  • #include "hal_lcd.h"
  • #include "hal_led.h"
  • #include "hal_uart.h"
  • #include "hal_adc.h"
  • /*********************************************************************
  • * MACROS
  • */
  • /*********************************************************************
  • * CONSTANTS
  • */
  • #if !defined( SAMPLE_APP_PORT )
  • #define SAMPLE_APP_PORT  0
  • #endif
  • #if !defined( SAMPLE_APP_BAUD )
  •   #define SAMPLE_APP_BAUD  HAL_UART_BR_115200
  • #endif
  • // When the Rx buf space is less than this threshold, invoke the Rx callback.
  • #if !defined( SAMPLE_APP_THRESH )
  • #define SAMPLE_APP_THRESH  64
  • #endif
  • #if !defined( SAMPLE_APP_RX_SZ )
  • #define SAMPLE_APP_RX_SZ  128
  • #endif
  • #if !defined( SAMPLE_APP_TX_SZ )
  • #define SAMPLE_APP_TX_SZ  128
  • #endif
  • // Millisecs of idle time after a byte is received before invoking Rx callback.
  • #if !defined( SAMPLE_APP_IDLE )
  • #define SAMPLE_APP_IDLE  6
  • #endif
  • // Loopback Rx bytes to Tx for throughput testing.
  • #if !defined( SAMPLE_APP_LOOPBACK )
  • #define SAMPLE_APP_LOOPBACK  FALSE
  • #endif
  • // This is the max byte count per OTA message.
  • #if !defined( SAMPLE_APP_TX_MAX )
  • #define SAMPLE_APP_TX_MAX  80
  • #endif
  • #define SAMPLE_APP_RSP_CNT  4
  • // This list should be filled with Application specific Cluster IDs.
  • const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
  • {
  •   SAMPLEAPP_P2P_CLUSTERID,
  •   SAMPLEAPP_PERIODIC_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;
  •   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  •   (cId_t *)SampleApp_ClusterList,  //  byte *pAppInClusterList;
  •   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumOutClusters;
  •   (cId_t *)SampleApp_ClusterList   //  byte *pAppOutClusterList;
  • };
  • endPointDesc_t SampleApp_epDesc =
  • {
  •   SAMPLEAPP_ENDPOINT,
  • &SampleApp_TaskID,
  •   (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
  •   noLatencyReqs
  • };
  • /*********************************************************************
  • * TYPEDEFS
  • */
  • /*********************************************************************
  • * GLOBAL VARIABLES
  • */
  • devStates_t SampleApp_NwkState;   
  • uint8 SampleApp_TaskID;           // Task ID for internal task/event processing.
  • /*********************************************************************
  • * EXTERNAL VARIABLES
  • */
  • /*********************************************************************
  • * EXTERNAL FUNCTIONS
  • */
  • /*********************************************************************
  • * LOCAL VARIABLES
  • */
  • uint8 Temp_Hth=30,Temp_Lth=10;
  • uint8 Humi_Hth=80,Humi_Lth=10;
  • uint8 AO_Hth=70,AO_Lth=10;
  • uint8 buff[20]={0};
  • uint8 header3[17]="AT+CIPSEND=0,28";
  • uint8 Temperature=0,Humidity=0,AO=0;
  • uint16 PM25;
  • static uint8 SampleApp_MsgID;
  • afAddrType_t SampleApp_Periodic_DstAddr; //广播
  • afAddrType_t SampleApp_Flash_DstAddr;    //组播
  • afAddrType_t SampleApp_P2P_DstAddr;      //点播
  • static afAddrType_t SampleApp_TxAddr;
  • static uint8 SampleApp_TxSeq;
  • static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
  • static uint8 SampleApp_TxLen;
  • static afAddrType_t SampleApp_RxAddr;
  • static uint8 SampleApp_RxSeq;
  • static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];
  • /*********************************************************************
  • * LOCAL FUNCTIONS
  • */
  • static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  • void SampleApp_CallBack(uint8 port, uint8 event);
  • static void SampleApp_Send_P2P_Message( void );
  • static void packDataAndSend(uint8 fc, uint8* data, uint8 len);
  • //取光照值,接在P06上
  • uint8 GetLight()
  • {
  •     uint8 temp=0;//百分比的整数值
  •     float vol=0.0; //adc采样电压  
  •     uint16 adc= HalAdcRead(HAL_ADC_CHANNEL_6, HAL_ADC_RESOLUTION_14); //ADC 采样值 P06口
  •     //最大采样值8192(因为最高位是符号位)
  •     if(adc>=8192)
  •     {
  •         return 0;
  •     }
  •     //转化为百分比
  •     vol=(float)((float)adc)/8192.0;
  •     //取百分比两位数字
  •     temp=vol*100;
  •     return temp;
  • }
  • /*********************************************************************
  • * @fn      SampleApp_Init
  • *
  • * @brief   This is called during OSAL tasks' initialization.
  • *
  • * @param   task_id - the Task ID assigned by OSAL.
  • *
  • * @return  none
  • */
  • void SampleApp_Init( uint8 task_id )
  • {
  •   halUARTCfg_t uartConfig;
  •   SampleApp_TaskID = task_id;
  •   SampleApp_RxSeq = 0xC3;
  •   SampleApp_NwkState = DEV_INIT;      
  •   MT_UartInit();                  //串口初始化
  •   MT_UartRegisterTaskID(task_id); //注册串口任务
  •   afRegister( (endPointDesc_t *)&SampleApp_epDesc );
  •   RegisterForKeys( task_id );
  • #ifdef ZDO_COORDINATOR
  •   //协调器初始化
  •   //逢蜂鸣器初始化
  •   P0SEL &= ~0x80;                 //设置P07为普通IO口
  •   P0DIR |= 0x80;                 //P07定义为输出口
  •   //默认蜂鸣器不响
  •   P0_7=1;  
  • #endif
  •   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;
  •   SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播
  •   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  •   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器
  • }
  • /*********************************************************************
  • * @fn      SampleApp_ProcessEvent
  • *
  • * @brief   Generic Application Task event processor.
  • *
  • * @param   task_id  - The OSAL assigned task ID.
  • * @param   events   - Bit map of events to process.
  • *
  • * @return  Event flags of all unprocessed events.
  • */
  • UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
  • {
  •   (void)task_id;  // Intentionally unreferenced parameter
  •   if ( events & SYS_EVENT_MSG )
  •   {
  •     afIncomingMSGPacket_t *MSGpkt;
  •     while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
  •     {
  •       switch ( MSGpkt->hdr.event )
  •       {
  •       case AF_INCOMING_MSG_CMD:
  •         SampleApp_ProcessMSGCmd( MSGpkt );
  •         break;
  •       case ZDO_STATE_CHANGE:
  •         SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
  •         if (SampleApp_NwkState == DEV_ZB_COORD)
  •         {
  •             uint8 header1[13]="AT+CIPMUX=1";
  •             header1[11]='\r';header1[12]='\n';
  •             header3[15]='\r';header3[16]='\n';
  •             HalUARTWrite(0,header1,13);
  •             osal_start_timerEx( SampleApp_TaskID,
  •                               SAMPLEAPP_SEND_WIFI_MSG1_EVT,
  •                               2*1000 );
  •             osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  •                               SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
  •         }
  •         break;
  •       default:
  •         break;
  •       }
  •       osal_msg_deallocate( (uint8 *)MSGpkt );
  •     }
  •     return ( events ^ SYS_EVENT_MSG );
  •   }
  •   //定时器时间到
  •   if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  •   {
  •     // 光照采集
  •     SampleApp_Send_P2P_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);
  •     // return unprocessed events
  •     return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  •   }
  •   if ( events & SAMPLEAPP_SEND_WIFI_MSG1_EVT )
  •   {
  •     uint8 header2[21]="AT+CIPSERVER=1,2020";
  •     header2[19]='\r';header2[20]='\n';
  •     HalUARTWrite(0,header2,21);
  •     LCD_Fill(0x00);  //初始清屏
  •     LCD_Set_Pos(0,0);
  •     HalLcdWriteString( "ZigBee环境监测", HAL_LCD_LINE_1 );
  •     return (events ^ SAMPLEAPP_SEND_WIFI_MSG1_EVT);
  •   }
  •   if ( events & SAMPLEAPP_UPDATE_EVT )
  •   {
  •     uint8 str[6]={0};
  •     str[0] = Temp_Hth;
  •     str[1] = Temp_Lth;
  •     str[2] = Humi_Hth;
  •     str[3] = Humi_Lth;
  •     str[4] = AO_Hth;
  •     str[5] = AO_Lth;
  •     if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
  •                        2,
  •                        6,
  •                        str,
  •                        &SampleApp_MsgID,
  •                        AF_DISCV_ROUTE,
  •                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  •   {
  •   }
  •     return (events ^ SAMPLEAPP_UPDATE_EVT);
  •   }
  •   return ( 0 );  // Discard unknown events.
  • }
  • /*********************************************************************
  • * @fn      SerialApp_ProcessMSGCmd
  • *
  • * @brief   Data message processor callback. This function processes
  • *          any incoming data - probably from other devices. Based
  • *          on the cluster ID, perform the intended action.
  • *
  • * @param   pkt - pointer to the incoming message packet
  • *
  • * @return  TRUE if the 'pkt' parameter is being used and will be freed later,
  • *          FALSE otherwise.
  • */
  • void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  • {
  •   uint8 id,PM25_L,PM25_H;
  •   switch ( pkt->clusterId )
  •   {
  •   // 接收终端上传的温度数据
  •   case SAMPLEAPP_P2P_CLUSTERID:
  • #ifdef ZDO_COORDINATOR
  •     {
  •         id=pkt->cmd.Data[0];
  •         if(id == 1)//终端1,温湿度
  •         {
  •             Temperature=pkt->cmd.Data[1];   //终端温度
  •             Humidity=pkt->cmd.Data[2];   //终端湿度
  •             buff[0]=id;
  •             buff[1]=Temperature;
  •             buff[2]=Humidity;
  •         }
  •         else if(id == 3)//终端3,PM25
  •         {     
  •             PM25_L=pkt->cmd.Data[1];
  •             PM25_H=pkt->cmd.Data[2];
  •             PM25=BUILD_UINT16(PM25_L, PM25_H);
  •             buff[0]=id;
  •             buff[1]=PM25_L;
  •             buff[2]=PM25_H;
  •         }
  • #if 0   
  •         HalUARTWrite(0, buff, osal_strlen(buff));           //串口输出提示信息
  •         HalUARTWrite(0, "\r\n",2);
  • #else        
  •         //打包数据用于发送到wifi网关
  •         HalUARTWrite(0,header3,17);
  •         packDataAndSend(FUN_CODE_UPDATA_DATA, buff, 3);
  • #endif
  •     }
  • #endif
  •     break;
  •   case 2://报警阈值信息下发的簇
  •           Temp_Hth=pkt->cmd.Data[0]; //温度上限
  •           Temp_Lth=pkt->cmd.Data[1]; //温度下限
  •           Humi_Hth=pkt->cmd.Data[2]; //湿度上限
  •           Humi_Lth=pkt->cmd.Data[3]; //湿度下限
  •           AO_Hth=pkt->cmd.Data[4]; //光照上限
  •           AO_Lth=pkt->cmd.Data[5]; //光照下限
  •       break;
  •     default:
  •       break;
  •   }
  • }
  • /*********************************************************************
  • * @fn      SampleApp_CallBack
  • *
  • * @brief   Send data OTA.
  • *
  • * @param   port - UART port.
  • * @param   event - the UART port event flag.
  • *
  • * @return  none
  • */
  • void SampleApp_CallBack(uint8 port, uint8 event)
  • {
  •   (void)port;
  •   if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  • #if SAMPLE_APP_LOOPBACK
  •       (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
  • #else
  •       !SampleApp_TxLen)
  • #endif
  •   {
  •     SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
  •                                                       SAMPLE_APP_TX_MAX-SampleApp_TxLen);
  •   }
  • }
  • /*********************************************************************
  • * @fn      SampleApp_Send_P2P_Message
  • *
  • * @brief   point to point.
  • *
  • * @param   none
  • *
  • * @return  none
  • */
  • void SampleApp_Send_P2P_Message( void )
  • {
  •   uint8 str[10]={0};
  •   uint8 strTemp[30]={0};
  •   AO = GetLight();    //光照AO口数据,然后转成百分比0~100,数据越高,光照越强
  •   int len=0;
  •   str[0] = 2;         //增加一个ID,如果是多个终端就增加这个值
  •   str[1] = AO;
  •   str[2] = 0;
  •   len=3;
  •   //LCD显示pm2.5
  •   sprintf(strTemp, "PM2.5:%d ug/m3",PM25);
  •   HalLcdWriteString( strTemp, HAL_LCD_LINE_4 );
  •   //LCD显示温湿度
  •   sprintf(strTemp, "温:%02d 湿:%02d",Temperature,Humidity);
  •   HalLcdWriteString( strTemp, HAL_LCD_LINE_3 );
  •   //LCD显示光照强度
  •   sprintf(strTemp, "光照:%02d",AO);
  •   HalLcdWriteString( strTemp, HAL_LCD_LINE_2 );
  •   /******阈值报警处理*****/
  •   P1SEL &= ~0x13;                //设置0、1、4为普通IO口
  •   P1DIR |= 0x13;                 //0、1、4定义为输出口
  •   if((str[3]>=AO_Lth)&&(str[3]<=AO_Hth))
  •   {
  •     P1_4=1;
  •   }
  •   else
  •   {
  •     P1_4=0;//板子上LED灯D3亮表示光照报警
  •   }
  •   //串口发送到WiFi
  •   HalUARTWrite(0,header3,17);
  •   packDataAndSend(FUN_CODE_UPDATA_DATA, str, len);
  • }
  • uint8 CheckSum(uint8 *pdata, uint8 len)
  • {
  •         uint8 i;
  •         uint8 check_sum=0;
  •         for(i=0; i<len; i++)
  •         {
  •                 check_sum += pdata;
  •         }
  •         return check_sum;
  • }
  • //数据打包发送
  • /**
  • *fc:功能码
  • *data:上传的数据
  • *len:数据长度
  • 格式:len,校验,fc,内容,$,@,
  • */
  • void packDataAndSend(uint8 fc, uint8* data, uint8 len)
  • {
  •     osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);
  •     //数据包长度
  •     SampleApp_TxBuf[0]=3+len;
  •     //功能码
  •     SampleApp_TxBuf[2]=fc;
  •     //发送的数据
  •     if(len>0)
  •     {
  •         osal_memcpy(SampleApp_TxBuf+3, data, len);
  •     }
  •     //校验和,从fc开始,
  •     SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);
  •     //数据结尾
  •     SampleApp_TxBuf[3+len]='
  •     SampleApp_TxBuf[4+len]='@';
  •     SampleApp_TxBuf[5+len]='\r';
  •     SampleApp_TxBuf[6+len]='\n';
  •     //发送长度
  •     SampleApp_TxLen=7+len;
  •     //接着发数据包
  •     HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
  • }


该用户从未签到

2#
发表于 2021-10-21 14:31 | 只看该作者
ZigBee将数据发送到
WiFi模块,最后通过WiFi信号上传到APP,显示各传感器数据

该用户从未签到

3#
发表于 2021-10-21 16:23 | 只看该作者
三个传感器节点的其中一个设计为兼容网关功能,另外两个节点的数据都汇聚到网关ZigBee处
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-5-25 18:45 , Processed in 0.125000 second(s), 24 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表