N1 或 树莓派 上 裸机安装zigbee2mqtt(即不是基于docker方式的安装)

背景

  • N1 或 树莓派 上 裸机安装zigbee2mqtt(即不是基于docker方式的安装)

需要

  • 提前安装好git
  • 提前安装好nodejs 和 npm

步骤

  1. 我的本机是已经安装过nodejs 和 npm ,确认一下
node --version  # Should output v12.X or v10.X
npm --version  # Should output 6.X
  1. git 方式下载代码
git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
  1. 编译,注意我使用的是root账号,如果不是,可能有文件夹权限的问题,参考官方指导文档
cd /opt/zigbee2mqtt
npm ci --unsafe-perm

特别注意要加上–unsafe-perm,否则会报没权限的错误

编译打印

root@aml:/opt/zigbee2mqtt# npm ci --unsafe-perm
npm WARN prepare removing existing node_modules/ before installation

> @serialport/bindings@8.0.7 install /opt/zigbee2mqtt/node_modules/zigbee-herdsman/node_modules/@serialport/bindings
> prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild

prebuild-install WARN install No prebuilt binaries found (target=10.19.0 runtime=node arch=arm64 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
../src/serialport_unix.cpp: In function 'int setup(int, OpenBaton*)':
../src/serialport_unix.cpp:166:5: warning: '%s' directive output may be truncated writing up to 1023 bytes into a region of size 1005 [-Wformat-truncation=]
 int setup(int fd, OpenBaton *data) {
     ^~~~~
In file included from /usr/include/stdio.h:862:0,
                 from ../src/serialport.h:3,
                 from ../src/serialport_unix.cpp:2:
/usr/include/aarch64-linux-gnu/bits/stdio2.h:65:44: note: '__builtin___snprintf_chk' output 20 or more bytes (assuming 1043) into a destination of size 1024
        __bos (__s), __fmt, __va_arg_pack ());
                                            ^
../src/serialport_unix.cpp: In function 'void EIO_Open(uv_work_t*)':
../src/serialport_unix.cpp:79:6: warning: '%s' directive output may be truncated writing up to 1023 bytes into a region of size 1003 [-Wformat-truncation=]
 void EIO_Open(uv_work_t* req) {
      ^~~~~~~~
In file included from /usr/include/stdio.h:862:0,
                 from ../src/serialport.h:3,
                 from ../src/serialport_unix.cpp:2:
/usr/include/aarch64-linux-gnu/bits/stdio2.h:65:44: note: '__builtin___snprintf_chk' output 22 or more bytes (assuming 1045) into a destination of size 1024
        __bos (__s), __fmt, __va_arg_pack ());
                                            ^
  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.6 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


added 851 packages in 76.896s

有些警告可以忽略,不影响使用。

  1. 配置网关的网络
    zigbee2mqtt开车网关到手后,这样干!

  2. 修改配置

Open the configuration file:

nano /opt/zigbee2mqtt/data/configuration.yaml

# Home Assistant integration (MQTT discovery)
homeassistant: false

# allow new devices to join
permit_join: true

# MQTT settings
mqtt:
  # MQTT base topic for zigbee2mqtt MQTT messages
  base_topic: zigbee2mqtt
  # MQTT server URL
  server: 'mqtt://192.168.1.108'
  # MQTT server authentication, uncomment if required:
  user: mqtt
  password: mqtt

# Serial settings
serial:
  # Location of CC2531 USB sniffer
  port: 'tcp://192.168.1.120:1024'
  1. 启动
cd /opt/zigbee2mqtt
npm start

如果串口还没有接上网关设备,启动会报错

root@aml:/opt/zigbee2mqtt# npm start

> zigbee2mqtt@1.11.0 start /opt/zigbee2mqtt
> node index.js

zigbee2mqtt:info  2020-03-09 09:30:10: Logging to console and directory: '/opt/zigbee2mqtt/data/log/2020-03-09.09-30-09' filename: log.txt
zigbee2mqtt:info  2020-03-09 09:30:10: Starting zigbee2mqtt version 1.11.0 (commit #e140cf5)
zigbee2mqtt:info  2020-03-09 09:30:10: Starting zigbee-herdsman...
zigbee2mqtt:error 2020-03-09 09:30:11: Error while starting zigbee-herdsman
zigbee2mqtt:error 2020-03-09 09:30:11: Failed to start zigbee
zigbee2mqtt:error 2020-03-09 09:30:11: Exiting...
zigbee2mqtt:error 2020-03-09 09:30:11: Error: Error while opening serialport 'Error: Error: No such file or directory, cannot open /dev/ttyACM0'
    at Znp.<anonymous> (/opt/zigbee2mqtt/node_modules/zigbee-herdsman/dist/adapter/z-stack/znp/znp.js:113:32)
    at Generator.next (<anonymous>)
    at /opt/zigbee2mqtt/node_modules/zigbee-herdsman/dist/adapter/z-stack/znp/znp.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/opt/zigbee2mqtt/node_modules/zigbee-herdsman/dist/adapter/z-stack/znp/znp.js:4:12)
    at SerialPort.serialPort.open (/opt/zigbee2mqtt/node_modules/zigbee-herdsman/dist/adapter/z-stack/znp/znp.js:111:49)
    at SerialPort._error (/opt/zigbee2mqtt/node_modules/zigbee-herdsman/node_modules/@serialport/stream/lib/index.js:198:14)
    at binding.open.then.err (/opt/zigbee2mqtt/node_modules/zigbee-herdsman/node_modules/@serialport/stream/lib/index.js:242:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! zigbee2mqtt@1.11.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the zigbee2mqtt@1.11.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-03-09T01_30_11_115Z-debug.log

由于还没有网关设备,是验证不了是否可以启动成功的。

后来连接上网关(使用是是H大的mysensor zigbee网关)了

root@aml:/opt/zigbee2mqtt# npm start

> zigbee2mqtt@1.11.0 start /opt/zigbee2mqtt
> node index.js

zigbee2mqtt:info  2020-05-02 07:01:50: Logging to console and directory: '/opt/zigbee2mqtt/data/log/2020-05-02.07-01-50' filename: log.txt
zigbee2mqtt:info  2020-05-02 07:01:51: Starting zigbee2mqtt version 1.11.0 (commit #e140cf5)
zigbee2mqtt:info  2020-05-02 07:01:51: Starting zigbee-herdsman...
zigbee2mqtt:info  2020-05-02 07:02:41: zigbee-herdsman started
zigbee2mqtt:info  2020-05-02 07:02:41: Coordinator firmware version: '{"type":"zStack30x","meta":{"transportrev":2,"product":2,"majorrel":2,"minorrel":7,"maintrel":2,"revision":20200327}}'
zigbee2mqtt:info  2020-05-02 07:02:41: Currently 0 devices are joined:
zigbee2mqtt:warn  2020-05-02 07:02:41: `permit_join` set to  `true` in configuration.yaml.
zigbee2mqtt:warn  2020-05-02 07:02:41: Allowing new devices to join.
zigbee2mqtt:warn  2020-05-02 07:02:41: Set `permit_join` to `false` once you joined all devices.
zigbee2mqtt:info  2020-05-02 07:02:41: Zigbee: allowing new devices to join.
zigbee2mqtt:info  2020-05-02 07:02:41: Connecting to MQTT server at mqtt://192.168.1.108
zigbee2mqtt:info  2020-05-02 07:02:42: Connected to MQTT server
zigbee2mqtt:info  2020-05-02 07:02:42: MQTT publish: topic 'zigbee2mqtt/bridge/state', payload 'online'
zigbee2mqtt:info  2020-05-02 07:02:42: MQTT publish: topic 'zigbee2mqtt/bridge/config', payload '{"version":"1.11.0","commit":"e140cf5","coordinator":{"type":"zStack30x","meta":{"transportrev":2,"product":2,"majorrel":2,"minorrel":7,"maintrel":2,"revision":20200327}},"log_level":"info","permit_join":true}'

  1. 以服务运行
sudo nano /etc/systemd/system/zigbee2mqtt.service

[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
#开机启动
systemctl enable zigbee2mqtt

# Start zigbee2mqtt
systemctl start zigbee2mqtt

# Show status
systemctl status zigbee2mqtt

参考

  1. 官方指导文档