gtxaspec / wz_mini_hacks

wz camera mods... make your camera better.
1.31k stars 113 forks source link

MQTT Support for homeassistant integration #23

Closed ankurvdev closed 2 years ago

ankurvdev commented 2 years ago

What would it take to add MQTT support

Publishing and subscribing to MQTT bindings will make integration with Homeassistant really useful and eliminate need for frigate or any other host-based accelerated AI detection.

Looking at something similiar in Dafang-Hacks It seems they have a binary reading state from shared memory that fires off a shell script on motion detection

https://github.com/EliasKotlyar/Xiaomi-Dafang-Software/blob/1cfed9e1735c3bd376f86195318a9067161cb3d3/v4l2rtspserver-master/src/ImpEncoder.cpp#L723

gtxaspec commented 2 years ago

will need some help on this one...

Vendo232 commented 2 years ago

I vote for MQTT, it would be very cool feature which would turn V3 into sensor device.

gtxaspec commented 2 years ago

With the latest build, once you enable rtsp, cmd becomes available:

cmd waitmotion <timeout>

so, cmd waitmotion 1000 waits for motion, for 1000 seconds, then exits once motion is detected. it outputs the coordinates of the motion detected.

you could so something like:

#!/bin/sh

while true
do cmd waitmotion 1000
###Do whatever you want here
echo "Motion Detected"
cmd jpeg | tail -n +4 > /media/mmc/test.jpg
cmd aplay /usr/share/notify/binbin.wav 40
sleep 1
done

To get continuous output for use in a script. This example prints text, takes a high-quality photo and plays a chime sound on the camera.

Checkout the sources in libcallback if you want more info on how it works.

work in progress...

Vendo232 commented 2 years ago

here is an idea: Execute script which will play certain *.wav audio file using shell command. use case: When alarm is triggered play script "intruder" with intruder.wav or if cam is in garage play script "Garage is closing clear the space... "

in Home Assistant there is an option to execute scripts via shell_command function. It is possible to SSH to external shell prompts from Home Assistant. You can then SSH to any remote machine and perform commands.

in example execution of maintanance on remote IP:

`shell_command: raspi_maintenance: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_IP '~/sys_maintenance'

script: system_maintenance_raspi: alias: Raspberry Pi System Maintenance sequence: service: shell_command.raspi_maintenance`

then I can create simple automation to run this script when : alarm is triggered or garage is opened.

setting up the SSH rsa keys is a hassle and I was unable to do it. Wanted to read CPU temp from another Debian server and pull the data to Home Assistant.

How to setup SSH keys

https://siytek.com/home-assistant-shell/

will try it later this week on V3 and run audio_test.sh via SSH command from HA

!/bin/sh

cmd aplay /usr/share/notify/swap.wav 40

Matthew-Whitlock commented 2 years ago

A workaround to full MQTT support is to use the webhook trigger in homeassistant (which I think makes more sense anyway) - I figured I'd post it here, as it took a little tinkering:

#!/opt/wz_mini/bin/bash

while true
do
  ret=`cmd waitmotion 1000 | grep "timeout" | wc -l`

  if [ $ret -eq 0  ] ; then
    echo "Reporting motion!"
    wget --post-data="motion=1" http://your.homeassistant.com/api/webhook/your_webhook -O/dev/null 2>/dev/null
  else
    #optionally actively report timeouts
    echo "No motion found!"
  fi
done

An important note is that the version of wget provided doesn't support https, so you'll have to expose http access if you aren't now. The post-data isn't important, and you can send any number of var1=data&var2=data2 within, which can be accessed from homeassistant.

gtxaspec commented 2 years ago

i'll try to compile a static version of wget with https, i forgot about that.

gtxaspec commented 2 years ago

wget with https support will be included in next release!

gtxaspec commented 2 years ago

release out now: /opt/wz_mini/bin/wget