DYI Zigbee
I want to water the lawn during the night, sure there are plenty of existing systems that cost like $20 that will do it for me but what is the fun in that ? No instead let's see if I can spent $100 to be able control a solenoid valve using my home server.
Hardware requirements
Installation
First let's get a modern version of Python.
~$ git clone https://github.com/pyenv/pyenv.git
~# apt install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
~/pyenv$ src/configure && make -C src
~$ echo 'export PATH="/home/${USER}/.local/bin:/home/${USER}/.pyenv/shims:${PATH}"' > ~/.bashrc
~$ mkdir -p ~/.local/bin
~$ ln -s /home/${USER}/pyenv/bin/pyenv /home/${USER}/.local/bin/pyenv
~$ source ~/.bashrc
~$ pyenv install 3.9.6
~$ pyenv global 3.9.6
~$ pip install pipenv
Okay done with the snake, we need an mqtt broker and zigbee2mqtt
~# apt install mosquitto
~# echo "listener 1883 127.0.0.1" >> /etc/mosquitto/mosquitto.conf
~# service mosquitto restart
~$ git clone https://github.com/Koenkk/zigbee2mqtt.git
~/zigbee2mqtt$ pipenv install nodeenv
~/zigbee2mqtt$ pipenv run nodeenv -p -n 16.4.2
~/zigbee2mqtt$ pipenv run npm ci
We got everything, let's check which device the control is mapped to and configure zigbee2mqtt accordingly
~$ ls /dev/tty[AU]*
/dev/ttyUSB0
~/zigbee2mqtt$ sed -i 's/ttyACM0/ttyUSB0/g' data/configuration.yaml
~# chmod 666 /dev/ttyUSB0
Let's start to test it out and give a friendly name to our device
~/zigbee2mqtt$ pipenv run npm start
...
Zigbee2MQTT:info 2021-07-11 02:53:10: Successfully configured '0x00124b0022ee4a71'
~/zigbee2mqtt$ sed -i 's/friendly_name: \'0x00124b0022ee4a71\'/friendly_name: \'lawn_valve\'/g' data/configuration.yaml
Tell systemd to make sure zigbee2mqtt always runs by adding a service in /etc/systemd/system/zigbee2mqtt.service (make sure to replace your username)
[Unit]
Description=zigbee2mqtt
After=network.target
[Service]
ExecStart=/bin/bash -c 'pipenv run npm start'
WorkingDirectory=/home/dorian/zigbee2mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=dorian
[Install]
WantedBy=multi-user.target
and enabling it
~# systemctl enable zigbee2mqtt.service
~# systemctl start zigbee2mqtt.service
And voila ! Now we can send an mqtt message to start watering the lawn.