2019-12-03

Setting up a Zigbee2mqtt gateway

Here are the steps I performed to setup a Zigbee2mqtt gateway using a CC2530+CC2591 board and a RaspberryPI 3.
  • My RaspberryPI 3 is running
    pi@racknode:~ $ uname -a
    Linux racknode 4.19.42-v7+ #1219 SMP Tue May 14 21:20:58 BST 2019 armv7l GNU/Linux
    pi@racknode:~ $ hostnamectl
       Static hostname: racknode
             Icon name: computer
            Machine ID: confidential
               Boot ID: confidential
      Operating System: Raspbian GNU/Linux 9 (stretch)
                Kernel: Linux 4.19.42-v7+
          Architecture: arm
  • My Zigbee board:


The steps can be structured in three groups:

  1. Flash the Z-Stack coordinator firmware on the Zigbee board
  2. Install zigbee2mqtt to run on the Raspberry Pi
  3. Setup a daemon to run zigbee2mqtt

Flash the Z-Stack coordinator firmware on the Zigbee board

  • Connect the Zigbee board with the Raspberry (Note: this is the connection schema for flashing the firmware, while normal operation another connection schema is used.)

    Name   Raspi     CC2530+CC2591*   
    3,3V/VCC Pin01P12 Pin02
    GND Pin39P12 Pin01
    RESET Pin35P12 Pin03
    DC Pin36P14 Pin04
    DD Pin38P14 Pin05
    *) P12 - 12pin connector, P14 - 14 pin connector




  • Install the wiringPI library
    sudo apt-get install wiringpi
  • Install git
    pi@racknode:~ $ sudo apt-get install git
    
  • Clone the flasher repo
    git clone https://github.com/jmichault/flash_cc2531.git
  • Test the connection
    pi@racknode:~/flash_cc2531 $ ./cc_chipid
      ID = a524.
  • Get the firmware - as mentioned above I need the CC2530+CC2591 version. See https://github.com/Koenkk/Z-Stack-firmware/tree/master/coordinator/Z-Stack_Home_1.2/bin/default for the correct version.
    pi@racknode:~/flash_cc2531 $ wget https://github.com/Koenkk/Z-Stack-firmware/raw/master/
    coordinator/Z-Stack_Home_1.2/bin/default/CC2530_CC2591_DEFAULT_20190608.zip
  • Extract the archive
    pi@racknode:~/flash_cc2531 $ unzip CC2530_CC2591_DEFAULT_20190608.zip
    Archive:  CC2530_CC2591_DEFAULT_20190608.zip
      inflating: CC2530ZNP-Prod.hex
      inflating: CC2530ZNP-Prod.bin
    pi@racknode:~/flash_cc2531 $
  • Erase the CC2531 CC2530
    pi@racknode:~/flash_cc2531 $ ./cc_erase
      ID = a524.
      erase result = 00a6.
    pi@racknode:~/flash_cc2531 $
  • Write the new firmware (takes about 2-3 minutes)
    pi@racknode:~/flash_cc2531 $ ./cc_write CC2530ZNP-Prod.hex
      ID = a524.
      reading line 15490.
      file loaded (15497 lines read).
    writing page 128/128.
    verifying page 128/128.
     flash OK.
    pi@racknode:~/flash_cc2531 $ cd ..
    pi@racknode:~ $

Install zigbee2mqtt to run on the Raspberry Pi

Steps based on
My steps were
  • Connect the Zigbee board with the Raspberry for operation mode

    Name   Raspi     CC2530+CC2591*   
    3,3V/VCCPin01P12 Pin02
    GND Pin39P12 Pin01
    TX (R→Z)Pin08P12 Pin07
    RX (R←Z) Pin10P12 Pin08
    *) P12 - 12pin connector




  • Enable UART and disable UART use for Bluetooth by adding following lines to /boot/config.txt
    pi@racknode:~ $ sudo vi /boot/config.txt
    
    enable_uart=1
    dtoverlay=pi3-disable-bt
    
  • Disable modem system
    sudo systemctl disable hciuart
  • Remove the serial console entry from /boot/cmdline.txt
    Before
    pi@racknode:~ $ cat /boot/cmdline.txt
    dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=...
    
    After
    pi@racknode:~ $ cat /boot/cmdline.txt
    dwc_otg.lpm_enable=0 console=tty1 root=...
    
  • Reboot the Raspberry
  • Installing the node.js 10.0 repo and install node.js
    pi@racknode:~ $ sudo curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    ... and install node.js
    pi@racknode:~ $ sudo apt-get install -y nodejs
  • Clone the zigbee2mqtt repo
    pi@racknode:~ $ sudo git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
    ... and change own to pi
    pi@racknode:~ $ sudo chown -R pi:pi /opt/zigbee2mqtt
  • Install the dependencies
    pi@racknode:~ $ cd /opt/zigbee2mqtt
    pi@racknode:/opt/zigbee2mqtt $ npm ci
    prebuild-install WARN install No prebuilt binaries found (target=10.17.0 runtime=node arch=arm libc= platform=linux) make: Entering directory '/opt/zigbee2mqtt/node_modules/zigbee-herdsman/ node_modules/@serialport/bindings/build' CXX(target) Release/obj.target/bindings/src/serialport.o CXX(target) Release/obj.target/bindings/src/serialport_unix.o CXX(target) Release/obj.target/bindings/src/poller.o CXX(target) Release/obj.target/bindings/src/serialport_linux.o SOLINK_MODULE(target) Release/obj.target/bindings.node COPY Release/bindings.node make: Leaving directory '/opt/zigbee2mqtt/node_modules/zigbee-herdsman/ node_modules/@serialport/bindings/build' > serialport@8.0.1 postinstall /opt/zigbee2mqtt/node_modules/zigbee-herdsman/ node_modules/serialport > node thank-you.js Thank you for using serialport! If you rely on this package, please consider supporting our open collective: > https://opencollective.com/serialport/donate npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/ zigbee-herdsman/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/ fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"}) added 1356 packages from 856 contributors and audited 877179 packages in 220.282s found 3 high severity vulnerabilities run `npm audit fix` to fix them, or `npm audit` for details
  • Fix the mentioned issues
    pi@racknode:/opt/zigbee2mqtt $ npm audit fix
    npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || ↵
    >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but ↵
    none is installed. You must install peer dependencies yourself.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/↵
    zigbee-herdsman/node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: ↵
    wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: ↵
    wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"})
    
    updated 2 packages in 52.752s
    fixed 3 of 3 vulnerabilities in 877319 scanned packages
    pi@racknode:/opt/zigbee2mqtt $
    
  • Enter your configuration in /opt/zigbee2mqtt/data/configuration.yaml
    vi data/configuration.yaml
    Enter your settings 
    # MQTT settings
    mqtt:
      # MQTT base topic for zigbee2mqtt MQTT messages
      base_topic: zigbee2mqtt
      # MQTT server URL
      server: 'mqtt://localhost'
      # MQTT server authentication, uncomment if required:
      # user: my_user
      # password: my_password
    # CC2590 is connected to serial line
    serial:
      port: /dev/ttyAMA0
    advanced:
      baudrate: 115200
      rtscts: false 
    
  • Start zigbee2mqtt
    pi@racknode:/opt/zigbee2mqtt $ npm start
    
    > zigbee2mqtt@1.7.1 start /opt/zigbee2mqtt
    > node index.js
    
    Logging to directory: '/opt/zigbee2mqtt/data/log/2019-11-30.16-53-43'
    Starting zigbee2mqtt version 1.7.1 (commit #b459c35)
    Starting zigbee-herdsman...
    zigbee-herdsman started
    Coordinator firmware version: '{"type":"zStack12","meta":{"transportrev":2,↵
    "product":0,"majorrel":2,"minorrel":6,"maintrel":3,"revision":20190608}}'
    Currently 0 devices are joined:
    `permit_join` set to  `true` in configuration.yaml.
    Allowing new devices to join.
    Set `permit_join` to `false` once you joined all devices.
    Zigbee: allowing new devices to join.
    Connecting to MQTT server at mqtt://xxx.xxx.xxx
    Connected to MQTT server
    MQTT publish: topic 'zigbee/bridge/state', payload 'online'
    MQTT publish: topic 'zigbee/bridge/config', payload '{"version":"1.7.1",↵
    "commit":"b459c35","coordinator":{"type":"zStack12","meta":{"transportrev":2,↵
    "product":0,"majorrel":2,"minorrel":6,"maintrel":3,"revision":20190608}},↵
    "log_level":"info","permit_join":true}'

Setup a daemon to run zigbee2mqtt

based on zigbee2mqtt - 5. (Optional) Running as a daemon with systemctl
  • Create a zigbee2mqtt service
    sudo vi /etc/systemd/system/zigbee2mqtt.service
    enter
    [Unit]
    Description=zigbee2mqtt
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/npm start
    WorkingDirectory=/opt/zigbee2mqtt
    StandardOutput=inherit
    StandardError=inherit
    Restart=always
    User=pi
    
    [Install]
    WantedBy=multi-user.target
    
  • Verify that it works
    pi@racknode:/opt/zigbee2mqtt $ sudo systemctl start zigbee2mqtt
    pi@racknode:/opt/zigbee2mqtt $ sudo systemctl status zigbee2mqtt
    ● zigbee2mqtt.service - zigbee2mqtt
       Loaded: loaded (/etc/systemd/system/zigbee2mqtt.service; disabled; ↵
       vendor preset: enabled)
  •    Active: active (running) since Tue 2019-12-03 08:35:46 CET; 7s ago
     Main PID: 626 (npm)
        Tasks: 23 (limit: 4915)
       CGroup: /system.slice/zigbee2mqtt.service
               ├─626 npm
               ├─643 sh -c node index.js
               └─644 node index.js
    
    Dec 03 08:35:46 racknode systemd[1]: Started zigbee2mqtt.
    Dec 03 08:35:50 racknode npm[626]: > zigbee2mqtt@1.7.1 start /opt/zigbee2mqtt
    Dec 03 08:35:50 racknode npm[626]: > node index.js
    Dec 03 08:35:53 racknode npm[626]: [..] Logging to directory: '/opt/zigb
    Dec 03 08:35:53 racknode npm[626]: [..] Starting zigbee2mqtt version 1.7
    Dec 03 08:35:53 racknode npm[626]: [..] Starting zigbee-herdsman...
  • Configure the autostart of the service
    pi@racknode:/opt/zigbee2mqtt $ sudo systemctl enable zigbee2mqtt.service
    Created symlink /etc/systemd/system/multi-user.target.wants/zigbee2mqtt.service ↵
    → /etc/systemd/system/zigbee2mqtt.service.
  • Reboot the Raspberry to verify your setup
    pi@racknode:/opt/zigbee2mqtt $ sudo reboot
    Connection to racknode closed by remote host.
    Connection to racknode closed.
    
    $ ssh pi@racknode
    Linux racknode 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Tue Dec  3 08:42:26 2019 from 10.0.0.171
    pi@racknode:~ $ sudo journalctl -u zigbee2mqtt.service -f
    -- Logs begin at Thu 2016-11-03 18:16:43 CET. --
    [..] Started zigbee2mqtt.
    [..] > zigbee2mqtt@1.7.1 start /opt/zigbee2mqtt
    [..] > node index.js
    [..] Logging to directory: '/opt/zigbee2mqtt/data/log/2019-12-03.08-44-14'
    [..] Starting zigbee2mqtt version 1.7.1 (commit #b459c35)
    [..] Starting zigbee-herdsman...
    [..] zigbee-herdsman started
    [..] Coordinator firmware version: '{"type":"zStack12","meta":{"transportrev":2,↵
         "product":0,"majorrel":2,"minorrel":6,"maintrel":3,"revision":20190608}}'
    [..] Currently 1 devices are joined:
    [..] 0x00158d00029acb06 (0x00158d00029acb06): MFKZQ01LM - Xiaomi Mi/Aqara smart ↵
         home cube (EndDevice)
    [..] `permit_join` set to  `true` in configuration.yaml.
    [..] Allowing new devices to join.
    [..] Set `permit_join` to `false` once you joined all devices.
    [..] Zigbee: allowing new devices to join.
    [..] Connecting to MQTT server at mqtt://mqtt.thomo.de
    [..] Connected to MQTT server
    [..] MQTT publish: topic 'zigbee/bridge/state', payload 'online'
    [..] MQTT publish: topic 'zigbee/0x00158d00029acb06', payload '{"battery":31,↵
         "voltage":2825,"linkquality":18,"angle":10.55,"side":3}'
    [..] MQTT publish: topic 'zigbee/bridge/config', payload '{"version":"1.7.1",↵
         "commit":"b459c35","coordinator":{"type":"zStack12","meta":{"transportrev":2,↵
         "product":0,"majorrel":2,"minorrel":6,"maintrel":3,"revision":20190608}},↵
         "log_level":"info","permit_join":true}'
    ^C
    pi@racknode:~ $

Links

  • https://www.zigbee2mqtt.io/

Update

2020-02-15: Fix error in flash connection table and picture.

12 Kommentare:

  1. Hi, it is very nice post, thank you very much, may I confirm your device is cc2530? because your document some part still cc2531

    AntwortenLöschen
    Antworten
    1. Hello, thank you for your feedback I have corrected it. However, I have only identified one place where I incorrectly referred to the CC2531. The git repo has the name flash_cc2531 and thus the directory when cloned - this is confusing but true.

      Löschen
  2. Hello, the pin pairing for flashing the device is wrong!
    DC Pin38 P14 Pin04
    DD Pin36 P14 Pin05

    The order should be reversed: DC/P14Pin04 goes to Pin36 and DD/P14Pin05 goes to Pin38

    Thanks

    AntwortenLöschen
    Antworten
    1. You are right, many thanks. I correct the table and the picture. Luckily i had also taken a photo, so that i could see the mistake.

      Löschen
  3. Hello

    Any idea why flashing stop when trying to flash ?

    /flash_cc2531# ./cc_erase
    ID = a524.
    erase result = 00a2.

    /flash_cc2531# ./cc_write CC2530ZNP-Prod.hex
    ID = a524.
    reading line 15490.
    file loaded (15497 lines read).
    writing page 1/128.

    AntwortenLöschen
    Antworten
    1. May be it is a power supply issue. Did you try another power supply?

      Löschen
  4. Great tutor!! this save me lot of time. Thanks for your time to sharing this.

    AntwortenLöschen
  5. Bei Verwendung eines (alten) Raspberry Pi 1B sind für das Flashen der Firmware gegenüber der Anleitung einige Änderungen vorzunehmen:

    Der RPi 1B hat nicht so viele IO-Pins; es müssen also andere Pins verwendet werden, z.B.:

    Name Raspi CC2530+CC2591
    ------------------------------
    3,3V Pin01 P12 Pin02
    GND Pin14 P12 Pin01
    RESET Pin03 P12 Pin03
    DC Pin11 P14 Pin04
    DD Pin13 P14 Pin05

    Bei der Benutzung des Flash-Tools mit abweichender Pinbelegung sind diese als Parameter anzugeben (-r -c -d); der Parameter "-m 300" dient der Vergrößerung des Delays bei langsamen Prozessoren

    ./cc_chipid -m 300 -r 8 -c 0 -d 2
    ./cc_erase -m 300 -r 8 -c 0 -d 2
    ./cc_write -m 300 -r 8 -c 0 -d 2 CC2531ZNP-Prod.hex

    AntwortenLöschen
  6. Thank you for putting these steps together! This helped me a ton! Great content in your blog, keep-up the good work!

    AntwortenLöschen
  7. Hello

    When I do ./cc_erase I got erase result = 00a2 is that ok?
    Thanks

    AntwortenLöschen
    Antworten
    1. I would go on - may be the value is device revision specific.

      Löschen