基于Z-Stack的零火线开关(一)

1. 在 Z-Stack Home 1.2.2a中提供的例程中包含SampleSwitch例程,我们在该例程的基础上进行开发。

2. 打开SampleSwitch工程,在 zcl_samplesw_data.c 文件中修改 Basic Cluster 信息。

// Basic Cluster
const uint8 zclSampleSw_HWRevision = SAMPLESW_HWVERSION;
const uint8 zclSampleSw_ZCLVersion = SAMPLESW_ZCLVERSION;
const uint8 zclSampleSw_ManufacturerName[] = { 16, 'T','e','x','a','s','I','n','s','t','r','u','m','e','n','t','s' };
const uint8 zclSampleSw_ModelId[] = { 16, 'T','I','0','0','0','1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
const uint8 zclSampleSw_DateCode[] = { 16, '2','0','0','6','0','8','3','1',' ',' ',' ',' ',' ',' ',' ',' ' };
const uint8 zclSampleSw_PowerSource = POWER_SOURCE_MAINS_1_PHASE;

uint8 zclSampleSw_LocationDescription[17] = { 16, ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
uint8 zclSampleSw_PhysicalEnvironment = 0;
uint8 zclSampleSw_DeviceEnable = DEVICE_ENABLED;

3. 修改简单描述符信息,此处主要是对Device Id和Cluster列表的修改,市面上的ZigBee零火线开关Device Id有0x0000(OnOff Switch),0x0002(OnOff Output),还有一些公司用0x0100(OnOff Light)来做。这里需要说明的是HA规范里面0x0000和0x0002的InCluster里面是不包含OnOff Cluster的,不管采用0x0000,0x0002还是0x0100都需要在 InCluster 列表里面添加OnOff Cluster,这样才能接收到OnOff命令。

SimpleDescriptionFormat_t zclSampleSw_SimpleDesc =
{
  SAMPLESW_ENDPOINT,                  //  int Endpoint;
  ZCL_HA_PROFILE_ID,                  //  uint16 AppProfId[2];
  ZCL_HA_DEVICEID_ON_OFF_SWITCH,      //  uint16 AppDeviceId[2];
  SAMPLESW_DEVICE_VERSION,            //  int   AppDevVer:4;
  SAMPLESW_FLAGS,                     //  int   AppFlags:4;
  ZCLSAMPLESW_MAX_INCLUSTERS,         //  byte  AppNumInClusters;
  (cId_t *)zclSampleSw_InClusterList, //  byte *pAppInClusterList;
  ZCLSAMPLESW_MAX_OUTCLUSTERS,        //  byte  AppNumInClusters;
  (cId_t *)zclSampleSw_OutClusterList //  byte *pAppInClusterList;
};

4. 在zcl_samplesw.c中添加OnOff Callback函数,如下列代码中的zclSampleSw_OnOffCB

/*********************************************************************
 * ZCL General Profile Callback table
 */
static zclGeneral_AppCallbacks_t zclSampleSw_CmdCallbacks =
{
  zclSampleSw_BasicResetCB,               // Basic Cluster Reset command
  zclSampleSw_IdentifyCB,                 // Identify command
#ifdef ZCL_EZMODE
  NULL,                                   // Identify EZ-Mode Invoke command
  NULL,                                   // Identify Update Commission State command
#endif
  NULL,                                   // Identify Trigger Effect command
  zclSampleSw_IdentifyQueryRspCB,         // Identify Query Response command
  zclSampleSw_OnOffCB,                    // On/Off cluster commands
  NULL,                                   // On/Off cluster enhanced command Off with Effect
  NULL,                                   // On/Off cluster enhanced command On with Recall Global Scene
  NULL,                                   // On/Off cluster enhanced command On with Timed Off
#ifdef ZCL_LEVEL_CTRL
  NULL,                                   // Level Control Move to Level command
  NULL,                                   // Level Control Move command
  NULL,                                   // Level Control Step command
  NULL,                                   // Level Control Stop command
#endif
#ifdef ZCL_GROUPS
  NULL,                                   // Group Response commands
#endif
#ifdef ZCL_SCENES
  NULL,                                   // Scene Store Request command
  NULL,                                   // Scene Recall Request command
  NULL,                                   // Scene Response command
#endif
#ifdef ZCL_ALARMS
  NULL,                                   // Alarm (Response) commands
#endif
#ifdef SE_UK_EXT
  NULL,                                   // Get Event Log command
  NULL,                                   // Publish Event Log command
#endif
  NULL,                                   // RSSI Location command
  NULL                                    // RSSI Location Response command
};

5. 写zclSampleSw_OnOffCB函数内容,就是收到OnOff命令后的处理过程,注意需要声明该函数

/*********************************************************************
 * @fn      zclSamplesw_OnOffCB
 *
 * @brief   Callback from the ZCL General Cluster Library when
 *          it received an On/Off Command for this application.
 *
 * @param   cmd - COMMAND_ON, COMMAND_OFF or COMMAND_TOGGLE
 *
 * @return  none
 */
static void zclSamplesw_OnOffCB( uint8 cmd )
{
  // Turn on the light
  if ( cmd == COMMAND_ON )
  {
    zclSamplesw_OnOff = LIGHT_ON;
  }
  // Turn off the light
  else if ( cmd == COMMAND_OFF )
  {
    zclSamplesw_OnOff = LIGHT_OFF;
  }
  // Toggle the light
  else if ( cmd == COMMAND_TOGGLE )
  {
    if ( zclSamplesw_OnOff == LIGHT_OFF )
    {
      zclSamplesw_OnOff = LIGHT_ON;
    }
    else
    {
      zclSamplesw_OnOff = LIGHT_OFF;
    }
  }

   if(zclSamplesw_OnOff == LIGHT_ON)
   {
      HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
   }
   else
   {
      HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
   }
}

6. 选择Router

基于Z-Stack的零火线开关(一)_第1张图片

7. 设置编译选项:点击Project——Options,在HOLD_AUTO_START前加“x”,设置为自启动模式。

基于Z-Stack的零火线开关(一)_第2张图片

8. 设置信道:将信道与协调器信道设置一致,我们的信道是16。

基于Z-Stack的零火线开关(一)_第3张图片

9. 在线调试:点击Download and Debug

基于Z-Stack的零火线开关(一)_第4张图片

10. 进入调试界面后点击Go来启动设备。

基于Z-Stack的零火线开关(一)_第5张图片

11. 协调器设置为允许入网,设备入网后,协调器发送OnOff命令,验证设备状态。

你可能感兴趣的:(ZigBee)