实验室智能管理系统(3)——zigbee部分

我使用的是ZStack-CC2530-2.5.1 修改了Sample app代码

SampleApp.c

/**************************************************************************************************
  Filename:       SampleApp.c
  Revised:        $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $
  Revision:       $Revision: 19453 $

  Description:    Sample Application (no Profile).


  Copyright 2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derASivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/

/*********************************************************************
  This application isn't intended to do anything useful, it is
  intended to be a simple example of an application's structure.

  This application sends it's messages either as broadcast or
  broadcast filtered group messages.  The other (more normal)
  message addressing is unicast.  Most of the other sample
  applications are written to support the unicast message model.

  Key control:
    SW1:  Sends a flash command to all devices in Group 1.
    SW2:  Adds/Removes (toggles) this device in and out
          of Group 1.  This will enable and disable the
          reception of the flash command.
*********************************************************************/

/*********************************************************************
 * INCLUDES
 */
#include "OSAL.h"
#include "ZGlobals.h"
#include "AF.h"
#include "aps_groups.h"
#include "ZDApp.h"

#include "SampleApp.h"

#include "OnBoard.h"

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
//#include "hal_uart.h"

#include  "MT_UART.h" //此处用于串口
#include  "MT.h"

#include  "DHT11.h"   //温湿度专用

/*********************************************************************
 * MACROS
 */

#define AIR   P2_0      //光敏传感器检测引脚 
/*
#define RELAY_1_PIN    P1_0
#define RELAY_2_PIN    P1_2
#define RELAY_3_PIN    P2_0
#define RELAY_4_PIN    P1_6   */

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

// This list should be filled with Application specific Cluster IDs.
const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
{
  SAMPLEAPP_DHT11_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;
};

// This is the Endpoint/Interface description.  It is defined here, but
// filled-in in SampleApp_Init().  Another way to go would be to fill
// in the structure here and make it a "const" (in code space).  The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t SampleApp_epDesc;

/*********************************************************************
 * EXTERNAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */

/*********************************************************************
 * LOCAL VARIABLES
 */
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_SensorGw_DstAddr,SampleApp_SensorCtl_DstAddr;

uint8 shakeFlag = 0;

int tianjiazhiwen;
int denglu;
uint8 usr_num;
/*********************************************************************
 * LOCAL FUNCTIONS
 */
void SampleApp_HandleKeys( uint8 shift, uint8 keys );
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
void SampleApp_SensorMessage( void );
void SampleApp_SendSensorCmd( uint8 enable );
void SerialCallback( uint8 port, uint8 events );
void UartSendNum( uint8 x );
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 )
{
  tianjiazhiwen = 0;
  denglu = 1;
  usr_num = 0x00;
  SampleApp_TaskID = task_id;
  SampleApp_NwkState = DEV_INIT;
  SampleApp_TransID = 0;

  //串口初始化
   MT_UartInit();//串口初始化
   MT_UartRegisterTaskID(task_id);//登记任务号

  // Setup for the periodic message's destination address
  SampleApp_SensorCtl_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//Ctl采用广播形式  协调器发送信息给终端
  SampleApp_SensorCtl_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_SensorCtl_DstAddr.addr.shortAddr = 0xFFFF;   

  SampleApp_SensorGw_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;//Gw采用点播   终端发送信息给协调器
  SampleApp_SensorGw_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_SensorGw_DstAddr.addr.shortAddr = 0;    //发送到协调器


  // 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 );

  // Register for all key events - This app will handle all key events
  RegisterForKeys( SampleApp_TaskID );


  //IO Init P20 P06
  //P0SEL &= ~0X40;  
  //P0DIR &= ~0X40;   
  //P0INP |= 0X40;

  //P2SEL &= ~0X01;  
  //P2DIR &= ~0X01;   
  //P2INP &= ~0x01; 

  //IO Init P13 P15
  /*
  P1SEL &= ~0x45;       //  0:GPIO
  P1DIR |= 0x45;       //  0:input    1:output

  P2SEL &= ~0x01;       //  0:GPIO
  P2DIR |= 0x01;       //  0:input    1:output

  P1_0 = 1;
  P1_2 = 1;
  P2_0 = 1;
  P1_6 = 1;*/

  P2SEL &= ~0X01;    //设置P2.0为普通IO口  
  P2DIR &= ~0X01;    // 在P2.0口,设置为输入模式 
  P2INP &= ~0x01;    //打开P2.0上拉电阻 

}

/*********************************************************************
 * @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层传递过来的数据,用网蜂方法接收,编译时不定义MT相关内容 
            {
                SampleApp_SerialCMD((mtOSALSerialData_t *)MSGpkt);
                break;
            }
            case KEY_CHANGE:
            {
                SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
                break;
            }
            case AF_INCOMING_MSG_CMD:
            {
                SampleApp_MessageMSGCB( MSGpkt );
                break;
            }
            case ZDO_STATE_CHANGE: //联网
            {
                SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
                if (SampleApp_NwkState == DEV_ZB_COORD)//协调器联网
                {
                    HalLcdWriteString( "Coordinate Start", HAL_LCD_LINE_1 );
                    //osal_start_timerEx( SampleApp_TaskID,SAMPLEAPP_SEND_PERIODIC_MSG_EVT,1000 );
                }
                else if(SampleApp_NwkState == DEV_END_DEVICE) //终端联网
                {
                    //HalLcdWriteString( "EndDevice Success to join the network", HAL_LCD_LINE_1 );

                    uint8 usr_cmd[8] = {0xF5, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xF5};//用户比对1:N

                    HalUARTWrite(0,usr_cmd,8);
                }
                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);
    }

    /*if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
    {
      //读取传感器并发送
      //SampleApp_SendPeriodicMessage();    
      DHT11();   //温度检测  
      uint8 T_H[4];//温湿度
      T_H[0]=wendu_shi+48;
      T_H[1]=wendu_ge%10+48;

      T_H[2]=shidu_shi+48;
      T_H[3]=shidu_ge%10+48;
      AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                           SAMPLEAPP_DHT11_CLUSTERID,
                           4,
                           T_H,
                           &SampleApp_TransID,
                           AF_DISCV_ROUTE,
                           AF_DEFAULT_RADIUS );

      osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
          (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );    
      // return unprocessed events
      return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
    }*/

    /*
    if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
    {
      // Send the periodic message
      //SampleApp_SendPeriodicMessage();//周期性发送函数
       //SampleApp_SendPointToPointMessage();//此处替换成点播函数
      uint8 L;
      if(AIR == 1)
      { 
        L=1;
        HalUARTWrite(0,"Got bad Air\n",12);     //串口
      }  
      else 
      {
        L=0;
        HalUARTWrite(0,"No bad Air\n",11);       //串口
      }

      AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                           SAMPLEAPP_SHAKE_CLUSTERID,
                           1,
                           &L,
                           &SampleApp_TransID,
                           AF_DISCV_ROUTE,
                           AF_DEFAULT_RADIUS );
      // 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 + (osal_rand() & 0x00FF)) );

      // return unprocessed events
      return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
    }*/
    return 0;
}

void SampleApp_HandleKeys( uint8 shift, uint8 keys ) //按键处理
{
    (void)shift;  // Intentionally unreferenced parameter

    if (SampleApp_NwkState == DEV_END_DEVICE)//协调器按键处理
    {
        if ( keys & HAL_KEY_SW_1 )
        {

        }

        if ( keys & HAL_KEY_SW_2 )             //按键S2处理 增加用户
        {
           uint8 usr_cmd[8] = {0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5};//查看用户数量
           tianjiazhiwen = 1;

           //uint8 usr_cmd[8] = {0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x05, 0xF5};//清除数据
           //uint8 usr_cmd[8] = {0xF5, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xF5};//用户比对1:N

           HalUARTWrite(0,usr_cmd,8); 
        }
    }
}


void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) //消息处理函数
{
  //HalUARTWrite(0,"Receive: \n",10);    
  switch ( pkt->clusterId )
  {
    case SAMPLEAPP_DHT11_CLUSTERID: //dht11
      {
          //***********温度打印***************
           //HalUARTWrite(0,"Temp is:",8);        //提示接收到数据
          // HalUARTWrite(0,&pkt->cmd.Data[0],2); //温度
          // HalUARTWrite(0,"\n",1);              // 回车换行 
           //***************湿度打印****************
          // HalUARTWrite(0,"Humidity is:",12);    //提示接收到数据
          // HalUARTWrite(0,&pkt->cmd.Data[2],2);  //湿度
          // HalUARTWrite(0,"\n",1);              // 回车换行

           //****************与arm通讯*****************
           uchar zigbee[11];
           zigbee[0] = 'F';
           zigbee[1] = 'D';
           zigbee[2] = 0x01;
           zigbee[3] = (pkt->cmd.Data[0]-48)*10 + pkt->cmd.Data[1]-48;
           zigbee[4] = 'E';

           zigbee[5] = 'F';
           zigbee[6] = 'D';
           zigbee[7] = 0x02;
           zigbee[8] = (pkt->cmd.Data[2]-48)*10 + pkt->cmd.Data[3]-48;
           zigbee[9] = 'E'; 

           HalUARTWrite(0,zigbee,11);    
           break;
      }
    /*case SAMPLEAPP_LED_CLUSTERID: //LED AND FAN
      {
         HalUARTWrite(0,"get led cmd:",12); 
         HalUARTWrite(0,pkt->cmd.Data,11);
         if(pkt->cmd.Data[2] == '0')//led1_open
         {
           HalUARTWrite(0,"get cmd 100\n",12);
           RELAY_1_PIN = 0;
         }
         if(pkt->cmd.Data[2] == '1')//led1_close
         {
           HalUARTWrite(0,"get cmd 101\n",12);
           RELAY_1_PIN = 1;
         }
         if(pkt->cmd.Data[2] == '2')//led2_open
         {
           HalUARTWrite(0,"get cmd 102\n",12);
           RELAY_2_PIN = 0;
         }
         if(pkt->cmd.Data[2] == '3')//led2_close
         {
           HalUARTWrite(0,"get cmd 103\n",12);
           RELAY_2_PIN = 1;
         }
         if(pkt->cmd.Data[2] == '4')//led3_open
         {
           HalUARTWrite(0,"get cmd 104\n",12);
           RELAY_3_PIN = 0;
         }
         if(pkt->cmd.Data[2] == '5')//led4_close
         {
           HalUARTWrite(0,"get cmd 105\n",12);
           RELAY_3_PIN = 1;
         }
         if(pkt->cmd.Data[2] == '6')//fan_open
         {
           HalUARTWrite(0,"get cmd 106\n",12);
           RELAY_4_PIN = 0;
         }
         if(pkt->cmd.Data[2] == '7'  )//fan_close
         {
           HalUARTWrite(0,"get cmd 107\n",12);
           RELAY_4_PIN = 1;
         }
         break; 
      }*/
    case SAMPLEAPP_AIR_CLUSTERID:
      {
        if(pkt->cmd.Data[0])
        {
         //HalUARTWrite(0,"Got bad Air\n",12);     //有烟雾
         //****************与arm通讯*****************
         uchar zigbee[11];
         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x04;
         zigbee[3] = 'B';
         zigbee[4] = 'E';

         HalUARTWrite(0,zigbee,11); 
        }
        else 
        {
          //HalUARTWrite(0,"No bad Air\n",11);     //无烟雾  
          //****************与arm通讯*****************
           uchar zigbee[11];
           zigbee[0] = 'F';
           zigbee[1] = 'D';
           zigbee[2] = 0x04;
           zigbee[3] = 'A';
           zigbee[4] = 'E';

           HalUARTWrite(0,zigbee,11); 
        }
        break;
     }
      case SAMPLEAPP_SHAKE_CLUSTERID:
      {
        if(pkt->cmd.Data[0])
        {
         //HalUARTWrite(0,"Got bad Air\n",12);     //有紫外线
         //****************与arm通讯*****************
         uchar zigbee[11];
         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x03;
         zigbee[3] = 'B';
         zigbee[4] = 'E';

         HalUARTWrite(0,zigbee,11); 
        }
        else 
        {
          //HalUARTWrite(0,"No bad Air\n",11);     //无紫外线  
          //****************与arm通讯*****************
           uchar zigbee[11];
           zigbee[0] = 'F';
           zigbee[1] = 'D';
           zigbee[2] = 0x03;
           zigbee[3] = 'A';
           zigbee[4] = 'E';

           HalUARTWrite(0,zigbee,11); 
        }
        break;
     }
     case SAMPLEAPP_FINGER_CLUSTERID:
      {
         if(pkt->cmd.Data[0] == 'F' && pkt->cmd.Data[1] == 'D')
         {
           HalUARTWrite(0,pkt->cmd.Data,11);
         }
        else if(pkt->cmd.Data[0] == '2' && pkt->cmd.Data[2] == '0')//denglu
         {
           //HalUARTWrite(0,"get cmd 200\n",12);
           denglu = 0;
           uint8 usr_cmd[8] = {0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5};//查看用户数量
           HalUARTWrite(0,usr_cmd,8);          
         }
        else if(pkt->cmd.Data[0] == '2' && pkt->cmd.Data[2] == '1')//add zhiwen
         {
           tianjiazhiwen = 1;
           uint8 usr_cmd[8] = {0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5};//查看用户数量
           //uint8 usr_cmd[8] = {0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x05, 0xF5};//清除数据
           //uint8 usr_cmd[8] = {0xF5, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xF5};//用户比对1:N

           HalUARTWrite(0,usr_cmd,8);
         }
        else if(pkt->cmd.Data[0] == '2' && pkt->cmd.Data[2] == '2')//del zhiwen
         {
           //uint8 usr_cmd[8] = {0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5};//查看用户数量
           uint8 usr_cmd[8] = {0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x05, 0xF5};//清除数据           
           HalUARTWrite(0,usr_cmd,8);
         }
        else if(pkt->cmd.Data[0] == '2' && pkt->cmd.Data[2] == '3')//del zhiwen
         {
           tianjiazhiwen = 0;
           uint8 usr_cmd[8] = {0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5};//查看用户数量   
           HalUARTWrite(0,usr_cmd,8);
         }
        else if(pkt->cmd.Data[0] == '2' && pkt->cmd.Data[2] == '4')//del zhiwen
         {
           uint8 usr_cmd[8] = {0xF5, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xF5};//用户比对1:N       
           HalUARTWrite(0,usr_cmd,8);
         }
         else if(pkt->cmd.Data[0] == '2' && pkt->cmd.Data[2] == '5')//restart
         {
            tianjiazhiwen = 0;
            denglu = 1;
            uint8 usr_cmd[8] = {0xF5, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xF5};//用户比对1:N
            HalUARTWrite(0,usr_cmd,8);          
         }
        else if(pkt->cmd.Data[0] == 0xF5)
        {
          HalUARTWrite(0,pkt->cmd.Data,8);
        }
        else
        {
          uint8 zigbee_error[4] = {0xFF, 0xFF, 0xFF, 0xFF};
          HalUARTWrite(0,zigbee_error,4);
        }
        break;
     }
    default:
    {
      break;
    }
  }
}

/*********************************************************************
 * @fn      SampleApp_SensorMessage
 *
 * @brief   Send the sensor message.
 *
 * @param   none
 *
 * @return  none
 */
void SampleApp_SensorMessage( void )//终端发送消息
{


}

/*********************************************************************
 * @fn      SampleApp_SendSensorCmd
 *
 * @brief   Send the sensor cmd.
 *
 * @param   none
 *
 * @return  none
 */
void SampleApp_SendSensorCmd( uint8 enable ) //协调器发送消息
{
  if(enable == 1)
  {
     HalUARTWrite(0,"#2\n",3); 
    AF_DataRequest( &SampleApp_SensorCtl_DstAddr, &SampleApp_epDesc, //广播发送
                       SAMPLEAPP_RELAY_CLUSTERID,
                       1,
                       (uint8*)&enable,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );
      HalUARTWrite(0,"send GDQ cmd\n",12); 
  }
  else if(enable == 2)
  {
    AF_DataRequest( &SampleApp_SensorCtl_DstAddr, &SampleApp_epDesc, //广播发送
                       SAMPLEAPP_DHT11_CLUSTERID,
                       1,
                       (uint8*)&enable,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );
      HalUARTWrite(0,"send DHT11 cmd\n",15);
  }
  else 
  {
    HalUARTWrite(0,"unknown enable\n",15);
  }
}





/*********************************************************************
*********************************************************************/
void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg)//串口接收
{
  uint8 i,len,*str=NULL;     //len有用数据长度
  str=cmdMsg->msg;          //指向数据开头
  len=*str;                 //msg里的第1个字节代表后面的数据长度 

  uint8 cmd [11];
  for(i=1;i<=len;i++)
  {
    cmd[i - 1] = *(str + i);
    //HalUARTWrite(0,&cmd[i - 1],1);
  }

  if(cmd[0] == '1' && SampleApp_NwkState == DEV_ZB_COORD)
  {
      AF_DataRequest( &SampleApp_SensorCtl_DstAddr, &SampleApp_epDesc, //广播发送
                       SAMPLEAPP_LED_CLUSTERID,
                       11,
                       (uint8*)&cmd,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );
      HalUARTWrite(0,"send led cmd\n",12); 
  }
  if(cmd[0] == '2' && SampleApp_NwkState == DEV_ZB_COORD)
  {
      AF_DataRequest( &SampleApp_SensorCtl_DstAddr, &SampleApp_epDesc, //广播发送
                       SAMPLEAPP_FINGER_CLUSTERID,
                       11,
                       (uint8*)&cmd,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );
      HalUARTWrite(0,"send finger cmd\n",12); 
      HalUARTWrite(0,cmd,11); 
  }

  if(SampleApp_NwkState == DEV_END_DEVICE)
  {
    AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送调试信息
                       SAMPLEAPP_FINGER_CLUSTERID,
                       8,
                       cmd,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );
  }

  if(tianjiazhiwen == 1 && cmd[0] == 0xF5 && cmd[1] == 0x05 && cmd[4] == 0x00 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//检查成功清除数据
  {    
    uchar zigbee[11];
    zigbee[0] = 'F';
    zigbee[1] = 'D';
    zigbee[2] = 0x09;
    zigbee[3] = 'A';
    zigbee[4] = 'E';    
   AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                   SAMPLEAPP_FINGER_CLUSTERID,
                   11,
                   zigbee,
                   &SampleApp_TransID,
                   AF_DISCV_ROUTE,
                   AF_DEFAULT_RADIUS );
  }
  if(cmd[0] == 0xF5 && cmd[1] == 0x09 && cmd[4] == 0x00 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//第一次添加指纹
  {
    uchar zigbee[11];
    zigbee[0] = 'F';
    zigbee[1] = 'D';
    zigbee[2] = 0x0A;
    zigbee[3] = cmd[3];
    zigbee[4] = 'E';    
   AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                   SAMPLEAPP_FINGER_CLUSTERID,
                   11,
                   zigbee,
                   &SampleApp_TransID,
                   AF_DISCV_ROUTE,
                   AF_DEFAULT_RADIUS );
    if(tianjiazhiwen == 1)
    {
      usr_num = cmd[3] + 1;
      uint8 usr_cmd[8] = {0xF5, 0x01, 0x00, usr_num, 0x01, 0x00, 0x01^usr_num^0x01, 0xF5};       
      AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送调试信息
                     SAMPLEAPP_FINGER_CLUSTERID,
                     8,
                     usr_cmd,
                     &SampleApp_TransID,
                     AF_DISCV_ROUTE,
                     AF_DEFAULT_RADIUS );
      HalUARTWrite(0,usr_cmd,8); 
    }
  }
  if(tianjiazhiwen == 1 && cmd[0] == 0xF5 && cmd[4] == 0x07 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//指纹已存在
  {
    tianjiazhiwen = 0;
    uchar zigbee[11];
    zigbee[0] = 'F';
    zigbee[1] = 'D';
    zigbee[2] = 0x08;
    zigbee[3] = 'A';
    zigbee[4] = 'E';    
   AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                   SAMPLEAPP_FINGER_CLUSTERID,
                   11,
                   zigbee,
                   &SampleApp_TransID,
                   AF_DISCV_ROUTE,
                   AF_DEFAULT_RADIUS );

   //uint8 usr_cmd[8] = {0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5};//查看用户数量
   //HalUARTWrite(0,usr_cmd,8);

  }
  if(tianjiazhiwen == 1 && cmd[0] == 0xF5 && cmd[1] == 0x01 && cmd[4] == 0x00 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//第2次添加指纹
  {
    uint8 usr_cmd[8] = {0xF5, 0x02, 0x00, usr_num, 0x01, 0x00, 0x02^usr_num^0x01, 0xF5};

    AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送调试信息
                     SAMPLEAPP_FINGER_CLUSTERID,
                     8,
                     usr_cmd,
                     &SampleApp_TransID,
                     AF_DISCV_ROUTE,
                     AF_DEFAULT_RADIUS );

    HalUARTWrite(0,usr_cmd,8);  
  }
  if(tianjiazhiwen == 1 && cmd[0] == 0xF5 && cmd[1] == 0x02 && cmd[4] == 0x00 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//第3次添加指纹
  {
    uint8 usr_cmd[8] = {0xF5, 0x03, 0x00, usr_num, 0x01, 0x00, 0x03^usr_num^0x01, 0xF5};

    AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送调试信息
                     SAMPLEAPP_FINGER_CLUSTERID,
                     8,
                     usr_cmd,
                     &SampleApp_TransID,
                     AF_DISCV_ROUTE,
                     AF_DEFAULT_RADIUS );

    HalUARTWrite(0,usr_cmd,8);      
  }
  if(tianjiazhiwen == 1 && cmd[0] == 0xF5 && cmd[1] == 0x03 && cmd[4] == 0x00 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//验证指纹录入成功
  {
       tianjiazhiwen = 0;    
       uchar zigbee[11];
         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x07;
         zigbee[3] = 'A';
         zigbee[4] = 'E';    
       AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                       SAMPLEAPP_FINGER_CLUSTERID,
                       11,
                       zigbee,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );
  }
  if(cmd[0] == 0xF5 && cmd[1] == 0x0C && cmd[4] == 0x01 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//用户比对1:N 指纹校对成功
  {      
    uchar zigbee[11];
    if(denglu == 1)//登陆界面识别指纹 向arm发送消息
    {
        denglu = 0;

         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x05;
         zigbee[3] = 'A';
         zigbee[4] = 'E';
    }
    else          //主界面识别指纹  向arm发送消息                                       
    {
         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x06;
         zigbee[3] = 'A';
         zigbee[4] = 'E';
    }
    AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                       SAMPLEAPP_FINGER_CLUSTERID,
                       11,
                       zigbee,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS ); 
  }
  if(cmd[0] == 0xF5 && cmd[1] == 0x0C && cmd[4] == 0x05 && cmd[7] == 0xF5 && SampleApp_NwkState == DEV_END_DEVICE)//用户比对1:N 指纹校对error
  {
    uchar zigbee[11];
    if(denglu == 1)//登陆界面重新识别指纹 向arm发送消息
    {
        uint8 usr_cmd[8] = {0xF5, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xF5};//用户比对1:N         
        HalUARTWrite(0,usr_cmd,8); 
         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x05;
         zigbee[3] = 'B';
         zigbee[4] = 'E';
    }  
    else        //主界面未能识别指纹  向arm发送消息  
    {
         zigbee[0] = 'F';
         zigbee[1] = 'D';
         zigbee[2] = 0x06;
         zigbee[3] = 'B';
         zigbee[4] = 'E';
    }
    AF_DataRequest( &SampleApp_SensorGw_DstAddr, &SampleApp_epDesc, //点播发送
                       SAMPLEAPP_FINGER_CLUSTERID,
                       11,
                       zigbee,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS ); 
  }
}

SampleApp.h

/**************************************************************************************************
  Filename:       SampleApp.h
  Revised:        $Date: 2007-10-27 17:22:23 -0700 (Sat, 27 Oct 2007) $
  Revision:       $Revision: 15795 $

  Description:    This file contains the Sample Application definitions.


  Copyright 2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com. 
**************************************************************************************************/

#ifndef SAMPLEAPP_H
#define SAMPLEAPP_H

#ifdef __cplusplus
extern "C"
{
#endif

/*********************************************************************
 * INCLUDES
 */
#include "ZComDef.h"

/*********************************************************************
 * CONSTANTS
 */

// These constants are only for example and should be changed to the
// device's needs
#define SAMPLEAPP_ENDPOINT           20

#define SAMPLEAPP_PROFID             0x0F08
#define SAMPLEAPP_DEVICEID           0x0001
#define SAMPLEAPP_DEVICE_VERSION     0
#define SAMPLEAPP_FLAGS              0

#define SAMPLEAPP_MAX_CLUSTERS       1
#define SAMPLEAPP_DHT11_CLUSTERID    10
#define SAMPLEAPP_LIGHT_CLUSTERID    11
#define SAMPLEAPP_AIR_CLUSTERID      12
#define SAMPLEAPP_LED_CLUSTERID  13
#define SAMPLEAPP_FIRE_CLUSTERID     14
#define SAMPLEAPP_SHAKE_CLUSTERID    15
#define SAMPLEAPP_IR_CLUSTERID       16
#define SAMPLEAPP_BEEP_CLUSTERID     17
#define SAMPLEAPP_RELAY_CLUSTERID    18
#define SAMPLEAPP_FINGER_CLUSTERID   19


// Send Message Timeout
#define SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT   1000     // Every 1 seconds

// Application Events (OSAL) - These are bit weighted definitions.
#define SAMPLEAPP_SEND_PERIODIC_MSG_EVT       0x0001

// Group ID for Flash Command
#define SAMPLEAPP_FLASH_GROUP                  0x0001

// Flash Command Duration - in milliseconds
#define SAMPLEAPP_FLASH_DURATION               1000

/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * FUNCTIONS
 */

/*
 * Task Initialization for the Generic Application
 */
extern void SampleApp_Init( uint8 task_id );

/*
 * Task Event Processor for the Generic Application
 */
extern UINT16 SampleApp_ProcessEvent( uint8 task_id, uint16 events );

/*********************************************************************
*********************************************************************/

#ifdef __cplusplus
}
#endif

#endif /* SAMPLEAPP_H */

DHT11.c

#include <ioCC2530.h>
#include "OnBoard.h"

#define uint unsigned int
#define uchar unsigned char

#define wenshi P0_6

/*******函数声明*********/
void Delay_us(void); //1 us延时
void Delay_10us(void); //10 us延时
void Delay_ms(uint Time);//n ms延时
void COM(void); // 温湿写入
void DHT11(void) ;  //温湿传感启动


//温湿度定义
uchar ucharFLAG,uchartemp;
uchar shidu_shi,shidu_ge,wendu_shi,wendu_ge=4;
uchar ucharT_data_H,ucharT_data_L,ucharRH_data_H,ucharRH_data_L,ucharcheckdata;
uchar ucharT_data_H_temp,ucharT_data_L_temp,ucharRH_data_H_temp,ucharRH_data_L_temp,ucharcheckdata_temp;
uchar ucharcomdata;

uchar temp[2]={0,0}; 
uchar temp1[5]="temp=";
uchar humidity[2]={0,0};
uchar humidity1[9]="humidity=";

/****************************
        延时函数
*****************************/
void Delay_us() //1 us延时
{
   MicroWait(1);   
}

void Delay_10us() //10 us延时
{
  MicroWait(10);
}

void Delay_ms(uint Time)//n ms延时
{
  unsigned char i;
  while(Time--)
  {
    for(i=0;i<100;i++)
     Delay_10us();
  }
}


/***********************
   温湿度传感
***********************/
void COM(void)  // 温湿写入
{     
    uchar i;         
    for(i=0;i<8;i++)    
    {
     ucharFLAG=2; 
     while((!wenshi)&&ucharFLAG++);
     Delay_10us();
     Delay_10us();
     Delay_10us();
     uchartemp=0;
     if(wenshi)uchartemp=1;
     ucharFLAG=2;
     while((wenshi)&&ucharFLAG++);   
     if(ucharFLAG==1)break;    
     ucharcomdata<<=1;
     ucharcomdata|=uchartemp; 
     }    
}

void DHT11(void)   //温湿传感启动
{
    wenshi=0;
    Delay_ms(19);  //>18MS
    wenshi=1; 
    P0DIR &= ~0x40; //重新配置IO口方向
    Delay_10us();
    Delay_10us();                       
    Delay_10us();
    Delay_10us();  
     if(!wenshi) 
     {
      ucharFLAG=2; 
      while((!wenshi)&&ucharFLAG++);
      ucharFLAG=2;
      while((wenshi)&&ucharFLAG++); 
      COM();
      ucharRH_data_H_temp=ucharcomdata;
      COM();
      ucharRH_data_L_temp=ucharcomdata;
      COM();
      ucharT_data_H_temp=ucharcomdata;
      COM();
      ucharT_data_L_temp=ucharcomdata;
      COM();
      ucharcheckdata_temp=ucharcomdata;
      wenshi=1; 
      uchartemp=(ucharT_data_H_temp+ucharT_data_L_temp+ucharRH_data_H_temp+ucharRH_data_L_temp);
       if(uchartemp==ucharcheckdata_temp)
      {
          ucharRH_data_H=ucharRH_data_H_temp;
          ucharRH_data_L=ucharRH_data_L_temp;
          ucharT_data_H=ucharT_data_H_temp;
          ucharT_data_L=ucharT_data_L_temp;
          ucharcheckdata=ucharcheckdata_temp;
       }
         wendu_shi=ucharT_data_H/10; 
         wendu_ge=ucharT_data_H%10;

         shidu_shi=ucharRH_data_H/10; 
         shidu_ge=ucharRH_data_H%10;        
    } 
    else //没用成功读取,返回0
    {
         wendu_shi=0; 
         wendu_ge=0;

         shidu_shi=0; 
         shidu_ge=0;  
    } 
   P0DIR |= 0x40; //IO口需要重新配置 
}

DTH11.h

#ifndef __DHT11_H__
#define __DHT11_H__

#define uchar unsigned char
extern void Delay_ms(unsigned int xms); //延时函数
extern void COM(void);         // 温湿写入
extern void DHT11(void);   //温湿传感启动

extern uchar temp[2]; 
extern uchar temp1[5];
extern uchar humidity[2];
extern uchar humidity1[9];
extern uchar shidu_shi,shidu_ge,wendu_shi,wendu_ge;

#endif

上面主要是指纹模块通讯和DHT11部分
继电器和其他的一部分需修改一下代码 把注释部分去掉
此外需要注意的地方 和ARM通讯的时候需要修改协议栈串口部分的波特率115200和交叉线设置为false
修改串口接收函数
void MT_UartProcessZToolData ( uint8 port, uint8 event )用的是网蜂提供的代码

void MT_UartProcessZToolData ( uint8 port, uint8 event )
{
  uint8 flag=0,i,j=0;   //flag是判断有没有收到数据,j记录数据长度
  uint8 buf[128];     //串口buffer最大缓冲默认是128,我们这里用128.
  (void)event;        // Intentionally unreferenced parameter  

  while (Hal_UART_RxBufLen(port)) //检测串口数据是否接收完成

  {
    HalUARTRead (port,&buf[j], 1);  //把数据接收放到buf中
    j++;                           //记录字符数
    flag=1;                         //已经从串口接收到信息
  } 

  if(flag==1)       //已经从串口接收到信息

  {     /* Allocate memory for the data */
        //分配内存空间,为机构体内容+数据内容+1个记录长度的数据
   pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof  
          ( mtOSALSerialData_t )+j+1);
  //事件号用原来的CMD_SERIAL_MSG
  pMsg->hdr.event = CMD_SERIAL_MSG;
  pMsg->msg = (uint8*)(pMsg+1);  // 把数据定位到结构体数据部分
  pMsg->msg [0]= j;              //给上层的数据第一个是长度
  for(i=0;i<j;i++)                //从第二个开始记录数据 
  pMsg->msg [i+1]= buf[i];   
  osal_msg_send( App_TaskID, (byte *)pMsg );  //登记任务,发往上层
  /* deallocate the msg */
  osal_msg_deallocate ( (uint8 *)pMsg );      //释放内存
  }
}

最后来一张全家福照片
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值