找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6713|回复: 3
打印 上一主题 下一主题
收起左侧

基于ZigBee的环境CC2530检测系统程序设计(温湿度,紫外线,空气质量,APP)

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

温湿度节点原理图:


空气质量节点原理图:


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


PM2.5节点源码:
  1. /*********************************************************************
  2. * INCLUDES
  3. */

  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "AF.h"
  7. #include "OnBoard.h"
  8. #include "OSAL_Tasks.h"
  9. #include "SampleApp.h"
  10. #include "ZDApp.h"
  11. #include "ZDObject.h"
  12. #include "ZDProfile.h"

  13. #include "hal_drivers.h"
  14. #include "hal_key.h"
  15. #if defined ( LCD_SUPPORTED )
  16.   #include "hal_lcd.h"
  17. #endif
  18. #include "hal_led.h"
  19. #include "hal_uart.h"
  20. #include "hal_adc.h"
  21. #include "MT_UART.h"
  22. #include "MT_APP.h"
  23. #include "MT.h"
  24. #include "pm2.5.h"

  25. #if !defined( SERIAL_APP_TX_MAX )
  26. #define SERIAL_APP_TX_MAX  80
  27. #endif
  28. /*********************************************************************
  29. * MACROS
  30. */

  31. /*********************************************************************
  32. * CONSTANTS
  33. */

  34. #if !defined( SAMPLE_APP_PORT )
  35. #define SAMPLE_APP_PORT  0
  36. #endif

  37. #if !defined( SAMPLE_APP_BAUD )
  38.   #define SAMPLE_APP_BAUD  HAL_UART_BR_115200
  39. #endif

  40. // When the Rx buf space is less than this threshold, invoke the Rx callback.
  41. #if !defined( SAMPLE_APP_THRESH )
  42. #define SAMPLE_APP_THRESH  64
  43. #endif

  44. #if !defined( SAMPLE_APP_RX_SZ )
  45. #define SAMPLE_APP_RX_SZ  128
  46. #endif

  47. #if !defined( SAMPLE_APP_TX_SZ )
  48. #define SAMPLE_APP_TX_SZ  128
  49. #endif

  50. // Millisecs of idle time after a byte is received before invoking Rx callback.
  51. #if !defined( SAMPLE_APP_IDLE )
  52. #define SAMPLE_APP_IDLE  6
  53. #endif

  54. // Loopback Rx bytes to Tx for throughput testing.
  55. #if !defined( SAMPLE_APP_LOOPBACK )
  56. #define SAMPLE_APP_LOOPBACK  FALSE
  57. #endif

  58. // This is the max byte count per OTA message.
  59. #if !defined( SAMPLE_APP_TX_MAX )
  60. #define SAMPLE_APP_TX_MAX  80
  61. #endif

  62. #define SAMPLE_APP_RSP_CNT  4

  63. // This list should be filled with Application specific Cluster IDs.
  64. const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
  65. {
  66.   SAMPLEAPP_P2P_CLUSTERID,
  67.   SAMPLEAPP_PERIODIC_CLUSTERID,
  68. };

  69. const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
  70. {
  71.   SAMPLEAPP_ENDPOINT,              //  int   Endpoint;
  72.   SAMPLEAPP_PROFID,                //  uint16 AppProfId[2];
  73.   SAMPLEAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  74.   SAMPLEAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  75.   SAMPLEAPP_FLAGS,                 //  int   AppFlags:4;
  76.   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  77.   (cId_t *)SampleApp_ClusterList,  //  byte *pAppInClusterList;
  78.   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumOutClusters;
  79.   (cId_t *)SampleApp_ClusterList   //  byte *pAppOutClusterList;
  80. };

  81. endPointDesc_t SampleApp_epDesc =
  82. {
  83.   SAMPLEAPP_ENDPOINT,
  84. &SampleApp_TaskID,
  85.   (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
  86.   noLatencyReqs
  87. };

  88. /*********************************************************************
  89. * TYPEDEFS
  90. */

  91. /*********************************************************************
  92. * GLOBAL VARIABLES
  93. */
  94. devStates_t SampleApp_NwkState;   
  95. uint8 SampleApp_TaskID;           // Task ID for internal task/event processing.

  96. /*********************************************************************
  97. * EXTERNAL VARIABLES
  98. */

  99. /*********************************************************************
  100. * EXTERNAL FUNCTIONS
  101. */

  102. /*********************************************************************
  103. * LOCAL VARIABLES
  104. */
  105. uint16 PM25_TH=500;
  106. uint8 buff[20]={0};

  107. static uint8 SampleApp_MsgID;

  108. afAddrType_t SampleApp_Periodic_DstAddr; //广播
  109. afAddrType_t SampleApp_Flash_DstAddr;    //组播
  110. afAddrType_t SampleApp_P2P_DstAddr;      //点播


  111. static afAddrType_t SampleApp_TxAddr;
  112. static uint8 SampleApp_TxSeq;
  113. static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
  114. uint8 SerialApp_TxBuf[SERIAL_APP_TX_MAX+1];
  115. uint8 SerialApp_TxLen;
  116. static uint8 SampleApp_TxLen;

  117. static afAddrType_t SampleApp_RxAddr;
  118. static uint8 SampleApp_RxSeq;
  119. static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];

  120. /*********************************************************************
  121. * LOCAL FUNCTIONS
  122. */

  123. static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  124. void SampleApp_CallBack(uint8 port, uint8 event);
  125. static void SampleApp_Send_P2P_Message( void );
  126. static void packDataAndSend(uint8 fc, uint8* data, uint8 len);

  127. /*********************************************************************
  128. * @fn      SampleApp_Init
  129. *
  130. * @brief   This is called during OSAL tasks' initialization.
  131. *
  132. * @param   task_id - the Task ID assigned by OSAL.
  133. *
  134. * @return  none
  135. */
  136. void SampleApp_Init( uint8 task_id )
  137. {
  138.   halUARTCfg_t uartConfig;

  139.   SampleApp_TaskID = task_id;
  140.   SampleApp_RxSeq = 0xC3;
  141.   SampleApp_NwkState = DEV_INIT;      

  142.   MT_UartInit();                  //串口初始化
  143.   MT_UartRegisterTaskID(task_id); //注册串口任务
  144.   afRegister( (endPointDesc_t *)&SampleApp_epDesc );
  145.   RegisterForKeys( task_id );

  146. #ifdef ZDO_COORDINATOR
  147.   //协调器初始化

  148.   //逢蜂鸣器初始化

  149.   P0SEL &= ~0x80;                 //设置P07为普通IO口
  150.   P0DIR |= 0x80;                 //P07定义为输出口

  151.   //默认蜂鸣器不响
  152.   P0_7=1;  
  153. #endif

  154.   SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播
  155.   SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  156.   SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;

  157.   // Setup for the flash command's destination address - Group 1
  158.   SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//组播
  159.   SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  160.   SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;

  161.   SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播
  162.   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  163.   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器


  164. }

  165. /*********************************************************************
  166. * @fn      SampleApp_ProcessEvent
  167. *
  168. * @brief   Generic Application Task event processor.
  169. *
  170. * @param   task_id  - The OSAL assigned task ID.
  171. * @param   events   - Bit map of events to process.
  172. *
  173. * @return  Event flags of all unprocessed events.
  174. */
  175. UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
  176. {
  177.   (void)task_id;  // Intentionally unreferenced parameter

  178.   if ( events & SYS_EVENT_MSG )
  179.   {
  180.     afIncomingMSGPacket_t *MSGpkt;

  181.     while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
  182.     {
  183.       switch ( MSGpkt->hdr.event )
  184.       {
  185.       case AF_INCOMING_MSG_CMD:
  186.         SampleApp_ProcessMSGCmd( MSGpkt );
  187.         break;

  188.       case ZDO_STATE_CHANGE:
  189.         SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
  190.         if (//(SampleApp_NwkState == DEV_ZB_COORD)||
  191.             (SampleApp_NwkState == DEV_ROUTER)
  192.             || (SampleApp_NwkState == DEV_END_DEVICE))
  193.         {
  194.             osal_start_timerEx( SampleApp_TaskID,
  195.                               SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  196.                               SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );//采集PM2.5
  197.         }
  198.         break;

  199.       default:
  200.         break;
  201.       }

  202.       osal_msg_deallocate( (uint8 *)MSGpkt );
  203.     }

  204.     return ( events ^ SYS_EVENT_MSG );
  205.   }

  206.   //定时器时间到
  207.   if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  208.   {
  209.     // PM2.5采集
  210.     SampleApp_Send_P2P_Message();

  211.     // Setup to send message again in normal period (+ a little jitter)
  212.     osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  213.         2000);

  214.     // return unprocessed events
  215.     return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  216.   }
  217.   return ( 0 );  // Discard unknown events.
  218. }

  219. /*********************************************************************
  220. * @fn      SerialApp_ProcessMSGCmd
  221. *
  222. * @brief   Data message processor callback. This function processes
  223. *          any incoming data - probably from other devices. Based
  224. *          on the cluster ID, perform the intended action.
  225. *
  226. * @param   pkt - pointer to the incoming message packet
  227. *
  228. * @return  TRUE if the 'pkt' parameter is being used and will be freed later,
  229. *          FALSE otherwise.
  230. */
  231. void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  232. {
  233.   switch ( pkt->clusterId )
  234.   {
  235.     // 接收终端上传的温度数据
  236.     case SAMPLEAPP_P2P_CLUSTERID:
  237.     break;

  238.     case 2://报警阈值信息下发的簇

  239.       break;

  240.     default:
  241.       break;
  242.   }
  243. }


  244. /*********************************************************************
  245. * @fn      SampleApp_CallBack
  246. *
  247. * @brief   Send data OTA.
  248. *
  249. * @param   port - UART port.
  250. * @param   event - the UART port event flag.
  251. *
  252. * @return  none
  253. */
  254. void SampleApp_CallBack(uint8 port, uint8 event)
  255. {
  256.   (void)port;

  257.   if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  258. #if SAMPLE_APP_LOOPBACK
  259.       (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
  260. #else
  261.       !SampleApp_TxLen)
  262. #endif
  263.   {
  264.     SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
  265.                                                       SAMPLE_APP_TX_MAX-SampleApp_TxLen);
  266.   }
  267. }

  268. /*********************************************************************
  269. * @fn      SampleApp_Send_P2P_Message
  270. *
  271. * @brief   point to point.
  272. *
  273. * @param   none
  274. *
  275. * @return  none
  276. */
  277. void SampleApp_Send_P2P_Message( void )
  278. {
  279.   uint16 Pm25;
  280.   uint8 str[10]={0};
  281.   int len=0;

  282.   Pm25=GetPm25();     //取PM25数据

  283.   str[0] = 3;         //增加一个ID,如果是多个终端就增加这个值
  284.   str[1] = LO_UINT16( Pm25 );
  285.   str[2] = HI_UINT16( Pm25 );

  286.   len=3;

  287.   P1SEL &= ~0x13;                //设置0、1、4为普通IO口
  288.   P1DIR |= 0x13;                 //0、1、4定义为输出口
  289.   /******阈值报警处理*****/
  290.   if( Pm25>PM25_TH )
  291.   {
  292.     P1_0=0;//板子上LED灯D1亮表示粉尘浓度报警
  293.   }
  294.   else
  295.   {
  296.     P1_0=1;
  297.   }

  298.   //无线发送到协调器
  299.   if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
  300.                        SAMPLEAPP_P2P_CLUSTERID,
  301.                        len,
  302.                        str,
  303.                        &SampleApp_MsgID,
  304.                        AF_DISCV_ROUTE,
  305.                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  306.   {
  307.   }
  308.   else
  309.   {
  310.     // Error occurred in request to send.
  311.   }
  312. }

  313. uint8 CheckSum(uint8 *pdata, uint8 len)
  314. {
  315.         uint8 i;
  316.         uint8 check_sum=0;

  317.         for(i=0; i<len; i++)
  318.         {
  319.                 check_sum += pdata[i];
  320.         }
  321.         return check_sum;
  322. }

  323. //数据打包发送
  324. /**
  325. *fc:功能码
  326. *data:上传的数据
  327. *len:数据长度
  328. 格式:len,校验,fc,内容,$,@,
  329. */
  330. void packDataAndSend(uint8 fc, uint8* data, uint8 len)
  331. {
  332.     osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);


  333.     //数据包长度
  334.     SampleApp_TxBuf[0]=3+len;

  335.     //功能码
  336.     SampleApp_TxBuf[2]=fc;

  337.     //发送的数据
  338.     if(len>0)
  339.     {
  340.         osal_memcpy(SampleApp_TxBuf+3, data, len);
  341.     }

  342.     //校验和,从fc开始,
  343.     SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);

  344.     //数据结尾
  345.     SampleApp_TxBuf[3+len]='

  346.     SampleApp_TxBuf[4+len]='@';

  347.     SampleApp_TxBuf[5+len]='\r';
  348.     SampleApp_TxBuf[6+len]='\n';
  349.     //发送长度
  350.     SampleApp_TxLen=7+len;

  351.     //接着发数据包
  352.     HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
  353. }
  354. 温湿度节点源码:
  355. /*********************************************************************
  356. * INCLUDES
  357. */

  358. #include <stdio.h>
  359. #include <string.h>
  360. #include "AF.h"
  361. #include "OnBoard.h"
  362. #include "OSAL_Tasks.h"
  363. #include "SampleApp.h"
  364. #include "ZDApp.h"
  365. #include "ZDObject.h"
  366. #include "ZDProfile.h"

  367. #include "hal_drivers.h"
  368. #include "hal_key.h"
  369. #if defined ( LCD_SUPPORTED )
  370.   #include "hal_lcd.h"
  371. #endif
  372. #include "hal_led.h"
  373. #include "hal_uart.h"
  374. #include "hal_adc.h"
  375. #include "dht11.h"

  376. /*********************************************************************
  377. * MACROS
  378. */

  379. /*********************************************************************
  380. * CONSTANTS
  381. */

  382. #if !defined( SAMPLE_APP_PORT )
  383. #define SAMPLE_APP_PORT  0
  384. #endif

  385. #if !defined( SAMPLE_APP_BAUD )
  386.   #define SAMPLE_APP_BAUD  HAL_UART_BR_115200
  387. #endif

  388. // When the Rx buf space is less than this threshold, invoke the Rx callback.
  389. #if !defined( SAMPLE_APP_THRESH )
  390. #define SAMPLE_APP_THRESH  64
  391. #endif

  392. #if !defined( SAMPLE_APP_RX_SZ )
  393. #define SAMPLE_APP_RX_SZ  128
  394. #endif

  395. #if !defined( SAMPLE_APP_TX_SZ )
  396. #define SAMPLE_APP_TX_SZ  128
  397. #endif

  398. // Millisecs of idle time after a byte is received before invoking Rx callback.
  399. #if !defined( SAMPLE_APP_IDLE )
  400. #define SAMPLE_APP_IDLE  6
  401. #endif

  402. // Loopback Rx bytes to Tx for throughput testing.
  403. #if !defined( SAMPLE_APP_LOOPBACK )
  404. #define SAMPLE_APP_LOOPBACK  FALSE
  405. #endif

  406. // This is the max byte count per OTA message.
  407. #if !defined( SAMPLE_APP_TX_MAX )
  408. #define SAMPLE_APP_TX_MAX  80
  409. #endif

  410. #define SAMPLE_APP_RSP_CNT  4

  411. // This list should be filled with Application specific Cluster IDs.
  412. const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
  413. {
  414.   SAMPLEAPP_P2P_CLUSTERID,
  415.   SAMPLEAPP_PERIODIC_CLUSTERID,
  416. };

  417. const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
  418. {
  419.   SAMPLEAPP_ENDPOINT,              //  int   Endpoint;
  420.   SAMPLEAPP_PROFID,                //  uint16 AppProfId[2];
  421.   SAMPLEAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  422.   SAMPLEAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  423.   SAMPLEAPP_FLAGS,                 //  int   AppFlags:4;
  424.   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  425.   (cId_t *)SampleApp_ClusterList,  //  byte *pAppInClusterList;
  426.   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumOutClusters;
  427.   (cId_t *)SampleApp_ClusterList   //  byte *pAppOutClusterList;
  428. };

  429. endPointDesc_t SampleApp_epDesc =
  430. {
  431.   SAMPLEAPP_ENDPOINT,
  432. &SampleApp_TaskID,
  433.   (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
  434.   noLatencyReqs
  435. };

  436. /*********************************************************************
  437. * TYPEDEFS
  438. */

  439. /*********************************************************************
  440. * GLOBAL VARIABLES
  441. */
  442. devStates_t SampleApp_NwkState;   
  443. uint8 SampleApp_TaskID;           // Task ID for internal task/event processing.

  444. /*********************************************************************
  445. * EXTERNAL VARIABLES
  446. */

  447. /*********************************************************************
  448. * EXTERNAL FUNCTIONS
  449. */

  450. /*********************************************************************
  451. * LOCAL VARIABLES
  452. */
  453. uint8 Temp_Hth=30,Temp_Lth=10;
  454. uint8 Humi_Hth=80,Humi_Lth=10;
  455. uint8 AO_Hth=70,AO_Lth=10;
  456. uint8 buff[20]={0};
  457. uint8 Motor_state=0x05,LED_state=1;

  458. static uint8 SampleApp_MsgID;

  459. afAddrType_t SampleApp_Periodic_DstAddr; //广播
  460. afAddrType_t SampleApp_Flash_DstAddr;    //组播
  461. afAddrType_t SampleApp_P2P_DstAddr;      //点播


  462. static afAddrType_t SampleApp_TxAddr;
  463. static uint8 SampleApp_TxSeq;
  464. static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
  465. static uint8 SampleApp_TxLen;

  466. static afAddrType_t SampleApp_RxAddr;
  467. static uint8 SampleApp_RxSeq;
  468. static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];

  469. /*********************************************************************
  470. * LOCAL FUNCTIONS
  471. */

  472. static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  473. void SampleApp_CallBack(uint8 port, uint8 event);
  474. static void SampleApp_Send_P2P_Message( void );
  475. static void packDataAndSend(uint8 fc, uint8* data, uint8 len);

  476. /*********************************************************************
  477. * @fn      SampleApp_Init
  478. *
  479. * @brief   This is called during OSAL tasks' initialization.
  480. *
  481. * @param   task_id - the Task ID assigned by OSAL.
  482. *
  483. * @return  none
  484. */
  485. void SampleApp_Init( uint8 task_id )
  486. {
  487.   halUARTCfg_t uartConfig;

  488.   SampleApp_TaskID = task_id;
  489.   SampleApp_RxSeq = 0xC3;
  490.   SampleApp_NwkState = DEV_INIT;      

  491.   MT_UartInit();                  //串口初始化
  492.   MT_UartRegisterTaskID(task_id); //注册串口任务
  493.   afRegister( (endPointDesc_t *)&SampleApp_epDesc );
  494.   RegisterForKeys( task_id );

  495. #ifdef ZDO_COORDINATOR
  496.   //协调器初始化

  497.   //逢蜂鸣器初始化

  498.   P0SEL &= ~0x80;                 //设置P07为普通IO口
  499.   P0DIR |= 0x80;                 //P07定义为输出口

  500.   //默认蜂鸣器不响
  501.   P0_7=1;  
  502. #endif

  503.   SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播
  504.   SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  505.   SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;

  506.   // Setup for the flash command's destination address - Group 1
  507.   SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//组播
  508.   SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  509.   SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;

  510.   SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播
  511.   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  512.   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器


  513. }

  514. /*********************************************************************
  515. * @fn      SampleApp_ProcessEvent
  516. *
  517. * @brief   Generic Application Task event processor.
  518. *
  519. * @param   task_id  - The OSAL assigned task ID.
  520. * @param   events   - Bit map of events to process.
  521. *
  522. * @return  Event flags of all unprocessed events.
  523. */
  524. UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
  525. {
  526.   (void)task_id;  // Intentionally unreferenced parameter

  527.   if ( events & SYS_EVENT_MSG )
  528.   {
  529.     afIncomingMSGPacket_t *MSGpkt;

  530.     while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
  531.     {
  532.       switch ( MSGpkt->hdr.event )
  533.       {
  534.       case AF_INCOMING_MSG_CMD:
  535.         SampleApp_ProcessMSGCmd( MSGpkt );
  536.         break;

  537.       case ZDO_STATE_CHANGE:
  538.         SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
  539.         if ( //(SampleApp_NwkState == DEV_ZB_COORD)||
  540.             (SampleApp_NwkState == DEV_ROUTER)
  541.             || (SampleApp_NwkState == DEV_END_DEVICE) )
  542.         {
  543.             //连网成功后,启动一个定时器
  544.             osal_start_timerEx( SampleApp_TaskID,
  545.                               SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  546.                               SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
  547.         }
  548.         else
  549.         {
  550.           // Device is no longer in the network
  551.         }
  552.         break;

  553.       default:
  554.         break;
  555.       }

  556.       osal_msg_deallocate( (uint8 *)MSGpkt );
  557.     }

  558.     return ( events ^ SYS_EVENT_MSG );
  559.   }

  560.   //定时器时间到
  561.   if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  562.   {
  563.     // DHT11采集
  564.     SampleApp_Send_P2P_Message();

  565.     // Setup to send message again in normal period (+ a little jitter)
  566.     osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  567.         SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);

  568.     // return unprocessed events
  569.     return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  570.   }


  571.   return ( 0 );  // Discard unknown events.
  572. }

  573. /*********************************************************************
  574. * @fn      SerialApp_ProcessMSGCmd
  575. *
  576. * @brief   Data message processor callback. This function processes
  577. *          any incoming data - probably from other devices. Based
  578. *          on the cluster ID, perform the intended action.
  579. *
  580. * @param   pkt - pointer to the incoming message packet
  581. *
  582. * @return  TRUE if the 'pkt' parameter is being used and will be freed later,
  583. *          FALSE otherwise.
  584. */
  585. void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  586. {
  587.   switch ( pkt->clusterId )
  588.   {
  589.   // 接收终端上传的温度数据
  590.   case SAMPLEAPP_P2P_CLUSTERID:
  591. #ifdef ZDO_COORDINATOR
  592.     {
  593.         uint8 id=pkt->cmd.Data[0];//终端id
  594.         uint8 t=pkt->cmd.Data[1]; //终端温度
  595.         uint8 h=pkt->cmd.Data[2]; //终端湿度

  596.         if(id==1)
  597.         {
  598.           sprintf(buff, "%d T&H:%d %d", id, t, h);
  599.           HalLcdWriteString(buff, HAL_LCD_LINE_2); //LCD显示
  600.         }
  601.         else if(id==2)
  602.         {
  603.           sprintf(buff, "%d T&H:%d %d", id, t, h);
  604.           HalLcdWriteString(buff, HAL_LCD_LINE_3); //LCD显示
  605.         }
  606.         else if(id==3)
  607.         {
  608.           sprintf(buff, "%d T&H:%d %d", id, t, h);
  609.           HalLcdWriteString(buff, HAL_LCD_LINE_4); //LCD显示
  610.         }

  611.         //蜂鸣器报警
  612.         if(t>35)//温度高于35度报警
  613.         {
  614.           P0_7=0;        
  615.         }
  616.         else if(h<30)//湿度低于30报警
  617.         {
  618.           P0_7=0;
  619.         }
  620.         else
  621.         {
  622.           P0_7=1;//不报警
  623.         }

  624.       //串口输出可以看得见的字符,就把"#if 0"改为"#if 1"
  625.       //默认是输出16进制数据,使用我们写的PC工具或者手机软件查看
  626. #if 0   
  627.         HalUARTWrite(0, buff, osal_strlen(buff));           //串口输出提示信息
  628.         HalUARTWrite(0, "\r\n",2);
  629. #else        
  630.         //打包无线发送的数据        
  631.         buff[0]=id;//终端id
  632.         buff[1]=t; //终端温度
  633.         buff[2]=h; //终端湿度

  634.         //打包数据用于发送到wifi网关,或者串口
  635.         packDataAndSend(FUN_CODE_UPDATA_DATA, buff, 3);

  636. #endif
  637.     }
  638. #endif
  639.     break;

  640.   case 2://报警阈值信息下发的簇
  641.           Temp_Hth=pkt->cmd.Data[0];  //温度上限
  642.           Humi_Hth=pkt->cmd.Data[1];  //湿度上限
  643.           AO_Hth=pkt->cmd.Data[2];    //光照上限
  644.           Temp_Lth=pkt->cmd.Data[3];  //温度下限
  645.           Humi_Lth=pkt->cmd.Data[4];  //湿度下限
  646.           AO_Lth=pkt->cmd.Data[5];    //光照下限
  647.       break;

  648.     default:
  649.       break;
  650.   }
  651. }


  652. /*********************************************************************
  653. * @fn      SampleApp_CallBack
  654. *
  655. * @brief   Send data OTA.
  656. *
  657. * @param   port - UART port.
  658. * @param   event - the UART port event flag.
  659. *
  660. * @return  none
  661. */
  662. void SampleApp_CallBack(uint8 port, uint8 event)
  663. {
  664.   (void)port;

  665.   if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  666. #if SAMPLE_APP_LOOPBACK
  667.       (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
  668. #else
  669.       !SampleApp_TxLen)
  670. #endif
  671.   {
  672.     SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
  673.                                                       SAMPLE_APP_TX_MAX-SampleApp_TxLen);
  674.   }
  675. }

  676. /*********************************************************************
  677. * @fn      SampleApp_Send_P2P_Message
  678. *
  679. * @brief   point to point.
  680. *
  681. * @param   none
  682. *
  683. * @return  none
  684. */
  685. void SampleApp_Send_P2P_Message( void )
  686. {
  687.   uint8 str[10]={0};
  688.   int len=0;

  689.   DHT11();             //获取温湿度

  690.   str[0] = 1;         //增加一个ID,如果是多个终端就增加这个值
  691.   str[1] = wendu;  //温度
  692.   str[2] = shidu;  //湿度

  693.   len=3;

  694.   P1SEL &= ~0x13;                //设置0、1、4为普通IO口
  695.   P1DIR |= 0x13;                 //0、1、4定义为输出口
  696.   /******阈值报警处理*****/
  697.   if((str[1]>=Temp_Lth)&&(str[1]<=Temp_Hth))
  698.   {
  699.     P1_0=1;
  700.   }
  701.   else
  702.   {
  703.     P1_0=0;//板子上LED灯D1亮表示温度报警
  704.   }

  705.   if((str[2]>=Humi_Lth)&&(str[2]<=Humi_Hth))
  706.   {
  707.     P1_1=1;
  708.   }
  709.   else
  710.   {
  711.     P1_1=0;//板子上LED灯D2亮表示湿度报警
  712.   }

  713.   //无线发送到协调器
  714.   if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
  715.                        SAMPLEAPP_P2P_CLUSTERID,
  716.                        len,
  717.                        str,
  718.                        &SampleApp_MsgID,
  719.                        AF_DISCV_ROUTE,
  720.                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  721.   {
  722.   }
  723.   else
  724.   {
  725.     // Error occurred in request to send.
  726.   }
  727. }

  728. uint8 CheckSum(uint8 *pdata, uint8 len)
  729. {
  730.         uint8 i;
  731.         uint8 check_sum=0;

  732.         for(i=0; i<len; i++)
  733.         {
  734.                 check_sum += pdata[i];
  735.         }
  736.         return check_sum;
  737. }

  738. //数据打包发送
  739. /**
  740. *fc:功能码
  741. *data:上传的数据
  742. *len:数据长度
  743. 格式:len,校验,fc,内容,$,@,
  744. */
  745. void packDataAndSend(uint8 fc, uint8* data, uint8 len)
  746. {
  747.     osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);


  748.     //数据包长度
  749.     SampleApp_TxBuf[0]=3+len;

  750.     //功能码
  751.     SampleApp_TxBuf[2]=fc;

  752.     //发送的数据
  753.     if(len>0)
  754.     {
  755.         osal_memcpy(SampleApp_TxBuf+3, data, len);
  756.     }

  757.     //校验和,从fc开始,
  758.     SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);

  759.     //数据结尾
  760.     SampleApp_TxBuf[3+len]='

  761.     SampleApp_TxBuf[4+len]='@';

  762.     SampleApp_TxBuf[5+len]='\r';
  763.     SampleApp_TxBuf[6+len]='\n';
  764.     //发送长度
  765.     SampleApp_TxLen=7+len;

  766.     //接着发数据包
  767.     HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
  768. }
  769. 紫外线+网关源码:
  770. /*********************************************************************
  771. * INCLUDES
  772. */

  773. #include <stdio.h>
  774. #include <string.h>
  775. #include "AF.h"
  776. #include "OnBoard.h"
  777. #include "OSAL_Tasks.h"
  778. #include "SampleApp.h"
  779. #include "ZDApp.h"
  780. #include "ZDObject.h"
  781. #include "ZDProfile.h"

  782. #include "hal_drivers.h"
  783. #include "hal_key.h"
  784. #include "hal_lcd.h"
  785. #include "hal_led.h"
  786. #include "hal_uart.h"
  787. #include "hal_adc.h"

  788. /*********************************************************************
  789. * MACROS
  790. */

  791. /*********************************************************************
  792. * CONSTANTS
  793. */

  794. #if !defined( SAMPLE_APP_PORT )
  795. #define SAMPLE_APP_PORT  0
  796. #endif

  797. #if !defined( SAMPLE_APP_BAUD )
  798.   #define SAMPLE_APP_BAUD  HAL_UART_BR_115200
  799. #endif

  800. // When the Rx buf space is less than this threshold, invoke the Rx callback.
  801. #if !defined( SAMPLE_APP_THRESH )
  802. #define SAMPLE_APP_THRESH  64
  803. #endif

  804. #if !defined( SAMPLE_APP_RX_SZ )
  805. #define SAMPLE_APP_RX_SZ  128
  806. #endif

  807. #if !defined( SAMPLE_APP_TX_SZ )
  808. #define SAMPLE_APP_TX_SZ  128
  809. #endif

  810. // Millisecs of idle time after a byte is received before invoking Rx callback.
  811. #if !defined( SAMPLE_APP_IDLE )
  812. #define SAMPLE_APP_IDLE  6
  813. #endif

  814. // Loopback Rx bytes to Tx for throughput testing.
  815. #if !defined( SAMPLE_APP_LOOPBACK )
  816. #define SAMPLE_APP_LOOPBACK  FALSE
  817. #endif

  818. // This is the max byte count per OTA message.
  819. #if !defined( SAMPLE_APP_TX_MAX )
  820. #define SAMPLE_APP_TX_MAX  80
  821. #endif

  822. #define SAMPLE_APP_RSP_CNT  4

  823. // This list should be filled with Application specific Cluster IDs.
  824. const cId_t SampleApp_ClusterList[SAMPLE_MAX_CLUSTERS] =
  825. {
  826.   SAMPLEAPP_P2P_CLUSTERID,
  827.   SAMPLEAPP_PERIODIC_CLUSTERID,
  828. };

  829. const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
  830. {
  831.   SAMPLEAPP_ENDPOINT,              //  int   Endpoint;
  832.   SAMPLEAPP_PROFID,                //  uint16 AppProfId[2];
  833.   SAMPLEAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  834.   SAMPLEAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  835.   SAMPLEAPP_FLAGS,                 //  int   AppFlags:4;
  836.   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  837.   (cId_t *)SampleApp_ClusterList,  //  byte *pAppInClusterList;
  838.   SAMPLE_MAX_CLUSTERS,          //  byte  AppNumOutClusters;
  839.   (cId_t *)SampleApp_ClusterList   //  byte *pAppOutClusterList;
  840. };

  841. endPointDesc_t SampleApp_epDesc =
  842. {
  843.   SAMPLEAPP_ENDPOINT,
  844. &SampleApp_TaskID,
  845.   (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc,
  846.   noLatencyReqs
  847. };

  848. /*********************************************************************
  849. * TYPEDEFS
  850. */

  851. /*********************************************************************
  852. * GLOBAL VARIABLES
  853. */
  854. devStates_t SampleApp_NwkState;   
  855. uint8 SampleApp_TaskID;           // Task ID for internal task/event processing.

  856. /*********************************************************************
  857. * EXTERNAL VARIABLES
  858. */

  859. /*********************************************************************
  860. * EXTERNAL FUNCTIONS
  861. */

  862. /*********************************************************************
  863. * LOCAL VARIABLES
  864. */
  865. uint8 Temp_Hth=30,Temp_Lth=10;
  866. uint8 Humi_Hth=80,Humi_Lth=10;
  867. uint8 AO_Hth=70,AO_Lth=10;
  868. uint8 buff[20]={0};
  869. uint8 header3[17]="AT+CIPSEND=0,28";
  870. uint8 Temperature=0,Humidity=0,AO=0;
  871. uint16 PM25;

  872. static uint8 SampleApp_MsgID;

  873. afAddrType_t SampleApp_Periodic_DstAddr; //广播
  874. afAddrType_t SampleApp_Flash_DstAddr;    //组播
  875. afAddrType_t SampleApp_P2P_DstAddr;      //点播


  876. static afAddrType_t SampleApp_TxAddr;
  877. static uint8 SampleApp_TxSeq;
  878. static uint8 SampleApp_TxBuf[SAMPLE_APP_TX_MAX+1];
  879. static uint8 SampleApp_TxLen;

  880. static afAddrType_t SampleApp_RxAddr;
  881. static uint8 SampleApp_RxSeq;
  882. static uint8 SampleApp_RspBuf[SAMPLE_APP_RSP_CNT];

  883. /*********************************************************************
  884. * LOCAL FUNCTIONS
  885. */

  886. static void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  887. void SampleApp_CallBack(uint8 port, uint8 event);
  888. static void SampleApp_Send_P2P_Message( void );
  889. static void packDataAndSend(uint8 fc, uint8* data, uint8 len);

  890. //取光照值,接在P06上
  891. uint8 GetLight()
  892. {
  893.     uint8 temp=0;//百分比的整数值
  894.     float vol=0.0; //adc采样电压  
  895.     uint16 adc= HalAdcRead(HAL_ADC_CHANNEL_6, HAL_ADC_RESOLUTION_14); //ADC 采样值 P06口

  896.     //最大采样值8192(因为最高位是符号位)
  897.     if(adc>=8192)
  898.     {
  899.         return 0;
  900.     }

  901.     //转化为百分比
  902.     vol=(float)((float)adc)/8192.0;

  903.     //取百分比两位数字
  904.     temp=vol*100;

  905.     return temp;
  906. }
  907. /*********************************************************************
  908. * @fn      SampleApp_Init
  909. *
  910. * @brief   This is called during OSAL tasks' initialization.
  911. *
  912. * @param   task_id - the Task ID assigned by OSAL.
  913. *
  914. * @return  none
  915. */
  916. void SampleApp_Init( uint8 task_id )
  917. {
  918.   halUARTCfg_t uartConfig;

  919.   SampleApp_TaskID = task_id;
  920.   SampleApp_RxSeq = 0xC3;
  921.   SampleApp_NwkState = DEV_INIT;      

  922.   MT_UartInit();                  //串口初始化
  923.   MT_UartRegisterTaskID(task_id); //注册串口任务
  924.   afRegister( (endPointDesc_t *)&SampleApp_epDesc );
  925.   RegisterForKeys( task_id );

  926. #ifdef ZDO_COORDINATOR
  927.   //协调器初始化

  928.   //逢蜂鸣器初始化

  929.   P0SEL &= ~0x80;                 //设置P07为普通IO口
  930.   P0DIR |= 0x80;                 //P07定义为输出口

  931.   //默认蜂鸣器不响
  932.   P0_7=1;  
  933. #endif

  934.   SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播
  935.   SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  936.   SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;

  937.   // Setup for the flash command's destination address - Group 1
  938.   SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//组播
  939.   SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  940.   SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;

  941.   SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播
  942.   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  943.   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器


  944. }

  945. /*********************************************************************
  946. * @fn      SampleApp_ProcessEvent
  947. *
  948. * @brief   Generic Application Task event processor.
  949. *
  950. * @param   task_id  - The OSAL assigned task ID.
  951. * @param   events   - Bit map of events to process.
  952. *
  953. * @return  Event flags of all unprocessed events.
  954. */
  955. UINT16 SampleApp_ProcessEvent( uint8 task_id, UINT16 events )
  956. {
  957.   (void)task_id;  // Intentionally unreferenced parameter

  958.   if ( events & SYS_EVENT_MSG )
  959.   {
  960.     afIncomingMSGPacket_t *MSGpkt;

  961.     while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID )) )
  962.     {
  963.       switch ( MSGpkt->hdr.event )
  964.       {
  965.       case AF_INCOMING_MSG_CMD:
  966.         SampleApp_ProcessMSGCmd( MSGpkt );
  967.         break;

  968.       case ZDO_STATE_CHANGE:
  969.         SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
  970.         if (SampleApp_NwkState == DEV_ZB_COORD)
  971.         {
  972.             uint8 header1[13]="AT+CIPMUX=1";
  973.             header1[11]='\r';header1[12]='\n';
  974.             header3[15]='\r';header3[16]='\n';
  975.             HalUARTWrite(0,header1,13);
  976.             osal_start_timerEx( SampleApp_TaskID,
  977.                               SAMPLEAPP_SEND_WIFI_MSG1_EVT,
  978.                               2*1000 );
  979.             osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  980.                               SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
  981.         }
  982.         break;

  983.       default:
  984.         break;
  985.       }

  986.       osal_msg_deallocate( (uint8 *)MSGpkt );
  987.     }

  988.     return ( events ^ SYS_EVENT_MSG );
  989.   }

  990.   //定时器时间到
  991.   if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  992.   {
  993.     // 光照采集
  994.     SampleApp_Send_P2P_Message();

  995.     // Setup to send message again in normal period (+ a little jitter)
  996.     osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  997.         SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);

  998.     // return unprocessed events
  999.     return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  1000.   }

  1001.   if ( events & SAMPLEAPP_SEND_WIFI_MSG1_EVT )
  1002.   {
  1003.     uint8 header2[21]="AT+CIPSERVER=1,2020";
  1004.     header2[19]='\r';header2[20]='\n';
  1005.     HalUARTWrite(0,header2,21);

  1006.     LCD_Fill(0x00);  //初始清屏
  1007.     LCD_Set_Pos(0,0);
  1008.     HalLcdWriteString( "ZigBee环境监测", HAL_LCD_LINE_1 );
  1009.     return (events ^ SAMPLEAPP_SEND_WIFI_MSG1_EVT);
  1010.   }

  1011.   if ( events & SAMPLEAPP_UPDATE_EVT )
  1012.   {
  1013.     uint8 str[6]={0};
  1014.     str[0] = Temp_Hth;
  1015.     str[1] = Temp_Lth;
  1016.     str[2] = Humi_Hth;
  1017.     str[3] = Humi_Lth;
  1018.     str[4] = AO_Hth;
  1019.     str[5] = AO_Lth;
  1020.     if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
  1021.                        2,
  1022.                        6,
  1023.                        str,
  1024.                        &SampleApp_MsgID,
  1025.                        AF_DISCV_ROUTE,
  1026.                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  1027.   {
  1028.   }
  1029.     return (events ^ SAMPLEAPP_UPDATE_EVT);
  1030.   }
  1031.   return ( 0 );  // Discard unknown events.
  1032. }

  1033. /*********************************************************************
  1034. * @fn      SerialApp_ProcessMSGCmd
  1035. *
  1036. * @brief   Data message processor callback. This function processes
  1037. *          any incoming data - probably from other devices. Based
  1038. *          on the cluster ID, perform the intended action.
  1039. *
  1040. * @param   pkt - pointer to the incoming message packet
  1041. *
  1042. * @return  TRUE if the 'pkt' parameter is being used and will be freed later,
  1043. *          FALSE otherwise.
  1044. */
  1045. void SampleApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  1046. {
  1047.   uint8 id,PM25_L,PM25_H;

  1048.   switch ( pkt->clusterId )
  1049.   {
  1050.   // 接收终端上传的温度数据
  1051.   case SAMPLEAPP_P2P_CLUSTERID:
  1052. #ifdef ZDO_COORDINATOR
  1053.     {
  1054.         id=pkt->cmd.Data[0];
  1055.         if(id == 1)//终端1,温湿度
  1056.         {
  1057.             Temperature=pkt->cmd.Data[1];   //终端温度
  1058.             Humidity=pkt->cmd.Data[2];   //终端湿度

  1059.             buff[0]=id;
  1060.             buff[1]=Temperature;
  1061.             buff[2]=Humidity;
  1062.         }
  1063.         else if(id == 3)//终端3,PM25
  1064.         {     
  1065.             PM25_L=pkt->cmd.Data[1];
  1066.             PM25_H=pkt->cmd.Data[2];
  1067.             PM25=BUILD_UINT16(PM25_L, PM25_H);

  1068.             buff[0]=id;
  1069.             buff[1]=PM25_L;
  1070.             buff[2]=PM25_H;
  1071.         }

  1072. #if 0   
  1073.         HalUARTWrite(0, buff, osal_strlen(buff));           //串口输出提示信息
  1074.         HalUARTWrite(0, "\r\n",2);
  1075. #else        
  1076.         //打包数据用于发送到wifi网关
  1077.         HalUARTWrite(0,header3,17);
  1078.         packDataAndSend(FUN_CODE_UPDATA_DATA, buff, 3);

  1079. #endif
  1080.     }
  1081. #endif
  1082.     break;

  1083.   case 2://报警阈值信息下发的簇
  1084.           Temp_Hth=pkt->cmd.Data[0]; //温度上限
  1085.           Temp_Lth=pkt->cmd.Data[1]; //温度下限
  1086.           Humi_Hth=pkt->cmd.Data[2]; //湿度上限
  1087.           Humi_Lth=pkt->cmd.Data[3]; //湿度下限
  1088.           AO_Hth=pkt->cmd.Data[4]; //光照上限
  1089.           AO_Lth=pkt->cmd.Data[5]; //光照下限
  1090.       break;

  1091.     default:
  1092.       break;
  1093.   }
  1094. }


  1095. /*********************************************************************
  1096. * @fn      SampleApp_CallBack
  1097. *
  1098. * @brief   Send data OTA.
  1099. *
  1100. * @param   port - UART port.
  1101. * @param   event - the UART port event flag.
  1102. *
  1103. * @return  none
  1104. */
  1105. void SampleApp_CallBack(uint8 port, uint8 event)
  1106. {
  1107.   (void)port;

  1108.   if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  1109. #if SAMPLE_APP_LOOPBACK
  1110.       (SampleApp_TxLen < SAMPLE_APP_TX_MAX))
  1111. #else
  1112.       !SampleApp_TxLen)
  1113. #endif
  1114.   {
  1115.     SampleApp_TxLen += HalUARTRead(SAMPLE_APP_PORT, SampleApp_TxBuf+SampleApp_TxLen+1,
  1116.                                                       SAMPLE_APP_TX_MAX-SampleApp_TxLen);
  1117.   }
  1118. }

  1119. /*********************************************************************
  1120. * @fn      SampleApp_Send_P2P_Message
  1121. *
  1122. * @brief   point to point.
  1123. *
  1124. * @param   none
  1125. *
  1126. * @return  none
  1127. */
  1128. void SampleApp_Send_P2P_Message( void )
  1129. {
  1130.   uint8 str[10]={0};
  1131.   uint8 strTemp[30]={0};
  1132.   AO = GetLight();    //光照AO口数据,然后转成百分比0~100,数据越高,光照越强
  1133.   int len=0;

  1134.   str[0] = 2;         //增加一个ID,如果是多个终端就增加这个值
  1135.   str[1] = AO;
  1136.   str[2] = 0;

  1137.   len=3;

  1138.   //LCD显示pm2.5
  1139.   sprintf(strTemp, "PM2.5:%d ug/m3",PM25);
  1140.   HalLcdWriteString( strTemp, HAL_LCD_LINE_4 );
  1141.   //LCD显示温湿度
  1142.   sprintf(strTemp, "温:%02d 湿:%02d",Temperature,Humidity);
  1143.   HalLcdWriteString( strTemp, HAL_LCD_LINE_3 );
  1144.   //LCD显示光照强度
  1145.   sprintf(strTemp, "光照:%02d",AO);
  1146.   HalLcdWriteString( strTemp, HAL_LCD_LINE_2 );

  1147.   /******阈值报警处理*****/
  1148.   P1SEL &= ~0x13;                //设置0、1、4为普通IO口
  1149.   P1DIR |= 0x13;                 //0、1、4定义为输出口
  1150.   if((str[3]>=AO_Lth)&&(str[3]<=AO_Hth))
  1151.   {
  1152.     P1_4=1;
  1153.   }
  1154.   else
  1155.   {
  1156.     P1_4=0;//板子上LED灯D3亮表示光照报警
  1157.   }
  1158.   //串口发送到WiFi
  1159.   HalUARTWrite(0,header3,17);
  1160.   packDataAndSend(FUN_CODE_UPDATA_DATA, str, len);
  1161. }

  1162. uint8 CheckSum(uint8 *pdata, uint8 len)
  1163. {
  1164.         uint8 i;
  1165.         uint8 check_sum=0;

  1166.         for(i=0; i<len; i++)
  1167.         {
  1168.                 check_sum += pdata[i];
  1169.         }
  1170.         return check_sum;
  1171. }

  1172. //数据打包发送
  1173. /**
  1174. *fc:功能码
  1175. *data:上传的数据
  1176. *len:数据长度
  1177. 格式:len,校验,fc,内容,$,@,
  1178. */
  1179. void packDataAndSend(uint8 fc, uint8* data, uint8 len)
  1180. {
  1181.     osal_memset(SampleApp_TxBuf, 0, SAMPLE_APP_TX_MAX+1);


  1182.     //数据包长度
  1183.     SampleApp_TxBuf[0]=3+len;

  1184.     //功能码
  1185.     SampleApp_TxBuf[2]=fc;

  1186.     //发送的数据
  1187.     if(len>0)
  1188.     {
  1189.         osal_memcpy(SampleApp_TxBuf+3, data, len);
  1190.     }

  1191.     //校验和,从fc开始,
  1192.     SampleApp_TxBuf[1]=CheckSum(SampleApp_TxBuf+2, len+1);

  1193.     //数据结尾
  1194.     SampleApp_TxBuf[3+len]='

  1195.     SampleApp_TxBuf[4+len]='@';

  1196.     SampleApp_TxBuf[5+len]='\r';
  1197.     SampleApp_TxBuf[6+len]='\n';
  1198.     //发送长度
  1199.     SampleApp_TxLen=7+len;

  1200.     //接着发数据包
  1201.     HalUARTWrite(0,SampleApp_TxBuf, SampleApp_TxLen);
  1202. }
复制代码


全部资料51hei下载地址:

硬件原理图和模块资料.7z

8.2 MB, 下载次数: 164, 下载积分: 黑币 -5

android.zip

1.5 MB, 下载次数: 79, 下载积分: 黑币 -5

Sunlight_Coord.7z

8.49 MB, 下载次数: 77, 下载积分: 黑币 -5

PM25.7z

8.5 MB, 下载次数: 71, 下载积分: 黑币 -5

Temp Humi.7z

8.45 MB, 下载次数: 79, 下载积分: 黑币 -5

评分

参与人数 2黑币 +115 收起 理由
REwilliam + 15
admin + 100 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏21 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:903268 发表于 2021-4-10 01:15 | 只看该作者
感谢楼主分享的资料,很有用
回复

使用道具 举报

板凳
ID:274804 发表于 2021-12-25 23:34 | 只看该作者
你好,我想问一下,你协调器烧写代码是任意一个工程里面的协调器代码嘛
回复

使用道具 举报

地板
ID:618330 发表于 2023-2-22 15:54 | 只看该作者
十分感谢老哥的资料!学习到了! 请问android 这个应该用什么软件打开呢?

-------------------------------------------------------------------------------------------
已经找到解决办法啦!多谢大佬!!
回复

使用道具 举报

5#
ID:84600 发表于 2023-4-4 14:59 | 只看该作者
努力学习中,感谢楼主分享。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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