Skip to content

Instantly share code, notes, and snippets.

@lluisd
Last active February 3, 2024 21:43
Show Gist options
  • Save lluisd/b3a7c8922599a09136e4856cab1ccc9c to your computer and use it in GitHub Desktop.
Save lluisd/b3a7c8922599a09136e4856cab1ccc9c to your computer and use it in GitHub Desktop.
Sonoff zigbee3.0 + Synology

antenanas

Part 1: Sonoff zigbee 3.0 usb dongle

Material

  1. Sonoff zigbee3.0 - P (https://www.amazon.es/dp/B09KXTCMSC)

Flashing firmware

Download

  1. Coordinator firmware here from https://www.zigbee2mqtt.io/guide/adapters/#recommended

image

  1. ZigStar GW Multi tool here
  2. Sonoff dongle usb driver here

Flashing Sonoff dongle

  1. Unzip all 3 files.
  2. Install the usb driver by right click over silabser file and click install.
  3. Disassemble Sonoff dongle by unscrewing the 2 screws from the antenna side.
  4. Open the ZigStar GW Multi tool .
  5. Hold the 'boot' button from Sonoff dongle (the one closer the the usb connector) meanwhile you connect it to your computer. Unhold when plugged.
  6. Search for the COM port of the Sonoff dongle in Zigstar tool, upload the coordinator firmware already downloaded and check the ERASE, WRITE and Verify checkboxes and click Start and wait until is firmware is uploaded.

Install drivers in the Synology (with ssh)

Install usb drivers

Run

/sbin/modprobe usbserial
/sbin/modprobe ftdi_sio
/sbin/modprobe cdc-acm

Search for you CPU model

By running uname -a or checking it in What kind of CPU does my NAS have

Download and install the driver

  1. Go to https://github.com/robertklep/dsm7-usb-serial-drivers and download the cp210x driver from the modules/ folder. For example in my case a DS920+ is a

, so it the file will be located here: https://github.com/robertklep/dsm7-usb-serial-drivers/blob/main/modules/geminilake/dsm-7.2/cp210x.ko

  1. Copy the link from step 1 but replace the subfolder /blob/ by /raw/ to have the link that will download the binary file (raw file) and not the html page. In that example will be: https://github.com/robertklep/dsm7-usb-serial-drivers/raw/main/modules/geminilake/dsm-7.2/cp210x.ko

  2. Go to /lib/modules folder from ssh: cd /lib/modules

  3. Run: wget https://github.com/robertklep/dsm7-usb-serial-drivers/raw/main/modules/geminilake/dsm-7.2/cp210x.ko to download the driver to the current /lib/modules folder. You can also run cat cp210x.ko to check it doesn't print HTML code and it's an unreadable binary file.

WRONG file:

image

CORRECT file:

image

Load drivers when rebooting your NAS (with ssh)

Now we download a script so that it always loads the driver when starting the system.

  1. Move to cd /usr/local/etc/rc.d.
  2. Download the script with wget https://github.com/robertklep/dsm7-usb-serial-drivers/raw/main/usb-serial-drivers.sh.
  3. Give execute permission to the script chmod +x /usr/local/etc/rc.d/usb-serial-drivers.sh.
  4. Plug your Sonoff dongle if is not already connected and run the script ./usb-serial-drivers.sh start.
  5. Check if it's detected by running: ls /dev/ttyU* which it will show /dev/ttyUSB0 if it's detected.

image

Part 2: Zigbee2MQTT with MQTT broker

Docker (with Portainer) for MQTT

  1. Create synology volume folders for the mqtt docker
/volume1/docker/mosquitto/config
/volume1/docker/mosquitto/data
/volume1/docker/mosquitto/log
  1. Create the file mosquitto.conf in the /volume1/docker/mosquitto/config folder already created before
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
allow_anonymous false
listener 1883
  1. docker-compose for the mqtt container to copy-paste to your new stack (in Portainer) and deploy it:
version: '3.8'
services:
 mqtt:
  container_name: mqtt
  image: eclipse-mosquitto:latest
  restart: always
  volumes:
   - "/volume1/docker/mosquitto/config:/mosquitto/config"
   - "/volume1/docker/mosquitto/data:/mosquitto/data"
   - "/volume1/docker/mosquitto/log:/mosquitto/log"
  ports:
   - "1883:1883"
   - "9001:9001"
  network_mode: host
  1. When docker is running, connect to the mqtt docker bash/console to create a user ('lluis' in this example) with password hashed in a file:
>> mosquitto_passwd -c /mosquitto/config/password.txt lluis

and then you will see a password.txt created in the same directory where you created the mosquitto.conf

image

  1. Modify the mosquitto.conf from step 2 with this (is the same but adding the password_file in last line):
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
allow_anonymous false
listener 1883
password_file /mosquitto/config/password.txt
  1. Restart mqtt container to apply the new config

Docker (with Portainer) for zigbee2mqtt

  1. Create synology volume folder for the zigbee2mqtt docker
/volume1/docker/zigbee2mqtt
  1. docker-compose for the zigbee2mqtt container to copy-paste to your new stack (in Portainer) and deploy it:
version: '3.8'
services:
 zigbee2mqtt:
  container_name: zigbee2mqtt
  restart: always
  image: koenkk/zigbee2mqtt:latest
  volumes:
   - /volume1/docker/zigbee2mqtt:/app/data
   - /run/udev:/run/udev:ro
  ports:
   - 8020:8020
  environment:
   - TZ=Europe/Madrid
  devices:
   - /dev/ttyUSB0:/dev/ttyUSB0

(In my case I use port 8020 for the zigbee2mqtt web interface)

  1. Go to /volume1/docker/zigbee2mqtt and edit the configuration.yaml (easy way is to install synology text editor app)
homeassistant: true
permit_join: false

mqtt:
 base_topic: zigbee2mqtt
 server: mqtt://192.168.0.160 #your NAS ip
 keepalive: 60 
 password: (mqtt password)
 reject_unauthorized: true
 user: (mqtt user)
serial:
 port: /dev/ttyUSB0 #dongle usb sonoff
frontend:
 port: 8020 #zigbee2mqtt web interface port
  1. Check http://19.168.0.160:8020 is working.
  2. Check zigbee2mqtt container logs to ensure all is working fine:

image

Install MQTT integration in Home Assistant:

  1. Go to Settings - Integrations - Add integration and search for MQTT

image

  1. Use 127.0.0.1 as Broker adress and 1883 as Port and finish.

Connect a new zigbee device

  1. Go to http://19.168.0.160:8020 (zigbee2mqtt)

  2. Press 'Allow join (All)' button. This wil set to TRUE the permit_join property of the /volume1/docker/zigbee2mqtt/configuration.yaml

image

  1. Press 5 seconds your pairing button of your zigbee device (a sensor, a cover...) and in few seconds it will appear in the list

image

  1. Go to Home Assistant and it will be show in the settings - Integrations - mqtt:

image

Troubleshooting

Security Advisor app

Synology Security Advisor app it can warn to you that some unkown script is detected. Omit it because if you Resolve it by Security Advisor then the usb-serial-drivers.sh start content will be commented with a # in each line and Sonff dongle won't work during a reboot. Also it can remove the execute permission, so you will need to give it again by chmod +x /usr/local/etc/rc.d/usb-serial-drivers.sh

Security Advisor app:

image

Commented script:

image

You can always check with cat /usr/local/etc/rc.d/usb-serial-drivers.sh if all the script is commented and undo it:

  1. open the editor sudo vi usb-serial-drivers.sh.
  2. press i to INSERT text.
  3. Go line by line to delete only one # character per line (take into account that first line has ##!bin/sh and needs to be #!bin/sh).
  4. press ESC.
  5. write :wq!.
  6. press ENTER.
  7. Check by cat usb-serial-drivers.sh that file its again ok.

USB3 inteferences (lost mqtt messages / no communication to your zigbee devices)

Use an usb extender cable to put the zigbee dongle usb far from your NAS and also from cables like the ones that go to an external dock HDD. https://youtu.be/tHqZhNcFEvA

DSM updated and zigbee2mqtt container not able to start.

Check if 'ls /dev/ttyU*' shows the sonoff driver and the cp210x.ko file is still in '/lib/modules', if not download it again from the step "Download and install the driver" and start it again.

@huesofredy
Copy link

thank you lluis for this manual and for your time.

@avdotion
Copy link

avdotion commented Feb 3, 2024

Thank you a lot, lluis!

There are three major notes about E version:

  1. There is no "special" firmware or something, just install a latest image in the one-button website.
  2. It shows /dev/ttyACM0 instead of /dev/ttyUSB0, it is OK.
  3. Do not forget adapter: ezsp in zigbee2mqtt config serial section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment