Skip to content

Instantly share code, notes, and snippets.

@jcconnell
Last active March 18, 2024 06:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcconnell/e4df5d71c68e67fd89b1d3573d762008 to your computer and use it in GitHub Desktop.
Save jcconnell/e4df5d71c68e67fd89b1d3573d762008 to your computer and use it in GitHub Desktop.
Yi Camera (22US) Info, Scripts & Links
#!/bin/sh
# Modified version of script from @airdrummingfool
# Sends JSON payload to Home Assistant to emulate MQTT
# Works with https://github.com/npetrovski/yi-hack-easy
#
# Description
# Every 20 seconds, check for files modified since we last checked
# if there is activity, it means motion was recently detected
# Since file writes begin immediately on motion detected, and don't end until approx 1min after motion ends, we only need to look for files modified very recently
# Relies on running mp4record process if armed variable is important to you
homeassistant_url="https://hass.example.com/api/services/mqtt/publish"
homeassistant_password="Example-HA-Password"
topic="homeassistant/example-ha-topic"
cd /home/hd1/record/ || exit
touch last_motion_check.txt
sleep 5 # since we /just/ created last_motion_check, the first check can return a false negative unless we wait a beat
while true; do
#echo "Checking for motion at `date`..."
has_motion=$([ -z "`find . -type f -name "*.mp4*" -newer last_motion_check.txt`" ] && echo "false" || echo "true")
echo `date '+%m-%d %I:%M%p'` > last_motion_check.txt
# is mp4record running?
armed=$(ps | grep mp4record | grep -v grep -q && echo "true" || echo "false")
# Start of motion notification
body=$(cat <<END
{"payload":"{\"camera\":\"$(hostname)\",\"motion\":\"$has_motion\",\"last_motion_check\":\"$(cat last_motion_check.txt)\",\"date\":\"$(date +%F)\",\"time\":\"$(date +%I:%M%p)\",\"armed\":\"$armed\"}","topic":"$topic","qos":"0","retain":"0"}
END
)
# Build Curl command
/home/curl --insecure -X POST -H "x-ha-access: $homeassistant_password" -H "Content-Type: application/json" --data "$body" "$homeassistant_url" --silent --show-error --stderr - -o /dev/null
ip=$(ip addr | grep 'ra0' | grep 'inet' | awk '{print $2}' | cut -f1 -d'/')
motion_file=$(find . -type f -name "*.mp4" -mmin -1 | tail -1)
touch /home/hd1/record/motion.html
[ -z "$motion_file" ] || echo $motion_file | sed "s/.\//record\//" | echo '<a href='$motion_file'>'$motion_file'</a>' > /home/hd1/record/motion.html
sleep 20
done
# Works Through telnet!
# Disable IR Illumination (Cuts down IR illumination)
/home/sendMq 0x10 1 0x1141 0
# Enable IR Illumination (Cut the IR illumination)
/home/sendMq 0x10 1 0x1140 1
# Enable IR Filter (IR cut filter)
/home/sendMq 0x10 1 0x1147 0
# Disable IR Filter (Cuts IR filter)
/home/sendMq 0x10 1 0x1146 1
# Flip the image
vi /etc/ui.conf'
# Change ptz_hflip=0 to ptz_hflip=1
sync
reboot
# Note: When making changes to /etc/ui.conf, these steps may be required:
/home/killapp.sh
sync
vi /etc/ui.conf
# make edit (see above)
sync
reboot
# Night Vision IR Light is wrHdThreadKB in /etc/ui.conf.
# 2=Off 1=On
# LED Control
/home/led_ctl -boff -yoff &
Focusing the image:
- http://4pda.ru/forum/index.php?showtopic=638230&view=findpost&p=40302353
- http://4pda.ru/forum/index.php?showtopic=638230&view=findpost&p=47551927
#!/bin/sh
# Cron entry
# * * * * * /home/script/run_check_motion.sh
mkdir -p "/home/script/tmp"
PIDFILE="/home/script/tmp/check_motion.pid"
if [ -e "${PIDFILE}" ] && (ps |
grep -P "^\s*$(cat ${PIDFILE})$" &> /dev/null); then
exit 99
fi
/home/script/check_motion.sh &
echo $! > "${PIDFILE}"
chmod 644 "${PIDFILE}"
@jcconnell
Copy link
Author

jcconnell commented May 16, 2019

I'm finding that check_motion.sh often stops running unexpectedly. I've written a helper script run_check_motion.sh to run each minute via cron, check if check_motion.sh is running and restart it if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment