seaky / nspanel_pro_tools_apk

283 stars 4 forks source link

Zigbee Gateway Sensor integration #11

Open jacob3141 opened 1 year ago

jacob3141 commented 1 year ago

I found that HA is able to recognize the connected zigbee devices when using the SONOFF cloud integration (from HACS) and propose them to HA that way. This is especially useful because I want to control room temperatures by reading temperature zigbee devices and activate heaters when too could or maybe roll down the shutters when too warm.

I noticed the zigbee devices report values to HA only when the ewelink app is running in the foreground (the one usually set up as the default launcher). In the second it is being moved to the background the reporting stops working.

It would be cool to have a zigbee gateway control to pair devices and also propose them to HA. Although I am a developer myself, I have only very little knowledge in that field. But I'm happy to support where I can.

jacob3141 commented 1 year ago

So I dived in a little and after browser through adb shell I found zgateway running in top. When doing a find on / it quickly show there is a zgateway application in

px30_evb:/vendor/bin/siliconlabs_host

decorated with a couple of supervisor scripts

px30_evb:/vendor/bin/siliconlabs_host # ls
Readme.txt       libcrypto.so.1.1  libssl.so.1.1 mosquitto.conf     mosquitto_pub zgateway
factory_shell.sh libfreezgb.so     mosquitto     mosquitto.password mosquitto_sub
guard_process.sh libmosquitto.so.1 mosquitto.acl mosquitto_passwd   run.sh

I have translated the chinese Readme which is describing some process of factory testing the zigbee module.

guard_process.sh reveals

echo "-------------Zigbee guard_process run-------------------------"

export LD_LIBRARY_PATH=/vendor/bin/siliconlabs_host/:${LD_LIBRARY_PATH}
echo ${LD_LIBRARY_PATH}

Z3GatewayHost_sonoff_go() {
        echo "do Z3GatewayHost_sonoff_go"

        killall zgateway
        sleep 1

        # killall mosquitto
        # sleep 1

        echo $(pwd)
        cd /vendor/bin/siliconlabs_host/
        echo $(pwd)

        # sleep 1
        # /vendor/bin/siliconlabs_host/mosquitto -c /vendor/bin/siliconlabs_host/mosquitto.conf &

        sleep 3

        #在终端打印整个运行日志
        /vendor/bin/siliconlabs_host/zgateway &
}

check() {
    echo "do check"

    local Z3GatewayHost_sonoffNum=`ps -A | grep -w 'zgateway' | grep -v "grep" | wc -l`
        # local mosquittoNum=`ps -A | grep -w 'mosquitto' | grep -v "grep" | wc -l`

    echo "Z3GatewayHost_sonoffNum:"${Z3GatewayHost_sonoffNum}

    if [ ${Z3GatewayHost_sonoffNum} -ne 1 ]; then
        echo "Z3GatewayHost_sonoffNum restart `date +"%Y-%m-%d %H:%M:%S"`" >> /data/vendor/siliconlabs_host/zgatewayStatus

        Z3GatewayHost_sonoff_go
    fi

}

while true
do
    sleep 60
    check
done

There is a logfile which also shows my zigbee temperature/humidity sensor sending data in

/data/vendor/siliconlabs_host/zgateway.log

jacob3141 commented 1 year ago

run.sh revelas someone tried to start and stop mosquitto within the guard_process.sh, but later commented it out in the final implementation.

px30_evb:/vendor/bin/siliconlabs_host # cat run.sh
#!/bin/bash

echo "-------------Zigbee Host run-------------------------"
LOCAL_TIME=$(date +"%Y-%m-%d %H:%M:%S")
echo $LOCAL_TIME 'Zigbee Host, start' > /vendor/run.log

export LD_LIBRARY_PATH=/vendor/bin/siliconlabs_host/:${LD_LIBRARY_PATH}
echo ${LD_LIBRARY_PATH}

#检查是否需要拷贝
if [ ! -d "/data/vendor/siliconlabs_host" ]; then
        echo "cp oem..."
        mkdir /data/vendor/siliconlabs_host
        chmod 777 /data/vendor/siliconlabs_host
        echo $LOCAL_TIME 'Zigbee Host, start' > /data/vendor/run.log
        # cp -r /vendor/bin/siliconlabs_host/* /oem/siliconlabs_host/
else
        echo "file exist"
fi

# sleep 1
# echo "copy libfreezgb.so"
# cp -r /vendor/bin/siliconlabs_host/libfreezgb.so /vendor/lib
# cp -r /vendor/bin/siliconlabs_host/libfreezgb.so /system/lib
# echo "copy libfreezgb.so"
# cp -r /vendor/bin/siliconlabs_host/libmosquitto.so.1 /vendor/lib
# cp -r /vendor/bin/siliconlabs_host/libmosquitto.so.1 /system/lib

# sleep 1

killall zgateway
# sleep 1
killall guard_process
killall mosquitto
sleep 1

echo $(pwd)
cd /vendor/bin/siliconlabs_host/
echo $(pwd)

#需要先启动MQTT 服务
/vendor/bin/siliconlabs_host/mosquitto -c /vendor/bin/siliconlabs_host/mosquitto.conf &

sleep 3

#在终端打印整个运行日志
/vendor/bin/siliconlabs_host/zgateway &

sh /vendor/bin/siliconlabs_host/guard_process.sh &

This is the mosquitto.conf

px30_evb:/vendor/bin/siliconlabs_host # cat mosquitto.conf
per_listener_settings false
allow_zero_length_clientid false
check_retain_source false
max_inflight_messages 20
max_keepalive 60
persistent_client_expiration 1w
retain_available true
sys_interval 3600
#listener 1883 127.0.0.1
listener 1883

#persistence false

user root

allow_anonymous true
#password_file /oem/siliconlabs_host/mosquitto.password
#acl_file /oem/siliconlabs_host/mosquitto.acl

#log_dest none
#log_type error
#log_type warning
#log_type notice
#log_type information
#connection_messages true
#log_timestamp true
#log_timestamp_format %Y-%m-%dT%H:%M:%S
#max_keepalive 5
#sys_interval 10

I don't now much about mosquitto, but the documentation reagarding the authentication method for listeners can be found here: https://mosquitto.org/documentation/authentication-methods/

I wasn't sure about the 127.0.0.1 line (comment) which looks like the developer tried to have it listen only on 127.0.0.1, but checking the open ports show 1883 is open publicly:

tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN
tcp        1      0 192.168.0.41:48068      192.168.0.64:8123       CLOSE_WAIT
tcp        1      0 192.168.0.41:48060      192.168.0.64:8123       CLOSE_WAIT
tcp       32      0 192.168.0.41:58170      37.252.171.85:443       CLOSE_WAIT
tcp        0      0 192.168.0.41:55486      146.75.117.131:443      ESTABLISHED
tcp        0      0 127.0.0.1:35176         127.0.0.1:1883          ESTABLISHED
tcp       32      0 192.168.0.41:58172      37.252.171.85:443       CLOSE_WAIT
tcp        0      0 192.168.0.41:33818      23.35.236.188:443       ESTABLISHED
tcp        0      0 192.168.0.41:55310      146.75.118.217:443      ESTABLISHED
tcp        1      0 192.168.0.41:48064      192.168.0.64:8123       CLOSE_WAIT
tcp        1      0 192.168.0.41:48070      192.168.0.64:8123       CLOSE_WAIT
tcp        1      0 192.168.0.41:48066      192.168.0.64:8123       CLOSE_WAIT
tcp        1      0 192.168.0.41:48062      192.168.0.64:8123       CLOSE_WAIT
tcp        1      0 192.168.0.41:34092      130.248.173.59:443      CLOSE_WAIT
tcp        0      0 127.0.0.1:1883          127.0.0.1:35176         ESTABLISHED
tcp        0      0 192.168.0.41:59952      104.87.143.22:443       ESTABLISHED
tcp6       0      0 :::1883                 :::*                    LISTEN
tcp6       0      0 :::5555                 :::*                    LISTEN
tcp6       0      0 2a02:908:1c18:e9a:55520 2a03:8180:1c01:2f::5938 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:45312 2606:4700::6813:bb6:443 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:34530 2606:4700:10::6816::443 ESTABLISHED
tcp6       0      0 ::ffff:192.168.0.4:5555 ::ffff:192.168.0.:54770 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:45314 2606:4700::6813:bb6:443 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:41320 2a00:11c0:31:351:2:5938 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:41674 2606:4700:20::681a::443 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:47460 2606:4700::6810:3f1:443 ESTABLISHED
tcp6       0      0 ::ffff:192.168.0.:48012 ::ffff:192.168.0.6:8123 ESTABLISHED
tcp6       0      0 2a02:908:1c18:e9a:54160 2606:4700:4400::ac4:443 ESTABLISHED
tcp6       0   1350 ::ffff:192.168.0.4:5555 ::ffff:192.168.0.:60754 ESTABLISHED
udp        0      0 0.0.0.0:37489           0.0.0.0:*
udp        0      0 0.0.0.0:47800           0.0.0.0:*
udp     2560      0 192.168.0.41:68         192.168.0.10:67         ESTABLISHED
udp        0      0 0.0.0.0:68              0.0.0.0:*
udp        0      0 0.0.0.0:5353            0.0.0.0:*
udp6       0      0 :::41789                :::*
udp6       0      0 :::5353                 :::*
udp6       0      0 :::5353                 :::*

So I downloaded MQTTX and tried to connect from outside via 192.168.0.41:1883, and it connected. So the MQTT broker is available from outside. I subscribed to the zigbee topic and this is what I got.

grafik

seaky commented 1 year ago

Hi,

Currently, I’m waiting for my zigbee devices but I’ll check the ControlApp what is going under the hood. I’ll inform you!

thx, Robi

acseven commented 8 months ago

I'm trying to understand if it's possible to handle zigbee devices connected to a NSPanel Pro in Home Assistant, without ewelink.

In particular, I stumbled upon these posts on Twitter/X, does this mean that it is possible?

Guess what else works in NSPanel Pro!?!?!?!?!?!?!?!?!?!?

ZHA friggin works and by proxy Zigbee2MQTT should work too!

  1. killall Z3GatewayHost_sonoff
  2. copy https://github.com/jakev/android-binaries/blob/master/socat to android
  3. ./socat -d -d -d -d tcp-listen:8889,reuseaddr,fork file:/dev/ttyS5,b115200,raw,echo=0

btw you need to be su before all that

https://twitter.com/blakadder_/status/1574487984631693334