Motioneyeos Camera Down Detection

I have several raspi zero running motioneyeos and usb cameras, along with a pi 3b+ running motioneye which brings them all together in one place, however every now and then I loose a camera, and either get get a “message” in the video display or just the grey background and icon.

Is there any way I can I can detect either of these situations so that I can either reboot or power cycle the devices?

Anybody able to suggest any ideas?

This is something I need to fix as well.
I been using ping to see if the Raspberry Pi running MotionEye goes down completely.
But it’s often just the MotionEye OS that’s crashes and the Raspberry Pi answer on ping anyway.

dragonfly: Did you find a workaround for this?

I ended up using ffmpeg to capture an image from each camera every couple of minutes, if you get an image it’s still working, then I send an alive or dead over mqtt.

1 Like

Ah, thanks @dragonfly I’ll try that.
How did you get Home Assistant to sense if a image was taken?
Is it based on looking for a certain file?

I wrote a python script that sends a mqtt dead/alive every few minutes based on filesize, I’m on my phone at the moment, but when I sit down this evening I’ll dig it out, and post it without passwords etc. Don’t think it was that big.

Sorry took me a a day to remember…

Disclaimer, this code was knocked up to detect crashed cameras, it worked so it was never really properly developed or worked on beyond that, but if it helps you, you’re welcome to use it, modifiy it etc. I’ll try and stick some comments in to explain…

import sys
import os, glob, heapq
import paho.mqtt.publish as publish
import datetime

min_filesize = 20000 # Files smaller than this will be failed you're size might be different

file_name = str(sys.argv[1])
camera = str(sys.argv[2])
print file_name
print camera
file_stats = os.stat(file_name)
fs = file_stats.st_size
print "File Size in Bytes is "+str(fs)


files = glob.iglob("./"+camera+"/*.jpeg")
latest=heapq.nlargest(5, files, key=os.path.getmtime) # last 10 entries
#print("\n".join(latest))
dupesize = True
for fn in latest:
    old_stat = os.stat(fn)
#    print old_stat.st_size
    if old_stat.st_size <> fs:
        dupesize = False

now = datetime.datetime.now()
 
if dupesize:
    print "Duplicate Sizes Found"
    with open('/home/pi/cams/camcheck.log','a+') as f:
        output = f.write((now.strftime("%Y-%m-%d %H:%M:%S ")) + camera +" Duplicate Sizes Found\n")

if file_stats.st_size < min_filesize and file_stats.st_size > 0:
    print "Small File Found"
    with open('/home/pi/cams/camcheck.log','a+') as f:
        output = f.write((now.strftime("%Y-%m-%d %H:%M:%S ")) + camera + " Small File Found\n")
 
if (file_stats.st_size > 0 and file_stats.st_size < min_filesize) or dupesize == True:
    publish.single("cctv/"+camera+"/live", "false", hostname="x.x.x.x", auth = {'username':"user", 'password':"password"})
    print "Dead"
else:
    publish.single("cctv/"+camera+"/live", "true", hostname="x.x.x.x", auth = {'username':"user", 'password':"password"})
    print "Alive"

I then have the following script that calls the python from a cronjob

#!/bin/sh
cd /home/pi/cams

today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`;

#Grab snapshot from MotionEye Cameras
wget -O ./camera1/$today.jpeg http://x.x.x.x/cctv/picture/1/current/?_username=admin\&_signature=xxxxxxxxxxxxxxxxxxxx
python checkjpg.py ./camera1/$today.jpeg camera1
wget -O ./camera3/$today.jpeg http://x.x.x.x/cctv/picture/3/current/?_username=admin\&_signature=xxxxxxxxxxxxxxxxxxxx
python checkjpg.py ./camera3/$today.jpeg camera3

#Delete previous taken snapshots older than 1 days
find /home/pi/cams/camera1/ -name '*.jpeg' -mtime 1 -delete
find /home/pi/cams/camera3/ -name '*.jpeg' -mtime 1 -delete

The acutal script has 12 cameras in it, but I’m sure you can get the basics of it from the 2 I’ve shown.

1 Like