| 
 | 
7#
 
 
 楼主 |
发表于 2016-8-15 13:58:14
|
只看该作者
 
 
 
【Gokit2&ZigBee&STM32】之无线工业粉尘监测仪
 本帖最后由 kiasur 于 2016-8-15 14:15 编辑  
- /*ZigBee用户函数代码*/
 
 - #include "OSAL.h"
 
 - #include "ZGlobals.h"
 
 - #include "AF.h"
 
 - #include "aps_groups.h"
 
 - #include "ZDApp.h"
 
  
- #include "SampleApp.h"
 
 - #include "SampleAppHw.h"
 
  
- #include "OnBoard.h"
 
  
- /* HAL */
 
 - #include "hal_lcd.h"
 
 - #include "hal_led.h"
 
 - #include "hal_key.h"
 
 - #include "MT_UART.h"
 
 - #include "MT.h"//消息命令ID定义头文件
 
 - #include "string.h"
 
 - #include "hal_voice.h"
 
  
- const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
 
 - {
 
 -   SAMPLEAPP_PERIODIC_CLUSTERID,//广播簇ID
 
 -   SAMPLEAPP_FLASH_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;
 
 -   SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;
 
 -   (cId_t *)SampleApp_ClusterList,  //  uint8 *pAppInClusterList;
 
 -   SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;
 
 -   (cId_t *)SampleApp_ClusterList   //  uint8 *pAppInClusterList;
 
 - };
 
  
- endPointDesc_t SampleApp_epDesc;
 
  
 
- uint8 SampleApp_TaskID;   // Task ID for internal task/event processing
 
 - // This variable will be received when
 
 - // SampleApp_Init() is called.
 
 - devStates_t SampleApp_NwkState;
 
  
- uint8 SampleApp_TransID;  // This is the unique message ID (counter)
 
  
- afAddrType_t SampleApp_Periodic_DstAddr;//广播afAddrType_t类型数据
 
 - afAddrType_t SampleApp_Flash_DstAddr;
 
 - afAddrType_t Point_To_Point_DstAddr;
 
  
- aps_Group_t SampleApp_Group;
 
  
- uint8 SampleAppPeriodicCounter = 0;
 
 - uint8 SampleAppFlashCounter = 0;
 
  
- /**********************************************
 
 - * LOCAL FUNCTIONS
 
 - */
 
 - void SampleApp_HandleKeys( uint8 shift, uint8 keys );
 
 - void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
 
 - void SampleApp_SendPointToPointMessage( uint8 *dat ) ;//点对点传播
 
 - void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg);//数据接收处理
 
 - /**********************************************
 
 - * NETWORK LAYER CALLBACKS
 
 - */
 
  
- /**********************************************
 
 - * PUBLIC FUNCTIONS
 
 - */
 
  
- /**********************************************
 
 - * @fn      SampleApp_Init
 
 - *
 
 - * @brief   Initialization function for the Generic App Task.
 
 - *          This is called during initialization and should contain
 
 - *          any application specific initialization (ie. hardware
 
 - *          initialization/setup, table initialization, power up
 
 - *          notificaiton ... ).
 
 - *
 
 - * @param   task_id - the ID assigned by OSAL.  This ID should be
 
 - *                    used to send messages and set timers.
 
 - *
 
 - * @return  none
 
 - */
 
 - void SampleApp_Init( uint8 task_id )
 
 - {
 
 -   SampleApp_TaskID = task_id;
 
 -   SampleApp_NwkState = DEV_INIT;
 
 -   SampleApp_TransID = 0;
 
 -   
 
 -   /********串口初始化************/
 
 -   MT_UartInit();
 
 -   MT_UartRegisterTaskID(SampleApp_TaskID);
 
 -   HalUARTWrite(0,"uart init success\n" ,18);  
 
 -   /**************************/
 
 -   
 
 -   //  uint8 vol[11]={0xfd,0x00,0x08,0x01,0x01,0x5b,0x76,0x30,0x31,0x5d,0x84};
 
 -   //  HalUARTWrite(1,vol,11);//减小音量
 
 -   
 
 -   
 
 - #if defined ( BUILD_ALL_DEVICES )
 
 -   // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
 
 -   // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
 
 -   // together - if they are - we will start up a coordinator. Otherwise,
 
 -   // the device will start as a router.
 
 -   if ( readCoordinatorJumper() )
 
 -     zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
 
 -   else
 
 -     zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
 
 - #endif // BUILD_ALL_DEVICES
 
 -   
 
 - #if defined ( HOLD_AUTO_START )
 
 -   // HOLD_AUTO_START is a compile option that will surpress ZDApp
 
 -   //  from starting the device and wait for the application to
 
 -   //  start the device.
 
 -   ZDOInitDevice(0);
 
 - #endif
 
 -   
 
 -   
 
 -   //Setup for the Point To Point destination address 
 
 -   Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;//点对点短地址
 
 -   Point_To_Point_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
 
 -   Point_To_Point_DstAddr.addr.shortAddr = 0x0000; //协调器地址
 
 -   
 
 -   // Fill out the endpoint description.
 
 -   SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
 
 -   SampleApp_epDesc.task_id = &SampleApp_TaskID;
 
 -   SampleApp_epDesc.simpleDesc
 
 -     = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
 
 -   SampleApp_epDesc.latencyReq = noLatencyReqs;
 
 -   
 
 -   // Register the endpoint description with the AF
 
 -   afRegister( &SampleApp_epDesc );
 
 -   
 
  
-   uint8 test[]={"欢迎使用无线工业粉尘监测仪"},len=0;
 
 -   len = sizeof(test);
 
 -   voice_input(test,len);
 
 - }
 
  
- /**********************************************
 
 - * @fn      SampleApp_ProcessEvent
 
 - *
 
 - * @brief   Generic Application Task event processor.  This function
 
 - *          is called to process all events for the task.  Events
 
 - *          include timers, messages and any other user defined events.
 
 - *
 
 - * @param   task_id  - The OSAL assigned task ID.
 
 - * @param   events - events to process.  This is a bit map and can
 
 - *                   contain more than one event.
 
 - *
 
 - * @return  none
 
 - */
 
 - uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
 
 - {
 
 -   afIncomingMSGPacket_t *MSGpkt;
 
 -   (void)task_id;  // Intentionally unreferenced parameter
 
 -   
 
 -   if ( events & SYS_EVENT_MSG )
 
 -   {
 
 -     MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
 
 -     while ( MSGpkt )
 
 -     {
 
 -       switch ( MSGpkt->hdr.event )
 
 -       {
 
 -       case CMD_SERIAL_MSG: //串口收到数据后由 MT_UART 层传递过来的
 
 -         SampleApp_SerialCMD((mtOSALSerialData_t *)MSGpkt);
 
 -         break;
 
  
-       case AF_INCOMING_MSG_CMD:
 
 -         SampleApp_MessageMSGCB( MSGpkt );
 
 -         break;
 
 -         
 
 -         // Received whenever the device changes state in the network
 
 -       case ZDO_STATE_CHANGE:
 
 -         SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
 
 -         break;
 
 -         
 
 -       default:
 
 -         break;
 
 -       }
 
 -       
 
 -       // Release the memory
 
 -       osal_msg_deallocate( (uint8 *)MSGpkt );
 
 -       
 
 -       // Next - if one is available
 
 -       MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
 
 -     }
 
 -     
 
 -     // return unprocessed events
 
 -     return (events ^ SYS_EVENT_MSG);
 
 -   }
 
 -   
 
 -   return 0;
 
 - }
 
  
 
- /*对接收到的串口数据进行处理*/
 
 - void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg)
 
 - {
 
 -   uint8 *str=NULL; //len 有用数据长度
 
 -   str=cmdMsg->msg; //指向数据开头
 
 -   if( (*(str+1) ==0x42) && (*(str+2) ==0x4D) )
 
 -     SampleApp_SendPointToPointMessage(str);//将数据通过广播发送
 
 - }
 
  
- void SampleApp_HandleKeys( uint8 shift, uint8 keys )
 
 - {
 
 -   
 
 - }
 
  
 
- /*协调器对接收到的数据进行处理*/
 
 - void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
 
 - {
 
 -   uint8 PM[10],WS[10];
 
 -   switch ( pkt->clusterId )
 
 -   {
 
 -   case SAMPLEAPP_POINT_TO_POINT_CLUSTERID:
 
 -     if(pkt->cmd.Data[1] == 0X42 && pkt->cmd.Data[2] == 0X4d)//判断粉尘数据是否符合协议
 
 -     {
 
 -       /*协调器接收到终端节点的数据后对数据进行剪切*/
 
 -       uint16 PM1_0,PM2_5,PM10;
 
 -       PM[0] = pkt->cmd.Data[1];
 
 -       PM[1] = pkt->cmd.Data[2];
 
 -       PM[2] = pkt->cmd.Data[5];
 
 -       PM[3] = pkt->cmd.Data[6];
 
 -       PM[4] = pkt->cmd.Data[7];
 
 -       PM[5] = pkt->cmd.Data[8];
 
 -       PM[6] = pkt->cmd.Data[9];
 
 -       PM[7] = pkt->cmd.Data[10];
 
 -       PM[8] = 0xdd;
 
 -       PM[9] = 0xff;
 
 -       
 
 -       PM1_0 = (PM[2]*256) + PM[3];
 
 -       PM2_5 = (PM[4]*256) + PM[5];
 
 -       PM10 =  (PM[6]*256) + PM[7];
 
 -       if( (PM1_0 < PM2_5 < PM10) && (PM1_0<2000) &&(PM2_5<2000) && (PM10<2000))//判断粉尘浓度是否超过设定阈值
 
 -       {
 
 -         HalUARTWrite(0,PM,10);
 
 -         
 
 -         if(( PM1_0>1000) | (PM2_5>1000)| (PM10>1000) )
 
 -         {
 
 -           uint8 test[]={"警告警告粉尘浓度超标"},len=0;//发送报警信息到语音合成模块
 
 -           len = sizeof(test);
 
 -           voice_input(test,len);
 
 -         }
 
 -       }
 
 -       
 
 -     }
 
 -     
 
 -     if(pkt->cmd.Data[1] == 0xaa && pkt->cmd.Data[2] == 0xbb)//判断温湿度数据是否符合协议
 
 -     {
 
 -       WS[0] = pkt->cmd.Data[1];
 
 -       WS[1] = pkt->cmd.Data[2];
 
 -       WS[2] = pkt->cmd.Data[3];
 
 -       WS[3] = pkt->cmd.Data[4];
 
 -       WS[4] = pkt->cmd.Data[5];
 
 -       WS[5] = pkt->cmd.Data[6];
 
 -       WS[6] = pkt->cmd.Data[7];
 
 -       WS[7] = pkt->cmd.Data[8];
 
 -       WS[8] = 0xdd;
 
 -       WS[9] = 0xff;
 
 -       HalUARTWrite(0,WS,10); 
 
 -       
 
 -       if(WS[2]>0x30 )//判断温度是否超过设定阈值
 
 -       {
 
 -         uint8 test[]={"警告警告温度过高"},len=0;//发送报警信息到语音合成模块
 
 -         len = sizeof(test);
 
 -         voice_input(test,len);
 
 -       }
 
 -       if(WS[3]>0x40 )////判断湿度浓度是否超过设定阈值
 
 -       {
 
 -         uint8 test[]={"警告警告湿度过高"},len=0;//发送报警信息到语音合成模块
 
 -         len = sizeof(test);
 
 -         voice_input(test,len);
 
 -       }
 
 -       break;
 
 -     }
 
 -   }
 
 - } 
 
  
 
- /*点对点传输函数,终端节点用来发送点对点消息到协调器*/
 
 - void SampleApp_SendPointToPointMessage( uint8 *dat )
 
 - {
 
 -   if ( AF_DataRequest( &Point_To_Point_DstAddr,
 
 -                       &SampleApp_epDesc,
 
 -                       SAMPLEAPP_POINT_TO_POINT_CLUSTERID,
 
 -                       *dat,//(byte)osal_strlen(dat),
 
 -                       dat,
 
 -                       &SampleApp_TransID,
 
 -                       AF_DISCV_ROUTE,
 
 -                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
 
 -   {
 
 -   } 
 
 -   else
 
 -   {
 
 -     // Error occurred in request to send.
 
 -   }
 
 - }
 
 - /**********************************************
 
  
- **********************************************/
 
 
  复制代码 
 |   
 
 
 
 |