├── .gitignore ├── bin ├── mosquitto_pub └── mosquitto_sub ├── install.sh ├── mqtt.conf ├── readme.md └── scripts ├── control.sh ├── publisher.sh └── subscriber.sh /.gitignore: -------------------------------------------------------------------------------- 1 | mqtt/ 2 | yi-hack-mqtt.tar 3 | -------------------------------------------------------------------------------- /bin/mosquitto_pub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fbrinker/yi-hack-mqtt/2ebc3d3cbee4623e2be5cf092593d90213d1809f/bin/mosquitto_pub -------------------------------------------------------------------------------- /bin/mosquitto_sub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fbrinker/yi-hack-mqtt/2ebc3d3cbee4623e2be5cf092593d90213d1809f/bin/mosquitto_sub -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if ! grep -Fq "mqtt/" /home/yi-hack-v3/script/system.sh 4 | then 5 | 6 | echo " 7 | # MQTT Motion Publisher 8 | if [ -f \"/home/yi-hack-v3/mqtt/scripts/publisher.sh\" ]; then 9 | sh /home/yi-hack-v3/mqtt/scripts/publisher.sh & 10 | fi 11 | 12 | # MQTT Motion Subscriber 13 | if [ -f \"/home/yi-hack-v3/mqtt/scripts/subscriber.sh\" ]; then 14 | sh /home/yi-hack-v3/mqtt/scripts/subscriber.sh & 15 | fi 16 | " >> /home/yi-hack-v3/script/system.sh 17 | 18 | fi 19 | 20 | # Home Assistant Yi Camera Hack (doesn't hurt anyone I guess) 21 | ln -fs /tmp/ /root/tmp 22 | -------------------------------------------------------------------------------- /mqtt.conf: -------------------------------------------------------------------------------- 1 | # MQTT Settings 2 | MQTT_HOST=*your broker* 3 | MQTT_USER=*your username* 4 | MQTT_PASSWORD=*your password* 5 | 6 | MQTT_DEVICE_NAME=$(hostname) 7 | MQTT_TOPIC_PREFIX="/home/camera/$MQTT_DEVICE_NAME/motion" 8 | 9 | # Binaries 10 | MOSQUITTO_PUB_BIN=/home/yi-hack-v3/mqtt/bin/mosquitto_pub 11 | MOSQUITTO_SUB_BIN=/home/yi-hack-v3/mqtt/bin/mosquitto_sub 12 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # yi-hack-mqtt 2 | 3 | This projects adds a MQTT subscriber and publisher for motion detection to the [yi-hack-v3](https://github.com/shadow-1/yi-hack-v3) project. 4 | 5 | ## Requirements 6 | 7 | You need a camera with the [yi-hack-v3](https://github.com/shadow-1/yi-hack-v3) firmware. 8 | 9 | You have to **enable Motion Detection / Alarms for the camera via the Yi App**: 10 | Stopping the motion detection just kills some processes on the camera and won't change the settings in the App. Starting the motion detection again will just "revive" the killed processes. 11 | 12 | ## Setup 13 | 14 | * Login to your camera via telnet/ssh. The username should be `root` and the password isn't set by default. 15 | * Move into the home directory: `cd /home/yi-hack-v3` 16 | * Download & untar the project: `wget https://github.com/fbrinker/yi-hack-mqtt/releases/download/1/yi-hack-mqtt.tar && tar -xvf yi-hack-mqtt.tar && rm yi-hack-mqtt.tar && cd mqtt` 17 | * Edit the mqtt.conf and add your MQTT broker ip and login credentials: `vi mqtt.conf` 18 | * *Remember: To save & close vi, press ESC and enter `:wq` and press Return/Enter* 19 | * Run the install script `./install.sh` 20 | * Reboot the camera `reboot` 21 | 22 | ## Usage 23 | 24 | * The camera will publish MQTT messages to the topic `/home/camera/*hostname*/motion/state` (default) with the value `1` if motion is detected and `0` if the recording stopped again. 25 | * You can publish `start` to the topic `/home/camera/*hostname*/motion/control` (default) to start the motion detection and `stop` to stop it. 26 | 27 | So you can react to state changes (Alarms) via software like Home Assistant and it's MQTT sensors. This will also allow you to activate the motion detection if you leave the house and to deactivate it if you are back home. 28 | 29 | ## Thanks 30 | 31 | Special thanks to [dvv](https://github.com/dvv) for the [compiled mosquitto binaries](https://github.com/shadow-1/yi-hack-v3/issues/130). -------------------------------------------------------------------------------- /scripts/control.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source /home/yi-hack-v3/mqtt/mqtt.conf 3 | 4 | case $1 in 5 | start) 6 | echo "Starting motion detection..." 7 | /home/app/watch_process & 8 | ;; 9 | stop) 10 | echo "Stopping motion detection..." 11 | killall watch_process 12 | killall mp4record 13 | rm /tmp/sd/record/tmp.mp4.tmp 2> /dev/null 14 | ;; 15 | *) 16 | echo "Usage: control.h cmd - cmd can be start or stop" 17 | ;; 18 | esac 19 | -------------------------------------------------------------------------------- /scripts/publisher.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source /home/yi-hack-v3/mqtt/mqtt.conf 3 | 4 | # publish motion triggers via MQTT 5 | # $1 topic (state|photo|video) 6 | # $2 data 7 | publish() { 8 | echo "publishing... " 9 | case "$1" in 10 | state) 11 | MESSAGE="-m $2" 12 | ;; 13 | *) 14 | MESSAGE="-f $2" 15 | esac 16 | 17 | $MOSQUITTO_PUB_BIN -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASSWORD \ 18 | -t "$MQTT_TOPIC_PREFIX/$1" $MESSAGE 19 | echo "done" 20 | } 21 | 22 | # Application loop 23 | triggered=0 24 | while true; do 25 | publish_state=0 26 | publish_photo=0 27 | publish_video=0 28 | 29 | test -r /tmp/sd/record/tmp.mp4.tmp 30 | is_recording=$((1-$?)) 31 | 32 | if [ "$is_recording" != "$triggered" ]; then 33 | triggered="$is_recording" 34 | publish_state=1 35 | fi 36 | 37 | #if [ -r /tmp/motion.jpg -a ! -r /tmp/temp.jpg ]; then 38 | # cp /tmp/motion.jpg /tmp/temp.jpg 39 | # echo "found photo" 40 | # publish_photo=1 41 | #fi 42 | 43 | #if [ -r /tmp/motion.mp4 -a ! -r /tmp/temp.mp4 ]; then 44 | # cp /tmp/motion.mp4 /tmp/temp.mp4 45 | # echo "found video" 46 | # publish_video=1 47 | #fi 48 | 49 | if [ "$triggered" = 0 ]; then 50 | rm /tmp/temp.jpg /tmp/temp.mp4 2> /dev/null 51 | fi 52 | 53 | if [ "$publish_state" = 1 ]; then 54 | publish state $triggered 55 | fi 56 | 57 | #if [ "$publish_photo" = 1 ]; then 58 | # publish photo /tmp/temp.jpg 59 | #fi 60 | 61 | #if [ "$publish_video" = 1 ]; then 62 | # publish video /temp/temp.mp4 63 | #fi 64 | 65 | sleep 0.01 66 | done 67 | -------------------------------------------------------------------------------- /scripts/subscriber.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source /home/yi-hack-v3/mqtt/mqtt.conf 3 | 4 | # We have to delay the startup, otherwise the system is not ready to run a mqtt client yet 5 | sleep 20 6 | 7 | $MOSQUITTO_SUB_BIN -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASSWORD \ 8 | -I $MQTT_DEVICE_NAME -t "$MQTT_TOPIC_PREFIX/control" \ 9 | | while read RAW_CMD 10 | do 11 | /home/yi-hack-v3/mqtt/scripts/control.sh $RAW_CMD 12 | done 13 | --------------------------------------------------------------------------------