I’ve been seeing that the Raspberry Pi Cameras are going offline quite often and it is quite annoying. I’ve done a few things in an attempt to improve the reliability of the cameras but have not been able to come up with a winning solution yet. If I was only having issues with the cameras that are some distance from my Wi-Fi router, I could blame the network, but the one camera that is closest to the router (11ft/3.3M with no obstructions) seems to be the worst offender.
I have used some new USB Power supplies and cables to rule out power as an issue. I’ve also used new SD Cards, but that has not improved the situation. I also created a script to monitor the Wi-Fi connection and restart it if it appears down. I also added monitoring and restarting the MediaMTX daemon as well.
Parts Used in this Project
Here are some links to the items that I used in an attempt to rectify the issue with the cameras.
The Amazon links are affiliate links that will provide me some income to offset the cost of this site and allow me to continue posting similar content.
- 2.1A Dual Port USB Cube Power Adapter
- USB-A to Micro USB Cable – 10 Foot
- PNY 32GB Elite Class 10 U1 microSDHC Flash Memory Card
Steps to create monitoring script
This script is modified from a post on the Raspberry Pi Forum at https://forums.raspberrypi.com/viewtopic.php?t=338723. There are a few errors in the posted code. Unfortunately, the thread is locked so I cannot reply with the corrected code. The main issues with the code as posted in the forum the author assumed that everyone’s terminal session has a white background with black text. Mine is the opposite, which is the default. I thought something was wrong with the script as there was nothing displayed after the status. Once I determined that the text was black on a black background, I was able to resolve that issue.
The other issue is that ifdown and ifup are no longer used to bring the connection down and back up. There was also a minor bug with the call to iwconfig as the interface was not specified. The grep command was returning the loopback connection as well as the Wi-Fi connection.
- Open a SSH or terminal session to the Raspberry Pi
- Open the text editor to create the script file.
sudo nano /usr/local/bin/wifi_monitor.bsh
- Add the following content to the file.
#!/bin/bash
# Shell script to monitor Wi-Fi status and operating conditions
# and attempt Wi-Fi restart if down
IWCONFIG="/usr/sbin/iwconfig wlan0"
# The IP for the server you wish to ping (8.8.8.8 is a public Google DNS server)
SERVER=192.168.1.1
LOGFILE="/var/log/wifi.log"
DATE=`date`
I=$((`cat /sys/class/thermal/thermal_zone0/temp`/100))
TEMP="Temp="$(($I/10))"."$(($I%10))" C"
STATUS=`$IWCONFIG | grep level | awk '{$1=$1};1'`
STATUS+=" "
STATUS+=`$IWCONFIG | grep Rate | awk '{$1=$1};1'`
# Only send two pings, sending output to /dev/null
ping -c2 ${SERVER} > /dev/null
# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? = 0 ]
then
UPDOWN="\033[32m UP \033[0m"
else
UPDOWN="\033[31mDOWN \033[0m"
STATUS+=" Attempting wlan0 restart"
# Restart the wireless interface
sh -c 'ifconfig wlan0 down; sleep 5; ifconfig wlan0 up'
fi
echo -e "WiFi @ $DATE: $UPDOWN $TEMP $STATUS" >> ${LOGFILE}
echo -e "WiFi @ $DATE: $UPDOWN $TEMP $STATUS"
# Check daemon
# Name of the daemon to check
DAEMON_NAME="mediamtx"
LOGFILE="/var/log/mediamtx.log"
STATUS=""
# Check if the daemon is running
if pgrep -x "$DAEMON_NAME" > /dev/null; then
STATUS="$DAEMON_NAME is running"
else
STATUS="$DAEMON_NAME is not running, restarting..."
# Use appropriate command to restart the daemon
sudo systemctl restart "$DAEMON_NAME"
fi
echo -e "MediaMTX @ $DATE: $STATUS" >> ${LOGFILE}
echo -e "MediaMTX @ $DATE: $STATUS"
- If necessary, change the IP Address from 192.168.1.1 to an IP Address on your local network or a reliable public IP Address. I chose the local gateway address as I do not care if the Raspberry Pi has access to the internet.
- Save the file and exit the Nano editor.
- Make the script executable by running the following command.
sudo chmod 744 /usr/local/bin/wifi_monitor.bsh
- Test the script by running the following command.
sudo /usr/local/bin/wifi_monitor.bsh
- If everything is working as expected you should see output similar to the following.
WiFi @ Mon 30 Dec 18:19:13 EST 2024: UP Temp=56.9 C Link Quality=40/70 Signal level=-70 dBm Bit Rate=52 Mb/s Tx-Power=31 dBm
MediaMTX @ Mon 30 Dec 18:19:13 EST 2024: mediamtx is running
Run the script on start/reboot
- Run the following command to bring up the CRON editor.
sudo crontab -e
- Select the editor you would like to use.
pi@PiCam01:~ $ sudo crontab -e
no crontab for root - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.tiny
3. /bin/ed
Choose 1-3 [1]:
- At the end of the file, add the following line to run the script every 5 minutes.
*/5 * * * * /usr/local/bin/wifi_monitor.bsh
- The completed file should look like the following.
### CRON FILE ###
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * /usr/local/bin/wifi_monitor.bsh
- Save the file and exit the text editor.
- Wait 5-minutes or longer and run the following commands to view the logs and verify that the script was run from the CRON Job as expected.
cat /var/log/wifi.log
cat /var/log/mediamtx.log
- If the script is running as expected, the Raspberry Pi should reconnect to the Wi-Fi if it finds that it is not able to reach the IP Address defined in the script. It will also start the mediamtx daemon if it is not running.
Final Thoughts
I’m still not certain what is causing the Raspberry Pi cameras to randomly stop connecting to Blue Iris. I will continue working on this to see if I can determine the root cause.
You must be logged in to post a comment.