MENU

自建网关接入Sonoff_Basic_R3 Zigbee版并实现HA控制

November 16, 2019 • Read: 199 • 技术,Home Assistant阅读设置

非常荣幸能拿到国内首批sonoff_basic_R3 zigbee版的开关模块,国外已经很早就上架了售卖了。此次业界大佬只赠送了三个样品,截止目前淘宝易微联官方店铺还没有上架,但是Sonoff_Basic_R3的WiFi版已经上架了,如需购买的话可以买了,zigbee的话还要等一段时间。
老样子还是先说一下体验,此次接入还是挺顺利的,正好手里有一个CC2530的模块我提前就把所需的协调器(网关)环境搭建好了就等开关模块到货,由于双十一大佬仓库那边耽搁了,今天才拿到货。使用起来感觉比之前的WiFi版要顺畅,之前WiFi版是放到灯里面的,外面还有一个铁盒子装起来的,信号也衰减了一大半,时不时就掉线了,这次的最主要是zigbee的不会受周围的干扰,因为我这边接了好多WiFi模块,设备一多延迟就特别大。虽然这次没有把之前的换掉,我感觉问题应该不大,我在网关上加了一根天线。
首先我们来看一下外观和内部结构

6.png

3.png

2.png

说实话这次的做工质量比我之前的版本好了很多,外壳的材料也换成了PC材料,之前的貌似是ABS的。
听业界大佬描述无需App直接支持Echo Plus / Echo show等音箱直接接入控制(通过亚马逊WWA认证),支持三星SmartThings Hub(通过三星WWST认证,同时海外博主实测兼容小米(Mijia)网关和欧瑞博mini网关接入,我没有上述的音响和网关,所以这次使用自建网关接入,最主要是接入HA 所以就选择zigbee2mqtt。

搭建网关环境:
所需工具和模块

7.png

我使用的是CC2530 zigbee模块做网关,首先安装SmartRF Flash programmer烧录软件,下载固件CC2530_DEFAULT_20190608.zip
启动SmartRF Flash Programmer,如下所示进行设置,然后按Perform actions。确保选择.hex文件,而不是.bin文件!
smartrf.png

到目前CC2530上就安装完成了,前提是相关的驱动也要正确安装才行
接下来在树莓派上安装运行zigbee2mqtt,具体安装步骤参考zigbee2mqtt
zigbee2mqtt运行起来之后就开始进出配对,在配对之前先修改一下树莓派上zigbee2mqtt的configuration.yaml使之允许设备加入,编辑sudo nano /opt/zigbee2mqtt/data/configuration.yaml

homeassistant: true
permit_join: true
mqtt:
  base_topic: zigbee2mqtt
  server: 'mqtt://192.168.1.18'   //mqtt
  user: *******
  password: *******
  client_id: zigbee
  include_device_information: true

当然也可以在HA里面加入一个允许加入设备的开关,这样方便以后加入设备就不用每次编辑配置文件,这样配对的时候直接使用开关就好了。

input_select:
  zigbee2mqtt_log_level:
    name: Zigbee2mqtt Log Level
    options:
     - debug
     - info
     - warn
     - error
    initial: info
    icon: mdi:format-list-bulleted

# Input boolean for enabling/disabling joining
input_boolean:
  zigbee_permit_join:
    name: Allow devices to join
    initial: off
    icon: mdi:cellphone-wireless

# Timer for joining time remaining (360 sec = 6 min)
timer:
  zigbee_permit_join:
    name: Time remaining
    duration: 360

# Sensor for monitoring the bridge state
sensor:
  - platform: mqtt
    name: Zigbee2mqtt Bridge state
    state_topic: "zigbee2mqtt/bridge/state"
    icon: mdi:router-wireless

# Switch for enabling joining
switch:
  - platform: mqtt
    name: "Zigbee2mqtt Main join"
    state_topic: "zigbee2mqtt/bridge/config/permit_join"
    command_topic: "zigbee2mqtt/bridge/config/permit_join"
    payload_on: "true"
    payload_off: "false"


automation:
  - alias: Zigbee2mqtt Log Level
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_select.zigbee2mqtt_log_level
        to: debug
      - platform: state
        entity_id: input_select.zigbee2mqtt_log_level
        to: warn
      - platform: state
        entity_id: input_select.zigbee2mqtt_log_level
        to: error
      - platform: state
        entity_id: input_select.zigbee2mqtt_log_level
        to: info
    action:
      - service: mqtt.publish
        data:
          payload_template: '{{ states(''input_select.zigbee2mqtt_log_level'') }}'
          topic: zigbee2mqtt/bridge/config/log_level

  - id: enable_zigbee_join
    alias: Enable Zigbee joining
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.zigbee_permit_join
      to: 'on'
    action:
    - service: mqtt.publish
      data:
        topic: zigbee2mqtt/bridge/config/permit_join
        payload: 'true'
    - service: timer.start
      data:
        entity_id: timer.zigbee_permit_join

  - id: disable_zigbee_join
    alias: Disable Zigbee joining
    hide_entity: true
    trigger:
    - entity_id: input_boolean.zigbee_permit_join
      platform: state
      to: 'off'
    action:
    - data:
        payload: 'false'
        topic: zigbee2mqtt/bridge/config/permit_join
      service: mqtt.publish
    - data:
        entity_id: timer.zigbee_permit_join
      service: timer.cancel

  - id: disable_zigbee_join_timer
    alias: Disable Zigbee joining by timer
    hide_entity: true
    trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.zigbee_permit_join
    action:
    - service: mqtt.publish
      data:
        topic: zigbee2mqtt/bridge/config/permit_join
        payload: 'false'
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.zigbee_permit_join

homeassistant:
 customize:
  timer.zigbee_permit_join:
   friendly_name: 倒计时
  input_boolean.zigbee_permit_join:
   friendly_name: 允许设备加入
  sensor.zigbee2mqtt_bridge_state:
   friendly_name: Zigbee桥状态
group:
  zigbee:
    view: false
    control: hidden
    name: Zigbee网关
    entities:
      - input_boolean.zigbee_permit_join
      - timer.zigbee_permit_join
      - sensor.zigbee2mqtt_bridge_state
      - switch.zigbee2mqtt_main_join
      - automation.enable_zigbee_joining
      - automation.disable_zigbee_joining
      - automation.disable_zigbee_joining_by_timer
      - input_select.zigbee2mqtt_log_level
      - automation.zigbee2mqtt_log_level

sonoff开关首次上电会自动进入配对模式,如长时间没配对成功,设备可能退出了配对模式,长按手动开关5秒直到信号指示灯闪烁即可,网关没问题的话就会出现如下图的配对成功日志(实时日志可运行sudo journalctl -u zigbee2mqtt.service -f查看)

zigbee.png

同时HA里面也会自动出现(前提已开启MQTT自动发现)一个类似switch.0x00124b001f669712_switchsensor.0x00124b001f669712_linkquality的设备ID,使用switch.0x00124b001f669712_switch就可以控制了。
如果没有开启自动发现或者自动发现没有生效时也可以再HA里面加入
注意这里的<FRIENDLY_NAME>为配对时的friendlyName,类似0x00124b001f669712,在实时日志里可以看到。

switch:
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/<FRIENDLY_NAME>"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_off: "OFF"
    payload_on: "ON"
    value_template: "{{ value_json.state }}"
    command_topic: "zigbee2mqtt/<FRIENDLY_NAME>/set"

sensor:
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/<FRIENDLY_NAME>"
    availability_topic: "zigbee2mqtt/bridge/state"
    unit_of_measurement: "-"
    value_template: "{{ value_json.linkquality }}"

下面是控制效果

再附一张开关模块的IO口,期待各位大佬能开发出更好的固件

1.png

最后与我之前的模块对比一下,总体上内部结构都差不对,大小也差不多,zigbee版的稍微大一点点,基本上普通86盒都能塞得下。

4.png
5.png

Leave a Comment

4 Comments
  1. Ai Ai

    #(小眼睛)dalao真强

  2. 非技术的路过。

  3. 黑客帝国既视感

  4. 缺一套房@(吐舌)