Categories
Raspberry Pi Uncategorized

Revisiting the Raspberry Pi Camera

The camera has been more stable but needed restarting quite often then finally failed to restart. Using AI, the following steps were generated in an attempt to improve the stability/availability of the Raspberry Pi Camera over time.

This project uses a Raspberry Pi Zero 2 W and a Raspberry Pi Camera Module 3 to build a Wi-Fi camera that works with Frigate, Blue Iris, or any other Network Video Recorder (NVR) that can read an RTSP stream.

Updated September 2026 for Raspberry Pi OS “Trixie” and MediaMTX v1.21.1. This version also adds the pieces that keep the camera running for months unattended: automatic restarts, a hardware watchdog, a Wi-Fi watchdog, fewer writes to the SD card, and a daily health-report email.

Updated October 11, 2026. This guide was corrected after I used the same steps to build my UPS monitor Pis. The most important fix: the system log was not actually being kept on the SD card, so the evidence from a crash was lost at the next reboot. If you followed an earlier version, see What has Changed (2026-10-11) near the end for the list of changes and the exact steps to bring your camera up to date.

Parts Used in this Project

Here are some links to the items that I used in this project.

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. I have also provided links to PiShop.us in case items are out of stock at Amazon.

What this guide sets up

ProblemWhat protects against itPart
Not enough power (long cable, weak supply)A check now, and a check in the daily email3, 12
Wi-Fi going to sleep and dropping off the networkWi-Fi power saving turned off4
SD card wearing outFewer writes to the card5
MediaMTX crashingsystemd restarts it automatically8
The whole Pi freezingHardware watchdog reboots it9
Wi-Fi disconnecting and not coming backWi-Fi watchdog reconnects, then reboots if needed10
Problems nobody notices for weeksDaily health-report email with fix instructions11, 12

Before you start

You will need:

  1. A DHCP reservation for the camera on your router, so its IP address never changes. Your NVR connects by IP address, so this matters. (You can add it after the Pi first connects, once you can see it in the router’s client list.)
  2. A Gmail account with 2-Step Verification turned on. You’ll create a Gmail app password in Part 11 so the Pi can send the daily email. Create a new app password just for this camera, so you can revoke it without affecting anything else.
  3. An SSH client (PuTTY on Windows) and VLC for testing the stream.

How to read this guide:

  • Every command block starts with a comment line such as # On picam03, as your user. That tells you where to run it. The # line is just a comment, so it’s harmless to paste it along with the command.
  • Run the commands in the order shown. Don’t skip ahead.
  • After most steps there is an Expected result. If what you see is different, stop and sort it out before moving on.
  • Anything written REPLACE_... or <like-this> is a placeholder you must change to your own value.
  • This guide uses the hostname picam03. Use your own camera’s name wherever you see it.

Part 1 – Write Raspberry Pi OS to the SD card

If you’ve already written the card: you’re fine to continue, as long as you picked Raspberry Pi OS Lite (64-bit) and set the hostname, username/password, Wi-Fi and SSH in the customization screens (step 1.4). If you skipped customization, it’s quickest to write the card again.

1.1. Open Raspberry Pi Imager and choose the device: “Raspberry Pi Zero 2 W”. List of Boards in the Raspberry Pi Imager software

1.2. Choose the operating system: “Raspberry Pi OS (other)”, then “Raspberry Pi OS Lite (64-bit)”. First list of Operating Systems in the Raspberry Pi Imager software Second list for other Operating Systems in the Raspberry Pi Imager software

1.3. Choose the storage: your SD card. List of available removable storage devices in the Raspberry Pi Imager software

1.4. When Imager offers to customize the OS, say yes and set all of these. (The screenshots are from an older Imager version, and newer versions lay these screens out differently, but the settings are the same.)

  • Hostname: picam03
  • Time zone and keyboard layout: yours. The time zone controls when the daily email arrives.
  • Username and password: your choice. Don’t reuse a password from anywhere else.
  • Wi-Fi: your network name (SSID), password, and Wi-Fi country (US). The Pi Zero 2 W only supports 2.4 GHz Wi-Fi.
  • SSH: enabled.

1.5. Write the card. When Imager confirms it’s done and verified, move the card to the Pi and power it on. Raspberry Pi Imager confirmation message

1.6. Give it 3–5 minutes. The first boot on a Pi Zero 2 W is slow, and it reboots itself once while it applies your settings.

Part 2 – First login and updates

2.1. Find the Pi’s IP address in your router’s client list (look for picam03) and add the DHCP reservation now. Then connect with PuTTY to that IP address, port 22, and log in with the username and password you set in Imager.

2.2. Confirm you’re on the right operating system:

# On picam03, as your user
grep VERSION_CODENAME /etc/os-release
uname -m

Expected result:

VERSION_CODENAME=trixie
aarch64

If it says bookworm, you have an older image. Update Raspberry Pi Imager and write the card again, because Part 5 of this guide is written for Trixie. If it says armv7l instead of aarch64, you picked the 32-bit OS. Write the card again with the 64-bit Lite version.

2.3. Install all updates. This can take 15–30 minutes on a Pi Zero 2 W.

# On picam03, as your user
sudo apt update
sudo apt full-upgrade -y

Expected result: it ends back at the prompt with no lines starting with E:. (W: warnings are okay.)

2.4. Install the tools the rest of this guide uses. Most are already installed, and listing them again does no harm.

# On picam03, as your user
sudo apt install -y curl iw rpicam-apps-lite

Expected result: it ends back at the prompt with no E: lines.

2.5. Reboot so the updates take effect, then log back in with PuTTY after a minute or two.

# On picam03, as your user
sudo reboot

Part 3 – Check the camera and the power

3.1. Make sure the Pi can see the camera:

# On picam03, as your user
rpicam-hello --list-cameras

Expected result: one camera listed. Camera Module 3 shows up as imx708, for example:

Available cameras
-----------------
0 : imx708 [4608x2592 10-bit RGGB] (/base/soc/i2c0mux/i2c@1/imx708@1a)

If it says No cameras available!, shut down (sudo poweroff), unplug the power, reseat both ends of the ribbon cable, and try again. The contacts must face the board, and the latches must be pushed fully closed.

3.2. Check the power. Power problems are the most common reason a Pi camera stops working, so don’t skip this. Run it with the camera mounted where it will live, on the real cable and power supply:

# On picam03, as your user
vcgencmd get_throttled

Expected result:

throttled=0x0

Any other value, such as throttled=0x50000 or throttled=0x50005, means the voltage has dropped too low since the Pi booted. Fix that before going any further: use a shorter or thicker cable, or a better power supply. Then reboot and check again. (Part 12’s daily email keeps checking this, because under-voltage often shows up only under load, once the camera is streaming.)

Part 4 – Make Wi-Fi reliable

4.1. Turn off Wi-Fi power saving. When it’s on, the Pi’s Wi-Fi dozes off and the camera disappears from the network. This setting file works however your Wi-Fi connection was created:

# On picam03, as your user
sudo tee /etc/NetworkManager/conf.d/wifi-powersave-off.conf >/dev/null <<'EOF'
[connection]
wifi.powersave = 2
EOF

Expected result: no output. (2 means “disabled” in NetworkManager’s settings.)

4.2. Reboot, log back in, and check it:

# On picam03, as your user
sudo reboot
# On picam03, as your user
iw dev wlan0 get power_save

Expected result:

Power save: off

4.3. Check the signal strength where the camera is mounted:

# On picam03, as your user
iw dev wlan0 link | grep -E "SSID|signal"

Expected result: something like signal: -58 dBm. Closer to 0 is stronger. Around -60 or better is good. Weaker than -70 (for example -75) is unreliable for video, so move the access point or the camera before continuing.

4.4. Check that your router answers a ping. The Wi-Fi watchdog in Part 10 uses this to decide whether the network is up.

# On picam03, as your user
ip route show default
ping -c 3 $(ip route show default | awk '{ print $3; exit }')

Expected result: the first line shows default via <router-ip> dev wlan0 ..., and the ping ends with 3 packets transmitted, 3 received, 0% packet loss. If the router does not answer pings (100% packet loss, even though you’re connected over SSH), make a note of it. You’ll need to point the watchdog at something else in step 10.3.

Part 5 – Reduce writes to the SD card

SD cards wear out from being written to. A camera that runs 24/7 doesn’t need to write much, so these steps cut out the writing it doesn’t need. Nothing here affects the video.

5.1. Keep swap in RAM only. Trixie uses compressed swap in RAM (called zram), but by default it also copies idle memory to a 2 GB file on the SD card every day. This drop-in file turns off the SD card part:

# On picam03, as your user
sudo mkdir -p /etc/rpi/swap.conf.d
sudo tee /etc/rpi/swap.conf.d/90-zram-only.conf >/dev/null <<'EOF'
[Main]
Mechanism=zram
EOF

Expected result: no output. The change takes effect at the reboot in step 5.6.

5.2. Keep the system log on the SD card, but cap its size. Out of the box, Raspberry Pi OS keeps the log in RAM only, so it’s lost at every reboot, including the reboot after a crash, when you need it most. This setting keeps it on the SD card, limited to 50 MB, so it can’t grow forever:

# On picam03, as your user
sudo mkdir -p /etc/systemd/journald.conf.d
sudo tee /etc/systemd/journald.conf.d/90-picam.conf >/dev/null <<'EOF'
[Journal]
Storage=persistent
SystemMaxUse=50M
EOF

Expected result: no output.

Why 90-? Raspberry Pi OS has its own setting file, 40-rpi-volatile-storage.conf, that says to keep the log in RAM. When several files set the same thing, the one whose name sorts last wins, so this file’s name has to come after 40-.

Followed an earlier version of this guide? It named this file 10-picam.conf, which loses to the Raspberry Pi OS file, so your logs were still being kept in RAM. Rename it, and restart the logging service:

# On picam03, as your user - ONLY if you have 10-picam.conf from an earlier version
sudo mv /etc/systemd/journald.conf.d/10-picam.conf /etc/systemd/journald.conf.d/90-picam.conf
sudo systemctl restart systemd-journald

Then do the checks in step 5.7.

5.3. Make sure logs aren’t written twice. Older systems ran a second logger (rsyslog) that copied every message into /var/log/syslog as well:

# On picam03, as your user
systemctl is-active rsyslog

Expected result: inactive, or an error that the unit could not be found. Either is fine. Only if it says active, turn it off:

# On picam03, as your user - ONLY if the previous command said "active"
sudo systemctl disable --now rsyslog

5.4. Stop automatic background package downloads. These timers download package lists and rebuild the manual-page index on a schedule. Turning them off saves writes, and it also means nothing changes on the camera unless you choose to update it (see Maintenance at the end). Run these one at a time. If one of them says it doesn’t exist, that’s fine; just go on to the next.

# On picam03, as your user
sudo systemctl disable --now apt-daily.timer
# On picam03, as your user
sudo systemctl disable --now apt-daily-upgrade.timer
# On picam03, as your user
sudo systemctl disable --now man-db.timer

Expected result: each prints a Removed "/etc/systemd/system/timers.target.wants/..." line, or nothing if it was already off.

5.5. Check two things Raspberry Pi OS should already do (nothing to change if they match):

# On picam03, as your user
findmnt -no OPTIONS /
findmnt -no FSTYPE /tmp

Expected result:

rw,noatime
tmpfs

noatime means Linux doesn’t write to the card every time a file is simply read. tmpfs means /tmp lives in RAM. If the first line doesn’t include noatime, stop and look into it before editing anything. A mistake in /etc/fstab can stop the Pi from booting.

5.6. Reboot to apply the swap and log changes, then log back in:

# On picam03, as your user
sudo reboot

5.7. Verify:

# On picam03, as your user
swapon --show
ls -l /var/swap
journalctl --disk-usage
ls -d /var/log/journal
systemd-analyze cat-config systemd/journald.conf | grep -E '^# /|^Storage' | grep -B1 '^Storage' | tail -n 2

Expected result:

  • swapon shows only /dev/zram0 (no /var/swap file).
  • The first ls says No such file or directory (the 2 GB swap file has been removed).
  • journalctl reports a few MB, and never more than 50 MB.
  • The second ls prints /var/log/journal. That folder only exists when the log is kept on the SD card.
  • The systemd-analyze command prints these two lines. The last Storage= setting is the one that wins, and it must be persistent:# /etc/systemd/journald.conf.d/90-picam.conf
    Storage=persistent

From the next reboot on, journalctl --list-boots --no-pager lists every boot, and the Troubleshooting section’s journalctl -b -1 commands can read the log from before a crash.

Optional, for later: Raspberry Pi OS can also make the whole SD card read-only (sudo raspi-config → Performance Options → Overlay File System). That protects the card completely, but every change you make, and every log, is lost at reboot, and you have to turn it off to install updates. Only consider it once everything has been running well for a while.

Part 6 – Install MediaMTX

6.1. Download MediaMTX v1.21.1 (the latest release as of September 2026), along with the official checksum file:

# On picam03, as your user
cd ~
wget https://github.com/bluenviron/mediamtx/releases/download/v1.21.1/mediamtx_v1.21.1_linux_arm64.tar.gz
wget https://github.com/bluenviron/mediamtx/releases/download/v1.21.1/checksums.sha256

Note: the file name changed in newer releases. It’s now linux_arm64, not linux_arm64v8 as in older guides.

6.2. Check that the download is complete and hasn’t been tampered with:

# On picam03, as your user
sha256sum --check --ignore-missing checksums.sha256

Expected result:

mediamtx_v1.21.1_linux_arm64.tar.gz: OK

If it says FAILED, delete the file and download it again. Do not continue with a file that fails.

6.3. Unpack it and install the program and its config file in the standard system locations:

# On picam03, as your user
mkdir -p ~/mediamtx
tar -xzf mediamtx_v1.21.1_linux_arm64.tar.gz -C ~/mediamtx
sudo install -m 755 ~/mediamtx/mediamtx /usr/local/bin/mediamtx
sudo mkdir -p /usr/local/etc
sudo install -m 644 ~/mediamtx/mediamtx.yml /usr/local/etc/mediamtx.yml
/usr/local/bin/mediamtx --version

Expected result: the last command prints v1.21.1.

6.4. Change the settings. This turns off everything except RTSP (the protocol your NVR uses), and turns on the local status API that the health email uses. The API only answers on the Pi itself (127.0.0.1), never on the network. Paste the whole block at once:

# On picam03, as your user
sudo sed -i \
-e 's/^rtspTransports: \[udp, multicast, tcp\]$/rtspTransports: [tcp]/' \
-e 's/^rtmp: true$/rtmp: false/' \
-e 's/^hls: true$/hls: false/' \
-e 's/^webrtc: true$/webrtc: false/' \
-e 's/^srt: true$/srt: false/' \
-e 's/^moq: true$/moq: false/' \
-e 's/^api: false$/api: true/' \
-e 's/^apiAddress: :9997$/apiAddress: 127.0.0.1:9997/' \
/usr/local/etc/mediamtx.yml
grep -nE '^(api|apiAddress|rtspTransports|rtmp|hls|webrtc|srt|moq):' /usr/local/etc/mediamtx.yml

Expected result: exactly these 8 lines, with these values (the line numbers may be slightly different):

147:api: true
149:apiAddress: 127.0.0.1:9997
245:rtspTransports: [tcp]
290:rtmp: false
314:hls: false
377:webrtc: false
437:srt: false
445:moq: false

If any line still shows its old value, open the file with sudo nano /usr/local/etc/mediamtx.yml, find that line with Ctrl+W, and change it by hand.

Why each change: rtspTransports: [tcp] makes the stream use TCP only, which holds up much better over Wi-Fi than UDP. RTMP, HLS, WebRTC, SRT and MoQ are other streaming protocols this camera doesn’t use. Turning them off saves memory and CPU on the Pi Zero, and leaves fewer network ports open.

6.5. Replace the paths: section at the bottom of the file with the camera settings. The first command deletes everything from the paths: line to the end of the file. The second adds the new section.

# On picam03, as your user
sudo sed -i '/^paths:/,$d' /usr/local/etc/mediamtx.yml
sudo tee -a /usr/local/etc/mediamtx.yml >/dev/null <<'EOF'
paths:
cam:
source: rpiCamera
rpiCameraWidth: 1280
rpiCameraHeight: 720
rpiCameraFPS: 15
rpiCameraBitrate: 1500000
rpiCameraIDRPeriod: 30
EOF
tail -n 8 /usr/local/etc/mediamtx.yml

Expected result: the last 8 lines of the file are exactly the paths: block above, and paths: appears only once in the file.

Why these settings:

  • 720p at 15 frames per second and 1.5 Mbps is comfortable for a Pi Zero 2 W and for 2.4 GHz Wi-Fi.
  • rpiCameraIDRPeriod: 30 sends a full “key frame” every 2 seconds. The default is every 4 seconds. With more frequent key frames, Frigate and Blue Iris connect faster and their recordings cut more cleanly.
  • The default all_others: entry is left out on purpose. Without it, cam is the only stream that exists on this camera, and nobody on the network can push a different stream into it.

6.6. Have MediaMTX check the config file for mistakes:

# On picam03, as your user
/usr/local/bin/mediamtx --validate-conf=/usr/local/etc/mediamtx.yml

Expected result:

configuration file: /usr/local/etc/mediamtx.yml
configuration file is valid

If it prints an ERR: line instead, it names the setting that’s wrong. Fix it with sudo nano /usr/local/etc/mediamtx.yml and run the check again.

6.7. Run MediaMTX by hand once to test it:

# On picam03, as your user
sudo /usr/local/bin/mediamtx /usr/local/etc/mediamtx.yml

Expected result: it keeps running and shows lines similar to these, with no ERR lines:

INF MediaMTX v1.21.1
INF configuration loaded from /usr/local/etc/mediamtx.yml
INF [RTSP] started with listeners on :8554 (TCP/RTSP)
INF [API] started with listener on 127.0.0.1:9997 (TCP/HTTP)

followed by some camera start-up lines. A WAR line mentioning sdn.cpp (legacy SDN tuning) is harmless. Leave it running for Part 7.

Part 7 – Test the stream in VLC

7.1. On your Windows PC, open VLC, click the “Media” menu, and choose “Open Network Stream…”. Windows VLC Media Menu items

7.2. In the “Please enter a network URL:” box, enter the following (with the Pi’s IP address) and press “Play”: rtsp://<picam03-ip>:8554/cam VLC Open Network Stream Dialog

Expected result: video from the camera appears within a few seconds, and the PuTTY window shows a line like:

INF [RTSP] [session 861b8faa] is reading from path 'cam', with TCP, 1 track (H264)
VLC showing video stream

If you see repeated write queue is full warnings, the Wi-Fi can’t keep up and frames are being dropped. Don’t ignore these. Improve the signal (step 4.3), or lower rpiCameraBitrate.

7.3. Close VLC. In PuTTY, stop MediaMTX with Ctrl+C. Only one program can use the camera at a time, so it has to be stopped before the service in Part 8 can start it.

Part 8 – Run MediaMTX as a service that restarts itself

8.1. Create the service file. Restart=always is the key line: if MediaMTX ever crashes, systemd starts it again 5 seconds later instead of leaving the camera dead.

# On picam03, as your user
sudo tee /etc/systemd/system/mediamtx.service >/dev/null <<'EOF'
[Unit]
Description=MediaMTX RTSP server for the Pi camera
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=0

[Service]
ExecStart=/usr/local/bin/mediamtx /usr/local/etc/mediamtx.yml
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

Expected result: no output.

8.2. Load it, start it, and make it start at every boot. These are separate commands, each on its own line:

# On picam03, as your user
sudo systemctl daemon-reload
sudo systemctl enable --now mediamtx
systemctl status mediamtx --no-pager

Expected result: the status shows Loaded: ... enabled and Active: active (running). Check VLC again. The stream should play.

8.3. Prove that the automatic restart works by force-killing MediaMTX:

# On picam03, as your user
sudo systemctl kill --signal=SIGKILL mediamtx

Wait 10 seconds, then:

# On picam03, as your user
systemctl status mediamtx --no-pager
journalctl -u mediamtx -n 20 --no-pager | grep "Scheduled restart"

Expected result: Active: active (running) with a start time from just a few seconds ago, and a line containing Scheduled restart job, restart counter is at 1. (Tomorrow’s health email will report “MediaMTX crashed and was restarted 1 time”. That’s this test, and it only appears once.)

Useful commands from now on:

systemctl status mediamtx --no-pager        # is it running?
sudo systemctl restart mediamtx # restart it (e.g. after editing the .yml)
sudo journalctl -u mediamtx -n 50 --no-pager # its last 50 log lines
sudo journalctl -u mediamtx -f # watch its log live (Ctrl+C to stop)

Part 9 – Turn on the hardware watchdog

The Pi has a built-in hardware timer. Once it’s switched on, the Pi’s system manager (systemd) resets the timer every few seconds. If the Pi freezes completely, the timer runs out and the hardware reboots the Pi about 15 seconds later. Without this, a frozen camera stays frozen until someone pulls the plug.

9.1. Create the setting file:

# On picam03, as your user
sudo mkdir -p /etc/systemd/system.conf.d
sudo tee /etc/systemd/system.conf.d/watchdog.conf >/dev/null <<'EOF'
[Manager]
RuntimeWatchdogSec=15
EOF

Expected result: no output. (15 seconds is the longest the Pi’s watchdog supports. Don’t set it higher.)

9.2. Reboot, log back in, and check it:

# On picam03, as your user
sudo reboot
# On picam03, as your user
systemctl show -p RuntimeWatchdogUSec --value

Expected result:

15s

9.3. (Optional, but it proves the watchdog works.) This deliberately crashes the Linux kernel. With the watchdog on, the Pi should reboot by itself within about 30 seconds, and PuTTY will disconnect. Only do this once everything above is working. It’s an unclean crash, much like pulling the power.

# On picam03, as your user - OPTIONAL: deliberately crashes the Pi
echo c | sudo tee /proc/sysrq-trigger

Expected result: PuTTY stops responding. After 1–2 minutes, you can log in again and the stream plays in VLC.

Part 10 – Install the Wi-Fi watchdog

Sometimes a Pi Zero loses Wi-Fi and never reconnects, even though Linux itself is still running fine, so the hardware watchdog won’t step in. This script pings your router every minute:

  • After 3 failed minutes in a row, it turns Wi-Fi off and back on.
  • After 10 failed minutes in a row, it reboots the Pi.
  • It never reboots within 30 minutes of booting, so a long router or access-point outage can’t cause a reboot loop.
  • It keeps its failure count in memory, so the only thing it writes to the SD card is its log lines, and only when something goes wrong.

10.1. Create the script. Paste the whole block at once. It’s long, and it ends with the line SCRIPT_END:

# On picam03, as your user
sudo tee /usr/local/sbin/wifi-watchdog.sh >/dev/null <<'SCRIPT_END'
#!/bin/bash
# wifi-watchdog.sh - keeps a Wi-Fi-only Raspberry Pi reachable.
#
# Every CHECK_INTERVAL seconds it pings the default gateway (or TARGET).
# - After FAILS_BEFORE_RECONNECT failed checks in a row it turns Wi-Fi off and on.
# - After FAILS_BEFORE_REBOOT failed checks in a row it reboots the Pi,
# but never sooner than MIN_UPTIME_FOR_REBOOT seconds after boot, so a long
# router/access-point outage can't cause a fast reboot loop.
# The failure counter is kept in memory only, so this never writes to the SD card
# except for its log lines (tag: wifi-watchdog), which the daily health email reads.
#
# Every setting can be overridden from the environment, which is how the test in
# picam_setup.md runs it. DRY_RUN=1 logs what it would do instead of doing it.

TARGET="${TARGET:-}" # empty = ping the default gateway
CHECK_INTERVAL="${CHECK_INTERVAL:-60}" # seconds between checks
FAILS_BEFORE_RECONNECT="${FAILS_BEFORE_RECONNECT:-3}" # ~3 minutes offline
FAILS_BEFORE_REBOOT="${FAILS_BEFORE_REBOOT:-10}" # ~10 minutes offline
MIN_UPTIME_FOR_REBOOT="${MIN_UPTIME_FOR_REBOOT:-1800}" # 30 minutes
STARTUP_DELAY="${STARTUP_DELAY:-120}" # let Wi-Fi connect after boot
DRY_RUN="${DRY_RUN:-0}"

# Test runs log under a different name so the daily health email doesn't
# mistake them for real reconnects/reboots.
LOG_TAG="wifi-watchdog"
[ "$DRY_RUN" = "1" ] && LOG_TAG="wifi-watchdog-test"

log() {
# -s also prints the message to the terminal when run by hand
logger -s -t "$LOG_TAG" "$1"
}

target_to_ping() {
if [ -n "$TARGET" ]; then
echo "$TARGET"
else
ip route show default 2>/dev/null | awk '{ print $3; exit }'
fi
}

log "started (check every ${CHECK_INTERVAL}s, reconnect after ${FAILS_BEFORE_RECONNECT}, reboot after ${FAILS_BEFORE_REBOOT} failed checks)"
sleep "$STARTUP_DELAY"

fails=0
while true; do
target=$(target_to_ping)

if [ -n "$target" ] && ping -c 3 -W 2 -q "$target" >/dev/null 2>&1; then
if [ "$fails" -gt 0 ]; then
log "network is back after $fails failed check(s)"
fi
fails=0
else
fails=$((fails + 1))
log "check failed ($fails in a row), target: ${target:-no default route}"

uptime_s=$(cut -d. -f1 /proc/uptime)
if [ "$fails" -ge "$FAILS_BEFORE_REBOOT" ] && [ "$uptime_s" -ge "$MIN_UPTIME_FOR_REBOOT" ]; then
log "REBOOTING: network unreachable for $fails checks"
if [ "$DRY_RUN" = "1" ]; then
log "DRY_RUN: would reboot now - stopping test"
exit 0
fi
systemctl reboot
exit 0
elif [ $((fails % FAILS_BEFORE_RECONNECT)) -eq 0 ]; then
log "RECONNECT: turning Wi-Fi off and on"
if [ "$DRY_RUN" = "1" ]; then
log "DRY_RUN: would run: nmcli radio wifi off; nmcli radio wifi on"
else
nmcli radio wifi off
sleep 5
nmcli radio wifi on
fi
fi
fi

sleep "$CHECK_INTERVAL"
done
SCRIPT_END
sudo chmod 755 /usr/local/sbin/wifi-watchdog.sh
sha256sum /usr/local/sbin/wifi-watchdog.sh

Expected result:

8661acc8ac396cd24f46ca8e453b55a7a32d0b99e0648fc94b723a69ce6017ae  /usr/local/sbin/wifi-watchdog.sh

If the checksum is different, something was lost or changed while pasting. Run step 10.1 again.

10.2. Test it without letting it change anything (DRY_RUN=1). First, against your real router, where it should stay quiet:

# On picam03, as your user
DRY_RUN=1 STARTUP_DELAY=0 CHECK_INTERVAL=5 /usr/local/sbin/wifi-watchdog.sh

Expected result: one line wifi-watchdog-test: started (...), and then nothing more. Every ping is succeeding. Wait about 20 seconds, then press Ctrl+C. (If you see check failed lines instead, your router doesn’t answer pings. See step 10.3.)

Next, against an address that doesn’t exist (192.0.2.1 is reserved for testing), where it should go through the reconnect and reboot steps. It only prints what it would do:

# On picam03, as your user
DRY_RUN=1 STARTUP_DELAY=0 CHECK_INTERVAL=1 TARGET=192.0.2.1 FAILS_BEFORE_RECONNECT=2 FAILS_BEFORE_REBOOT=4 MIN_UPTIME_FOR_REBOOT=0 /usr/local/sbin/wifi-watchdog.sh

Expected result: after about 20–30 seconds it stops by itself, having printed (each failed check takes a few seconds, because it waits for 3 pings to time out):

wifi-watchdog-test: started (check every 1s, reconnect after 2, reboot after 4 failed checks)
wifi-watchdog-test: check failed (1 in a row), target: 192.0.2.1
wifi-watchdog-test: check failed (2 in a row), target: 192.0.2.1
wifi-watchdog-test: RECONNECT: turning Wi-Fi off and on
wifi-watchdog-test: DRY_RUN: would run: nmcli radio wifi off; nmcli radio wifi on
wifi-watchdog-test: check failed (3 in a row), target: 192.0.2.1
wifi-watchdog-test: check failed (4 in a row), target: 192.0.2.1
wifi-watchdog-test: REBOOTING: network unreachable for 4 checks
wifi-watchdog-test: DRY_RUN: would reboot now - stopping test

(Each line may start with a date and <13>. That’s normal. Test runs log as wifi-watchdog-test so the health email doesn’t count them as real events.)

10.3. Only if your router did not answer pings in step 4.4 or 10.2: pick another device on your own network that is always on and does answer pings (for example your DNS server), and set it as the target. Don’t use an internet address such as 8.8.8.8. If your internet goes down while your home network is fine, the watchdog would keep rebooting the camera for as long as the outage lasts:

# On picam03, as your user - ONLY if your router doesn't answer pings
sudo nano /usr/local/sbin/wifi-watchdog.sh

Change the line TARGET="${TARGET:-}" to TARGET="${TARGET:-<ip-of-that-device>}", save (Ctrl+O, Enter), exit (Ctrl+X), and repeat the first test in step 10.2. (The checksum in step 10.1 won’t match after this edit. That’s expected.)

10.4. Create the service that keeps the watchdog running:

# On picam03, as your user
sudo tee /etc/systemd/system/wifi-watchdog.service >/dev/null <<'EOF'
[Unit]
Description=Wi-Fi watchdog (reconnect, then reboot, if the network is lost)
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/sbin/wifi-watchdog.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now wifi-watchdog
systemctl status wifi-watchdog --no-pager

Expected result: Loaded: ... enabled and Active: active (running). Because the status appears less than a second after the service starts, the script may not have written its first log line yet, and the process list may briefly show two wifi-watchdog.sh lines. Both are normal. A few seconds later, check its log:

#On picam03, as your user
journalctl -t wifi-watchdog -n 5 --no-pager

Expected result:

... wifi-watchdog[...]: started (check every 60s, reconnect after 3, reboot after 10 failed checks)

After that it stays quiet, unless the network actually drops.

To see what the watchdog has done at any time:

journalctl -t wifi-watchdog --since "7 days ago" --no-pager

Part 11 – Let the Pi send email (through Gmail)

11.1. Create a Gmail app password for this camera. Do this on your PC:

  1. Go to https://myaccount.google.com/apppasswords and sign in. (If Google says app passwords aren’t available, turn on 2-Step Verification first, under Security.)
  2. For the app name, type picam03 and click Create.
  3. Google shows a 16-letter password in four groups, like abcd efgh ijkl mnop. Copy it somewhere safe for the next few minutes. You’ll type it without the spaces: abcdefghijklmnop.

Setting up several cameras? One app password each, or one shared? A shared one works. Gmail accepts the same app password from any number of devices. One per camera (named picam01, picam02, and so on) is safer, though. The password is stored on the camera’s SD card, so if a card is ever lost, or thrown away without being wiped, you can revoke just that camera’s password while the others keep sending.

Better still, use a separate Gmail account just for alerts. An app password doesn’t only send mail: anyone who has it can also sign in and read that account’s email. If the cameras send from a dedicated account (for example myhouse.alerts@gmail.com), a password taken from an SD card exposes nothing but health reports. The reports can still be delivered to your everyday address, which you set in step 12.2.

11.2. Install msmtp, a small program that sends email through Gmail:

# On picam03, as your user
sudo apt install -y msmtp

Expected result: it installs. If a blue screen asks “Enable AppArmor support?”, choose No (the default) and press Enter.

11.3. Create the mail settings file. It uses placeholders, so your password never ends up in your command history:

# On picam03, as your user
sudo tee /etc/msmtprc >/dev/null <<'EOF'
# /etc/msmtprc - lets this Pi send email through Gmail
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
syslog LOG_MAIL

account gmail
host smtp.gmail.com
port 587
from REPLACE_GMAIL_ADDRESS
user REPLACE_GMAIL_ADDRESS
password REPLACE_APP_PASSWORD

account default : gmail
EOF
sudo chmod 600 /etc/msmtprc

11.4. Put in your real values:

# On picam03, as your user
sudo nano /etc/msmtprc

Replace both REPLACE_GMAIL_ADDRESS with your Gmail address, and REPLACE_APP_PASSWORD with the 16-letter app password (no spaces). Save (Ctrl+O, Enter) and exit (Ctrl+X). Then check who can read the file:

# On picam03, as your user
ls -l /etc/msmtprc

Expected result: it starts with -rw------- 1 root root, which means only root can read the password.

11.5. Send a test email (replace the address with where you want camera emails to go):

# On picam03, as your user
printf 'Subject: picam03 test email\n\nIf you can read this, the Pi can send email.\n' | sudo msmtp REPLACE_EMAIL_TO

Expected result: no output, and the email arrives within a minute or so (check Spam the first time). If it fails, see what happened with sudo journalctl -t msmtp -n 5 --no-pager. An authentication failed error means the app password or Gmail address is wrong. A connection timed out error means something (such as a firewall rule on your network) is blocking outgoing port 587.

Part 12 – Daily health-report email

Once a day at 7:00 AM, this script checks the camera and emails you a report. The subject line tells you at a glance: [picam03] Daily health: OK or [picam03] Daily health: 2 PROBLEM(S) FOUND. Every problem in the email comes with step-by-step instructions to fix it.

What it checks:

CheckFlagged when
Power supplyvcgencmd get_throttled is not 0x0. It tells a power problem apart from a heat problem, and gives fix suggestions for each.
Temperature75 °C or hotter
Wi-Fi power savingIt’s on (includes the instructions to turn it off)
Wi-Fi signalWeaker than -70 dBm
Wi-Fi watchdogIt had to reconnect or reboot in the last 24 hours, or it isn’t running
MediaMTXNot set to restart automatically (with fix instructions), not running, not starting at boot, or crashed in the last 24 hours
CameraNot producing video, even though MediaMTX is running
Hardware watchdogTurned off (with fix instructions)
Disk spaceAny filesystem with less than 20% free
SD card healthThe card has switched to read-only, filesystem errors are recorded, the kernel logged read/write errors, or much more is being written than normal

About SD card health: ordinary SD cards don’t report how worn out they are the way SSDs do, so there’s no “percent life left” to read. Instead, the script watches for the warning signs that show up before a card fails: filesystem errors, read/write errors and timeouts in the kernel log, and the card switching itself to read-only. It also reports how much is written to the card each day, so you can see that Part 5 is working.

12.1. Create the script. Paste the whole block at once. It’s long, and it ends with the line SCRIPT_END:

# On picam03, as your user
sudo tee /usr/local/sbin/picam-health.sh >/dev/null <<'SCRIPT_END'
#!/bin/bash
# picam-health.sh - daily health report for a Raspberry Pi camera running MediaMTX.
#
# Checks power, temperature, Wi-Fi, MediaMTX, the hardware watchdog, disk space and
# the SD card, then emails a plain-text report with fix instructions for anything wrong.
#
# sudo picam-health.sh build the report and email it
# sudo picam-health.sh --dry-run build the report and print it instead
#
# Run as root (the systemd timer does): reading the SD card superblock, the kernel
# log and /etc/msmtprc all need it.

MAIL_TO="REPLACE_EMAIL_TO"
DISK_WARN_PCT=80 # flag a filesystem with less than 20% free
WIFI_IF="wlan0"
WIFI_SIGNAL_WARN=-70 # dBm; a weaker (more negative) signal is flagged
TEMP_WARN_C=75 # the Pi starts slowing itself down at 80 C
WRITE_WARN_MB_PER_DAY=1024 # flag more than this much written to the SD card per day
WRITE_MIN_HOURS=12 # judge the write rate only over at least this many hours
MEDIAMTX_API="http://127.0.0.1:9997"
CAMERA_PATH="cam"
STATE_DIR="/var/lib/picam-health"

DRY_RUN=0
[ "${1:-}" = "--dry-run" ] && DRY_RUN=1

HOST=$(hostname)
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
PROBLEMS="$WORK/problems"
OKS="$WORK/ok"
DETAILS="$WORK/details"
: >"$PROBLEMS"; : >"$OKS"; : >"$DETAILS"
NPROB=0

# problem "<one-line summary>" (how-to-fix text is read from stdin)
problem() {
NPROB=$((NPROB + 1))
{
echo "PROBLEM $NPROB: $1"
echo "------------------------------------------------------------"
cat
echo
} >>"$PROBLEMS"
}
ok() { echo " [OK] $1" >>"$OKS"; }
detail() { echo " $1" >>"$DETAILS"; }

########################################################################
# 1. Power supply and temperature
########################################################################
THROTTLED=$(vcgencmd get_throttled 2>/dev/null | cut -d= -f2)
if [ -z "$THROTTLED" ]; then
problem "Could not read the power status (vcgencmd get_throttled)" <<EOF
The command 'vcgencmd get_throttled' returned nothing. Run it by hand to see the error:
vcgencmd get_throttled
EOF
elif [ "$THROTTLED" = "0x0" ]; then
ok "Power: no under-voltage or throttling since boot (throttled=0x0)"
else
T=$((THROTTLED))
NOW=""; EVER=""
[ $((T & 0x1)) -ne 0 ] && NOW="$NOW under-voltage,"
[ $((T & 0x2)) -ne 0 ] && NOW="$NOW CPU-speed-capped,"
[ $((T & 0x4)) -ne 0 ] && NOW="$NOW throttled,"
[ $((T & 0x8)) -ne 0 ] && NOW="$NOW soft-temperature-limit,"
[ $((T & 0x10000)) -ne 0 ] && EVER="$EVER under-voltage,"
[ $((T & 0x20000)) -ne 0 ] && EVER="$EVER CPU-speed-capped,"
[ $((T & 0x40000)) -ne 0 ] && EVER="$EVER throttled,"
[ $((T & 0x80000)) -ne 0 ] && EVER="$EVER soft-temperature-limit,"

if [ $((T & 0x10001)) -ne 0 ]; then
problem "POWER PROBLEM - the Pi is not getting enough voltage (throttled=$THROTTLED)" <<EOF
Happening right now:${NOW:- nothing}
Has happened since boot:${EVER:- nothing}

Low voltage causes freezes, Wi-Fi dropouts and SD card corruption. It is the most
common reason a Pi camera "just stops working". To fix it, try these in order:
1. Use a shorter USB cable. Long cables (for example 10 ft / 3 m) drop too much
voltage. 3 ft / 1 m or shorter is best.
2. Use a thicker cable. Look for 20 AWG or 22 AWG power wires on the packaging;
cheap charging cables are often 28 AWG and too thin.
3. Change the power supply. Use the official Raspberry Pi 5.1 V 2.5 A micro-USB
supply (it has its own attached cable), not a phone charger.
4. If the camera has to be far from an outlet, run an extension cord to it and
keep the USB cable short, rather than using a long USB cable.
The "since boot" flags stay set until the next reboot. After fixing the power,
reboot and check tomorrow's email, or run: vcgencmd get_throttled
You want to see: throttled=0x0
EOF
else
problem "HEAT - the Pi has slowed itself down (throttled=$THROTTLED)" <<EOF
Happening right now:${NOW:- nothing}
Has happened since boot:${EVER:- nothing}

There was no under-voltage, so this is almost certainly heat. A camera in a sunny
window can get very hot. To fix it:
1. Add a small heatsink to the Pi's processor.
2. Keep direct sun off the Pi board (shade it, or let the case breathe).
3. Check the temperature with: vcgencmd measure_temp
The flags stay set until the next reboot.
EOF
fi
fi

TEMP=$(vcgencmd measure_temp 2>/dev/null | sed -n "s/^temp=\([0-9]*\).*/\1/p")
if [ -n "$TEMP" ]; then
if [ "$TEMP" -ge "$TEMP_WARN_C" ]; then
problem "HEAT - the processor is at ${TEMP} C right now" <<EOF
The Pi slows itself down at 80 C and gets unreliable when it runs this hot for long.
1. Add a small heatsink to the Pi's processor.
2. Keep direct sun off the Pi board.
EOF
else
ok "Temperature: ${TEMP} C"
fi
fi

UV_COUNT=$(journalctl _TRANSPORT=kernel --since "24 hours ago" --no-pager -q 2>/dev/null | grep -ci "undervoltage")
if [ "${UV_COUNT:-0}" -gt 0 ]; then
detail "Kernel logged 'Undervoltage detected' $UV_COUNT time(s) in the last 24 hours."
fi

########################################################################
# 2. Wi-Fi
########################################################################
PS=$(iw dev "$WIFI_IF" get power_save 2>/dev/null)
case "$PS" in
*off*)
ok "Wi-Fi power saving: off" ;;
*on*)
problem "Wi-Fi power saving is ON" <<'EOF'
With power saving on, the Pi's Wi-Fi goes to sleep and the camera drops off the
network. To turn it off for good:
1. Create the setting file:
sudo nano /etc/NetworkManager/conf.d/wifi-powersave-off.conf
2. Put exactly these two lines in it, then save (Ctrl+O, Enter) and exit (Ctrl+X):
[connection]
wifi.powersave = 2
3. Reboot:
sudo reboot
4. After it comes back, check it:
iw dev wlan0 get power_save
You want to see: Power save: off
EOF
;;
*)
problem "Could not read the Wi-Fi power-save setting" <<EOF
The command 'iw dev $WIFI_IF get power_save' returned nothing. Run it by hand to see
the error, and check that the Wi-Fi interface is really called $WIFI_IF:
iw dev
EOF
;;
esac

LINK=$(iw dev "$WIFI_IF" link 2>/dev/null)
SIGNAL=$(echo "$LINK" | awk '/signal:/ { print $2; exit }')
SSID=$(echo "$LINK" | sed -n 's/^[[:space:]]*SSID: //p')
if [ -z "$SIGNAL" ]; then
detail "Wi-Fi: could not read the signal strength (not connected?)"
elif [ "$SIGNAL" -lt "$WIFI_SIGNAL_WARN" ]; then
problem "Weak Wi-Fi signal: $SIGNAL dBm (network: ${SSID:-unknown})" <<EOF
Anything weaker than $WIFI_SIGNAL_WARN dBm is unreliable for video. (Closer to 0 is
stronger: -55 is good, -75 is poor.) Weak signal causes dropped frames and the
"write queue is full" warnings in the MediaMTX log. To improve it:
1. Move the access point closer, or add one near the camera.
2. Reduce the bitrate in /usr/local/etc/mediamtx.yml (rpiCameraBitrate).
3. Metal window frames and low-E window film block Wi-Fi - try another spot.
EOF
else
ok "Wi-Fi signal: $SIGNAL dBm (network: ${SSID:-unknown})"
fi

WD_EVENTS=$(journalctl -t wifi-watchdog --since "24 hours ago" --no-pager -q -o short 2>/dev/null | grep -E "RECONNECT|REBOOTING")
if [ -n "$WD_EVENTS" ]; then
problem "The Wi-Fi watchdog had to step in during the last 24 hours" <<EOF
The Pi lost its network connection and the watchdog reconnected or rebooted it:

$WD_EVENTS

An occasional event is fine. If this shows up every day, check the power problem
and Wi-Fi signal sections of this email, and the access point itself.
To see the full watchdog log:
journalctl -t wifi-watchdog --since "2 days ago"
EOF
else
ok "Wi-Fi watchdog: no reconnects or reboots in the last 24 hours"
fi
if ! systemctl is-active --quiet wifi-watchdog; then
problem "The Wi-Fi watchdog service is not running" <<'EOF'
Start it and make it start at boot:
sudo systemctl enable --now wifi-watchdog
Then check it:
systemctl status wifi-watchdog
You want to see: Active: active (running)
EOF
fi

########################################################################
# 3. MediaMTX
########################################################################
if [ "$(systemctl show -p LoadState --value mediamtx)" != "loaded" ]; then
problem "The mediamtx service does not exist" <<'EOF'
The file /etc/systemd/system/mediamtx.service is missing or broken.
Follow the "Run MediaMTX as a service" part of picam_setup.md again.
EOF
else
RESTART=$(systemctl show -p Restart --value mediamtx)
case "$RESTART" in
always|on-failure|on-abnormal)
ok "MediaMTX restarts automatically if it crashes (Restart=$RESTART)" ;;
*)
problem "MediaMTX will NOT restart by itself if it crashes (Restart=$RESTART)" <<'EOF'
If MediaMTX crashes, the camera stays down until someone reboots the Pi. To fix it:
1. Open the service file:
sudo nano /etc/systemd/system/mediamtx.service
2. In the [Service] section, under the ExecStart= line, add these two lines:
Restart=always
RestartSec=5
3. In the [Unit] section, add this line:
StartLimitIntervalSec=0
4. Save (Ctrl+O, Enter) and exit (Ctrl+X), then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart mediamtx
5. Check it:
systemctl show -p Restart --value mediamtx
You want to see: always
EOF
;;
esac

if [ "$(systemctl is-enabled mediamtx 2>/dev/null)" != "enabled" ]; then
problem "MediaMTX is not set to start at boot" <<'EOF'
Fix it with:
sudo systemctl enable mediamtx
EOF
fi

if ! systemctl is-active --quiet mediamtx; then
problem "MediaMTX is NOT running" <<'EOF'
See why with:
sudo journalctl -u mediamtx -n 50 --no-pager
Try starting it with:
sudo systemctl start mediamtx
EOF
else
ok "MediaMTX is running"
fi

CRASHES=$(journalctl -u mediamtx --since "24 hours ago" --no-pager -q 2>/dev/null | grep -c "Scheduled restart job")
if [ "${CRASHES:-0}" -gt 0 ]; then
problem "MediaMTX crashed and was restarted $CRASHES time(s) in the last 24 hours" <<'EOF'
The automatic restart worked, but MediaMTX should not be crashing. See why with:
sudo journalctl -u mediamtx --since "24 hours ago" --no-pager | grep -iE "ERR|panic|fail"
(If you tested the auto-restart yourself yesterday, that test is counted here.)
EOF
fi
fi

API_OUT=$(curl -s -m 5 "$MEDIAMTX_API/v3/paths/get/$CAMERA_PATH" 2>/dev/null)
if [ -z "$API_OUT" ]; then
problem "Could not ask MediaMTX whether the camera is working" <<'EOF'
The MediaMTX API did not answer on 127.0.0.1:9997. Either MediaMTX is not running
(see above), or the API is not turned on. Check /usr/local/etc/mediamtx.yml:
grep -nE '^(api|apiAddress):' /usr/local/etc/mediamtx.yml
You want to see:
api: true
apiAddress: 127.0.0.1:9997
EOF
elif echo "$API_OUT" | grep -q '"ready":true'; then
READERS=$(echo "$API_OUT" | grep -o '"readers":\[[^]]*\]' | grep -o '"type"' | wc -l)
ok "Camera stream '$CAMERA_PATH' is live, $READERS viewer(s) connected right now"
else
problem "The camera is NOT producing video (stream '$CAMERA_PATH' is not ready)" <<'EOF'
MediaMTX is running but the camera is not sending video. Most often this is the
ribbon cable. To check:
1. See the MediaMTX errors:
sudo journalctl -u mediamtx -n 50 --no-pager
2. Stop MediaMTX (it holds the camera) and ask the Pi to list its cameras:
sudo systemctl stop mediamtx
rpicam-hello --list-cameras
sudo systemctl start mediamtx
You want to see the camera listed (Camera Module 3 shows as "imx708").
3. If no camera is listed: power off, reseat both ends of the ribbon cable
(contacts facing the right way), and power on again.
EOF
fi

########################################################################
# 4. Hardware watchdog
########################################################################
WDT=$(systemctl show -p RuntimeWatchdogUSec --value 2>/dev/null)
if [ -z "$WDT" ] || [ "$WDT" = "0" ] || [ "$WDT" = "off" ]; then
problem "The hardware watchdog is OFF - a frozen Pi will stay frozen" <<'EOF'
With the watchdog on, the Pi reboots itself about 15 seconds after it freezes.
To turn it on:
1. Create the folder and the setting file:
sudo mkdir -p /etc/systemd/system.conf.d
sudo nano /etc/systemd/system.conf.d/watchdog.conf
2. Put exactly these two lines in it, then save (Ctrl+O, Enter) and exit (Ctrl+X):
[Manager]
RuntimeWatchdogSec=15
3. Reboot:
sudo reboot
4. After it comes back, check it:
systemctl show -p RuntimeWatchdogUSec --value
You want to see: 15s
EOF
else
ok "Hardware watchdog: on (reboots the Pi $WDT after a freeze)"
fi

########################################################################
# 5. Disk space
########################################################################
while read -r MNT PCT AVAIL; do
PCT=${PCT%\%}
case "$PCT" in ''|*[!0-9]*) continue ;; esac
FREE=$((100 - PCT))
if [ "$PCT" -gt "$DISK_WARN_PCT" ]; then
problem "Low disk space on $MNT: only $FREE% free ($AVAIL left)" <<EOF
Find what is using the space (biggest folders listed last):
sudo du -xh --max-depth=2 $MNT 2>/dev/null | sort -h | tail -n 15
Common fixes:
sudo apt clean (removes downloaded update files)
sudo journalctl --vacuum-size=20M (shrinks the system log)
EOF
else
ok "Disk $MNT: $FREE% free ($AVAIL left)"
fi
done < <(df -h --output=target,pcent,avail -x tmpfs -x devtmpfs -x efivarfs -x squashfs -x overlay 2>/dev/null | tail -n +2)

########################################################################
# 6. SD card health
# Ordinary SD cards do not report their wear level to Linux, so these are
# the signs that show up before a card fails completely.
########################################################################
ROOT_DEV=$(findmnt -no SOURCE /)
ROOT_OPTS=$(findmnt -no OPTIONS /)
ROOT_PART=$(basename "$ROOT_DEV")
SD_DISK=$(lsblk -no PKNAME "$ROOT_DEV" 2>/dev/null | head -n 1)
SD_DISK=${SD_DISK:-mmcblk0}

case ",$ROOT_OPTS," in
*,ro,*)
problem "CRITICAL: the SD card has been switched to READ-ONLY" <<'EOF'
Linux does this when it finds errors on the card, to protect your data. The card
is probably failing. Plan to replace it:
1. Buy a new high-endurance card.
2. Re-image it with Raspberry Pi Imager and follow picam_setup.md again.
Nothing new can be saved until then (including logs and settings).
EOF
;;
*)
ok "SD card: mounted read-write (normal)" ;;
esac

TUNE=$(tune2fs -l "$ROOT_DEV" 2>/dev/null)
FS_STATE=$(echo "$TUNE" | sed -n 's/^Filesystem state:[[:space:]]*//p')
FS_ERRORS=$(echo "$TUNE" | sed -n 's/^FS Error count:[[:space:]]*//p')
if [ -z "$TUNE" ]; then
detail "SD card: could not read the filesystem details (tune2fs -l $ROOT_DEV)"
elif echo "$FS_STATE" | grep -qi error || [ "${FS_ERRORS:-0}" -gt 0 ]; then
FIRST=$(echo "$TUNE" | sed -n 's/^First error time:[[:space:]]*//p')
LAST=$(echo "$TUNE" | sed -n 's/^Last error time:[[:space:]]*//p')
problem "Filesystem errors recorded on the SD card (state: $FS_STATE, errors: ${FS_ERRORS:-0})" <<EOF
First error: ${FIRST:-unknown}
Last error: ${LAST:-unknown}

The filesystem has been damaged at least once, usually by a power problem or a
worn-out card. The Pi checks and repairs a filesystem marked like this when it
boots, so reboot it:
sudo reboot
Then see whether the errors were cleared:
sudo tune2fs -l $ROOT_DEV | grep -iE "state|error"
If it still says "with errors", or new errors appear in later emails, replace the
SD card.
EOF
else
ok "SD card filesystem: $FS_STATE, no errors recorded"
fi

MMC_HOST=$(basename "$(readlink -f "/sys/block/$SD_DISK/device" 2>/dev/null)" 2>/dev/null | cut -d: -f1)
KERR=$(journalctl _TRANSPORT=kernel --since "24 hours ago" --no-pager -q -o short 2>/dev/null \
| grep -E "${MMC_HOST:-mmc0}: |$SD_DISK|EXT4-fs|I/O error" \
| grep -Ei "error|timeout|timed out|fail|corrupt|medium" | tail -n 10)
if [ -n "$KERR" ]; then
problem "The kernel logged SD card or filesystem errors in the last 24 hours" <<EOF
Last 10 matching messages:

$KERR

Occasional errors can come from a power dip (see the power section). Errors every
day mean the SD card is failing - replace it soon.
EOF
else
ok "SD card: no read/write errors in the kernel log in the last 24 hours"
fi

LIFE_FILE="/sys/fs/ext4/$ROOT_PART/lifetime_write_kbytes"
if [ -r "$LIFE_FILE" ]; then
LIFE_KB=$(cat "$LIFE_FILE")
NOW_S=$(date +%s)
mkdir -p "$STATE_DIR"
SAVE=1
if [ -r "$STATE_DIR/last-write" ]; then
read -r PREV_KB PREV_S <"$STATE_DIR/last-write"
MINUTES=$(( (NOW_S - PREV_S) / 60 ))
if [ "$LIFE_KB" -lt "$PREV_KB" ]; then
detail "SD card writes: counter went backwards (new card?) - starting a new measurement"
elif [ "$MINUTES" -lt $((WRITE_MIN_HOURS * 60)) ]; then
# Too soon to judge: reboots and updates make short windows look busy.
# Keep the old starting point, so the daily report still covers ~24 hours.
SAVE=0
detail "SD card writes: measured once $WRITE_MIN_HOURS hours have passed since the last measurement ($((MINUTES / 60))h $((MINUTES % 60))m so far)"
else
MB_PER_DAY=$(( (LIFE_KB - PREV_KB) * 1440 / MINUTES / 1024 ))
if [ "$MB_PER_DAY" -gt "$WRITE_WARN_MB_PER_DAY" ]; then
problem "Heavy writing to the SD card: about $MB_PER_DAY MB per day" <<'EOF'
This wears the card out faster. Something is writing much more than normal - often a
program logging too much. Find the busiest logs with:
sudo journalctl --since "24 hours ago" --no-pager -o cat | sort | uniq -c | sort -n | tail -n 20
Then re-check the "Reduce writes to the SD card" part of picam_setup.md.
EOF
else
ok "SD card writes: about $MB_PER_DAY MB per day"
fi
fi
fi
[ "$SAVE" -eq 1 ] && echo "$LIFE_KB $NOW_S" >"$STATE_DIR/last-write"
detail "SD card: $((LIFE_KB / 1024 / 1024)) GB written since the card was imaged."
fi

CARD_NAME=$(cat "/sys/block/$SD_DISK/device/name" 2>/dev/null)
CARD_DATE=$(cat "/sys/block/$SD_DISK/device/date" 2>/dev/null)
CARD_SIZE=$(lsblk -dno SIZE "/dev/$SD_DISK" 2>/dev/null | tr -d ' ')
[ -n "$CARD_NAME" ] && detail "SD card: model $CARD_NAME, $CARD_SIZE, made $CARD_DATE."

########################################################################
# 7. General information
########################################################################
detail "Up since: $(uptime -s) ($(uptime -p))"
detail "IP address: $(hostname -I 2>/dev/null)"
detail "OS: $(. /etc/os-release && echo "$PRETTY_NAME"), kernel $(uname -r)"
MTX_VER=$(/usr/local/bin/mediamtx --version 2>/dev/null)
detail "MediaMTX version: ${MTX_VER:-unknown}"

########################################################################
# Build and send the report
########################################################################
if [ "$NPROB" -eq 0 ]; then
SUBJECT="[$HOST] Daily health: OK"
else
SUBJECT="[$HOST] Daily health: $NPROB PROBLEM(S) FOUND"
fi

REPORT="$WORK/report"
{
echo "To: $MAIL_TO"
echo "Subject: $SUBJECT"
echo "Content-Type: text/plain; charset=UTF-8"
echo
echo "Daily health report for $HOST - $(date '+%A %Y-%m-%d %H:%M %Z')"
echo
if [ "$NPROB" -eq 0 ]; then
echo "Everything looks good. No action needed."
else
echo "$NPROB problem(s) need attention. Each one has instructions below."
echo
cat "$PROBLEMS"
fi
echo
echo "PASSED CHECKS"
echo "------------------------------------------------------------"
cat "$OKS"
echo
echo "DETAILS"
echo "------------------------------------------------------------"
cat "$DETAILS"
echo
echo "-- Sent by /usr/local/sbin/picam-health.sh on $HOST"
} >"$REPORT"

if [ "$DRY_RUN" -eq 1 ]; then
cat "$REPORT"
else
msmtp "$MAIL_TO" <"$REPORT"
fi
SCRIPT_END
sudo chmod 755 /usr/local/sbin/picam-health.sh
sha256sum /usr/local/sbin/picam-health.sh

Expected result:

eb90668be34687b7c31d52aa0b6a1960468f737ebc0ddf7ec666044b470e80eb  /usr/local/sbin/picam-health.sh

If the checksum is different, something was lost or changed while pasting. Run step 12.1 again.

12.2. Set the address the report goes to. Open the script:

# On picam03, as your user
sudo nano /usr/local/sbin/picam-health.sh

Near the top (line 13), change MAIL_TO="REPLACE_EMAIL_TO" to your address, for example MAIL_TO="you@example.com". Save (Ctrl+O, Enter) and exit (Ctrl+X). Then check it:

# On picam03, as your user
grep -n '^MAIL_TO=' /usr/local/sbin/picam-health.sh

Expected result: 13:MAIL_TO="<your address>"

12.3. Run the report without sending it, and read through it:

# On picam03, as your user
sudo /usr/local/sbin/picam-health.sh --dry-run

Expected result: a report starting with To: and Subject:. If you’ve done every part above, the subject shows OK, or just 1 PROBLEM(S) FOUND for the crash you caused on purpose in step 8.3. The PASSED CHECKS section lists everything else, and the camera line shows how many viewers are connected. (The “SD card writes per day” check only gives a number once at least 12 hours have passed since its first measurement. Until then, the DETAILS section shows how long it has been. Measuring over a shorter time, for example across the reboots in this guide, would make a quiet card look busy.) Fix anything else it reports before going on.

12.4. Send one real report now to make sure it arrives:

# On picam03, as your user
sudo /usr/local/sbin/picam-health.sh

Expected result: no output, and the email arrives. Tip: in Gmail, create a filter on the subject [picam03] so the daily reports go to their own label.

12.5. Schedule it for 7:00 AM every day:

# On picam03, as your user
sudo tee /etc/systemd/system/picam-health.service >/dev/null <<'EOF'
[Unit]
Description=Send the daily picam health report
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/picam-health.sh
EOF
sudo tee /etc/systemd/system/picam-health.timer >/dev/null <<'EOF'
[Unit]
Description=Daily picam health report at 7:00 AM

[Timer]
OnCalendar=*-*-* 07:00:00
Persistent=true

[Install]
WantedBy=timers.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now picam-health.timer
systemctl list-timers picam-health.timer --no-pager

Expected result: one timer listed, with NEXT showing 7:00 AM tomorrow (or today, if it’s still before 7:00) in your time zone. If the time zone is wrong, fix it with sudo raspi-config → Localisation Options → Timezone. (Persistent=true means that if the Pi was off at 7:00, the report is sent as soon as it’s back on.)

If the daily email stops arriving, treat that as a problem too. It usually means the Pi is off, off the network, or its SD card has failed.

Part 13 – Final check

13.1. Reboot one last time, to prove everything comes back by itself:

# On picam03, as your user
sudo reboot

13.2. Wait 2–3 minutes, then:

  • VLC plays rtsp://<picam03-ip>:8554/cam.
  • Log in and run sudo /usr/local/sbin/picam-health.sh --dry-run. The only problem it should report is the step 8.3 test crash, if that happened in the last 24 hours.

13.3. Back up the finished card. Once everything works, shut down (sudo poweroff), put the SD card in your PC, and use Win32 Disk Imager’s Read button to save an image of it. If the card ever dies, you can write that image to a new card and be back up in minutes, instead of following this guide again.

13.4. Point your NVR (Frigate, Blue Iris, etc.) at rtsp://<picam03-ip>:8554/cam. If the NVR has an RTSP transport option, set it to TCP.

Checklist

  • grep VERSION_CODENAME /etc/os-release shows trixie, and uname -m shows aarch64
  • DHCP reservation created for picam03
  • rpicam-hello --list-cameras lists imx708
  • vcgencmd get_throttled shows throttled=0x0 with the camera mounted and streaming
  • iw dev wlan0 get power_save shows Power save: off
  • Wi-Fi signal is -70 dBm or better
  • swapon --show lists only /dev/zram0, and /var/swap is gone
  • Journal capped at 50 MB; apt/man-db timers disabled
  • mediamtx --version shows v1.21.1, and --validate-conf says the file is valid
  • The stream plays in VLC
  • mediamtx service enabled and running; the SIGKILL test restarted it
  • systemctl show -p RuntimeWatchdogUSec --value shows 15s
  • wifi-watchdog checksum matches, both dry-run tests behave as shown, and the service is running
  • Test email received through msmtp
  • picam-health.sh checksum matches, the dry run looks right, the real email arrived, and the timer is scheduled for 7:00 AM
  • After the final reboot, everything comes back without help
  • SD card image backed up

Maintenance

About once a month, install updates, since automatic updates are turned off in Part 5:

# On picam03, as your user
sudo apt update
sudo apt full-upgrade -y
sudo reboot

After it comes back, check that the stream still plays in VLC.

To update MediaMTX later (replace 1.21.1 with the new version number everywhere below, and read that release’s notes on GitHub for anything that changed):

# On picam03, as your user
cd ~
wget https://github.com/bluenviron/mediamtx/releases/download/v1.21.1/mediamtx_v1.21.1_linux_arm64.tar.gz
wget -O checksums.sha256 https://github.com/bluenviron/mediamtx/releases/download/v1.21.1/checksums.sha256
sha256sum --check --ignore-missing checksums.sha256

Only if that says OK:

# On picam03, as your user
rm -rf ~/mediamtx-new && mkdir ~/mediamtx-new
tar -xzf mediamtx_v1.21.1_linux_arm64.tar.gz -C ~/mediamtx-new
~/mediamtx-new/mediamtx --validate-conf=/usr/local/etc/mediamtx.yml

Only if that says configuration file is valid (this keeps your existing settings file):

# On picam03, as your user
sudo systemctl stop mediamtx
sudo install -m 755 ~/mediamtx-new/mediamtx /usr/local/bin/mediamtx
sudo systemctl start mediamtx
/usr/local/bin/mediamtx --version
systemctl status mediamtx --no-pager

Troubleshooting

If the camera goes down again, collect this before you wipe the card. The logs are kept across reboots (Part 5.2), so the evidence is still there after a restart:

# On picam03, as your user
journalctl --list-boots --no-pager | tail -n 5
sudo journalctl -b -1 -p warning --no-pager | tail -n 50
sudo journalctl -b -1 -n 30 --no-pager
vcgencmd get_throttled
sudo /usr/local/sbin/picam-health.sh --dry-run

-b -1 means “the previous boot”. The last lines before a sudden stop often show the cause, such as under-voltage, SD card errors, or Wi-Fi dropping. If the last lines are ordinary messages with no clean shutdown after them, the Pi lost power or froze.

No video / the health email says the camera isn’t producing video: check sudo journalctl -u mediamtx -n 50 --no-pager. Then run sudo systemctl stop mediamtx followed by rpicam-hello --list-cameras. If no camera is listed, power off and reseat the ribbon cable. Start MediaMTX again with sudo systemctl start mediamtx.

Image keeps drifting in and out of focus (often at night, through glass): Camera Module 3 autofocuses constantly by default. Lock the focus instead by adding these two lines under cam: in /usr/local/etc/mediamtx.yml, indented the same as the other rpiCamera... lines:

    rpiCameraAfMode: manual
  rpiCameraLensPosition: 0.0

0.0 focuses at infinity. 0.5 focuses at 2 m, and 1.0 at 1 m (the value is 1 divided by the distance in metres). Then run /usr/local/bin/mediamtx --validate-conf=/usr/local/etc/mediamtx.yml and sudo systemctl restart mediamtx.

write queue is full warnings, or choppy video: the Wi-Fi can’t keep up. Improve the signal, or lower rpiCameraBitrate (for example to 1000000) and restart MediaMTX.

The Pi keeps rebooting: run journalctl -t wifi-watchdog --since "2 days ago" --no-pager to see whether the Wi-Fi watchdog is doing it, and why. If the router is being replaced or will be down for a long time, pause the watchdog with sudo systemctl stop wifi-watchdog, and start it again afterwards with sudo systemctl start wifi-watchdog.

No daily email: run systemctl list-timers picam-health.timer --no-pager to confirm it’s scheduled, sudo journalctl -u picam-health -n 30 --no-pager to see the last run, and sudo journalctl -t msmtp -n 5 --no-pager to see whether sending failed.

What has Changed (2026-10-11)

I found these problems while following the same steps on other Pis. Here’s what changed, and what to do if your camera was set up with the September 2026 version of this guide.

What changedWhereDo you need to do anything?
The system log wasn’t really kept on the SD card. The setting file was named 10-picam.conf, and a Raspberry Pi OS file named 40-rpi-volatile-storage.conf overrode it, so the log stayed in RAM and was lost at every reboot. It’s now named 90-picam.conf, and step 5.7 checks that it works.Steps 5.2, 5.7Yes, step U1 below
The daily email could falsely report “Heavy writing to the SD card”. It measured over as little as an hour, so a few reboots looked like a busy card. It now waits for at least 12 hours of data.Part 12 (script), step 12.3Yes, step U2 below
Warning against using an internet address (like 8.8.8.8) as the Wi-Fi watchdog target. If your internet goes down, the watchdog would keep rebooting the camera.Step 10.3Only if you changed the target, step U3 below
Note on sharing one Gmail app password between several Pis, and using a separate Gmail account just for alerts.Step 11.1Optional, step U4 below
Clearer expected results for the Wi-Fi watchdog tests (the second test takes 20–30 seconds, and the service status may briefly show no log line).Steps 10.2, 10.4No

Do the steps below in order. They don’t touch the camera settings or MediaMTX, so the stream keeps running throughout (except during the reboot in step U5).

U1. Fix the system log (everyone who followed the earlier version)

U1.1. See which setting file you have:

# On picam03, as your user
ls /etc/systemd/journald.conf.d/

Expected result: 10-picam.conf. (If it already says 90-picam.conf, skip to step U1.3.)

U1.2. Rename it so it sorts after the Raspberry Pi OS file, restart the logging service so it reads the new setting, and then move the log that’s been kept in RAM so far onto the SD card:

# On picam03, as your user
sudo mv /etc/systemd/journald.conf.d/10-picam.conf /etc/systemd/journald.conf.d/90-picam.conf
sudo systemctl restart systemd-journald
sudo journalctl --flush

Expected result: no output. (Without the --flush, the log keeps going to RAM until the next boot, so the reboot in step U5 would still lose this session.)

U1.3. Check it:

# On picam03, as your user
ls -d /var/log/journal
systemd-analyze cat-config systemd/journald.conf | grep -E '^# /|^Storage' | grep -B1 '^Storage' | tail -n 2
sudo journalctl --header | grep -m1 'File path'

Expected result:

/var/log/journal
# /etc/systemd/journald.conf.d/90-picam.conf
Storage=persistent
File path: /var/log/journal/<long id>/system.journal

(The file name at the end may be different, for example user-1000.journal. What matters is that the path starts with /var/log/journal.) If the Storage= line says volatile instead, run systemd-analyze cat-config systemd/journald.conf | grep -E '^# /|^Storage' to see which file sets it, and make sure 90-picam.conf is the last one listed with a Storage= line. If the file path starts with /run/log/journal instead, the log is still in RAM, so run sudo journalctl --flush and check again.

U2. Update the daily health-report script (everyone)

U2.1. Paste the whole step 12.1 block again (it replaces the old script), and check the new checksum:

Expected result:

eb90668be34687b7c31d52aa0b6a1960468f737ebc0ddf7ec666044b470e80eb  /usr/local/sbin/picam-health.sh

U2.2. Pasting the script put the placeholder email address back. Set your address again, exactly as in step 12.2 (line 13 of the script), and check it:

# On picam03, as your user
sudo nano /usr/local/sbin/picam-health.sh
# On picam03, as your user
grep -n '^MAIL_TO=' /usr/local/sbin/picam-health.sh

Expected result: 13:MAIL_TO="<your address>"

There’s nothing else to do. The timer from step 12.5 runs the new script from tomorrow on, and the earlier write measurement it saved is kept.

U3. Check the Wi-Fi watchdog target (only if you followed step 10.3)

# On picam03, as your user
grep -n '^TARGET=' /usr/local/sbin/wifi-watchdog.sh

Expected result: TARGET="${TARGET:-}" (pings your router, the default), or TARGET="${TARGET:-<ip>}" where <ip> is a device on your own network, such as 192.168.1.10. If it’s an internet address such as 8.8.8.8 or 1.1.1.1, change it back. Open the script with sudo nano /usr/local/sbin/wifi-watchdog.sh, change that line to TARGET="${TARGET:-}" (or to a device on your own network that answers pings), save and exit, and then restart the watchdog:

# On picam03, as your user - ONLY if you changed the target
sudo systemctl restart wifi-watchdog

U4. (Optional) Use a separate Gmail account for alerts

Read the note in step 11.1. If you decide to switch, create the new Gmail account and an app password for it (step 11.1), then put the new address and app password into /etc/msmtprc (step 11.4), and send a test email (step 11.5). Your reports can still go to your everyday address.

U5. Reboot, and confirm the log now survives it

# On picam03, as your user
sudo reboot

After 2–3 minutes, log back in and run:

# On picam03, as your user
journalctl --list-boots --no-pager | tail -n 3
sudo /usr/local/sbin/picam-health.sh --dry-run

Expected result:

  • --list-boots lists at least 2 boots. Before the fix, it only ever listed the current one. (If it still lists only one, the --flush in step U1.2 was probably skipped. The log is kept from this boot on, so it will list 2 after the next reboot.) From now on, sudo journalctl -b -1 shows the log from before the last restart, which is what the Troubleshooting section relies on.
  • The health report runs without errors, and the stream still plays in VLC. Its DETAILS section may show SD card writes: measured once 12 hours have passed.... That’s expected right after updating.

References

I want to call out a few resources that helped me get this set up.

Final Thoughts

I was able to get things working but I have seen the Raspberry Pi not able to serve up the stream occasionally but it is starting to look like it may be usable. I’m going to make a case for this so that it can mount on a window where I have the ESP32-CAM cameras mounted. If they run well for a month or so, I may replace the three ESP32-CAM cameras that I have. I’m thinking about adding the ability to move the camera’s as well using some servo motors. I will have to determine what would be required to do that with Blue Iris and the Raspberry Pi GPIO pins.

Categories
Raspberry Pi

PiBox and KubeSail Are No More

Unfortunately PiBox and KubeSail ended operations in September 2025. I had not used my PiBox too much, just as a scratchpad for WordPress. I decided to reflash it with Raspbian. Below are the steps that I took to reuse my PiBox.

Oddly, the only post that I could find about it was at https://lemmy.world/post/38039322. I did go to the PiBox and KubeSail websites and they appear to be gone.

Flashing Raspbian

I’m using a Windows PC to flash the PiBox. I followed the directions at https://docs.kubesail.com/guides/pibox/rpiboot/. One thing that was not clear is that rpiboot.exe should be run before plugging the PiBox into the PC. It looked like there were errors but it still works. The errors are about optional files for configuring the connection, so they may be ignored. The connection is slow even with a high quality USB-C to USB-C cable.

  1. Remove the cover from the PiBox and move the internal switch from “normal” to “rpiboot”.
  2. Remove the carrier board from the backplane
  3. Run rpiboot.exe on the PC.
  4. Plug the PiBox into the PC using a USB-C Cable.
  5. Open Raspberry Pi Imager.
  6. Click Storage and make certain that the PiBox is showing as “RPi-MSD- 0001”.
  7. Click “Choose Device” and select “No filtering”.
  8. Click Operating System and select the 64-bit version you wish to install.
  9. Click the “Next” button to write the image to the PiBox.
  10. Once the image is written and verified, unplug the PiBox from the PC.
  11. Move the internal switch from “rpiboot” to “normal”.
  12. Reassemble the PiBox.

Updating Raspbian

If SSH is enabled and the Wi-Fi settings are configured or it is connected via ethernet , you may open an SSH session to run commands. If not, you may need to connect a monitor and keyboard to continue.

sudo apt update
sudo apt upgrade

Setup SSD Drives

I have two 1TB Samsung SSDs installed. I wanted to wipe them and remount them to the PiBox. Below are the steps that I followed.

/dev/sda

  1. Run the parted tool
    sudo parted /dev/sda
  2. Change the units used by parted
    unit s
  3. Delete the existing partitions
    • Run the print command to list all the partitions.
      print
    • Remove each partition (Repeat if more than one partition is present.)
      rm 1
  4. Create a single partition for the whole drive
    mkpart primary ext4 0% 100%
  5. Exit parted
    quit
  6. Format the partition
    sudo mkfs.ext4 -L data1 /dev/sda1
  7. Create a mounts point for the drive (The rancher mount point is only so it will display on the LCD display.)
    sudo mkdir -p /var/lib/rancher

/dev/sdb

  1. Run the parted tool
    sudo parted /dev/sdb
  2. Change the units used by parted
    unit s
  3. Delete the existing partitions
    • Run the print command to list all the partitions.
      print
    • Remove each partition (Repeat if more than one partition is present.)
      rm 1
  4. Create a single partition for the whole drive
    mkpart primary ext4 0% 100%
  5. Exit parted
    quit
  6. Format the partition
    sudo mkfs.ext4 -L data2 /dev/sdb1
  7. Create a mount point for the drive
    sudo mkdir -p /mnt/data2

Update fstab

  1. Open text editor to edit fstab
    sudo nano /etc/fstab
  2. Add the following lines
    /dev/sda1 /var/lib/rancher ext4 defaults,nofail,noatime,discard,errors=remount-ro 0 0

    /dev/sdb1 /mnt/data2 ext4 defaults,nofail,noatime,discard,errors=remount-ro 0 0

Enabling PWM Fan Support

  1. Clone the PiBox OS Scripts
    git clone https://github.com/kubesail/pibox-os.git
  2. Change to the Fan folder
    cd pibox-os/pwm-fan
  3. Uncompress the Fan scripts
    tar zxvf bcm2835-1.68.tar.gz
  4. Change to the Fan scripts folder
    cd bcm2835-1.68
  5. Run the make command
    ./configure && make && sudo make install
  6. Go to the parent folder
    cd ..
  7. Make and run the installation
    make && sudo make install
  8. The fan should run quieter now.

Optimize system for EMMC

  1. Store system logs in memory, instead of writing to disk, and lower log verbosity
    sudo sed -i "s/#Storage.*/Storage=volatile/" /etc/systemd/journald.conf

    sudo sed -i 's/.MaxLevelStore.*/MaxLevelStore=info/' /etc/systemd/journald.conf

    sudo sed -i 's/.MaxLevelSyslog.*/MaxLevelSyslog=info/' /etc/systemd/journald.conf

    sudo systemctl restart systemd-journald.service
  2. Disable swapfile
    sudo swapoff -a
    sudo dphys-swapfile swapoff
  3. Mount /var/tmp as tmpfs filesystems to extend EMMC lifetime
    echo "tmpfs /var/tmp tmpfs defaults,noatime,nosuid,nodev,noexec,mode=0755,size=1M 0 0" | sudo tee -a /etc/fstab

Enabling the 1.3” LCD display

  1. Return to the home directory
    cd ~
  2. Install additional drivers for Raspbian / Pibox-OS
    sudo apt-get install raspberrypi-kernel-headers

    pushd pibox-os/st7789_module

    make

    sudo mv fb_st7789v.ko /lib/modules/"$(uname -r)"/kernel/drivers/staging/fbtft/fb_st7789v.ko

    popd

    sudo dtc --warning no-unit_address_vs_reg -I dts -O dtb -o /boot/overlays/drm-minipitft13.dtbo pibox-os/overlays/minipitft13-overlay.dts
  3. Update config.txt
    cat <> /boot/firmware/config.txt
    dtoverlay=spi0-1cs
    # dtoverlay=dwc2,dr_mode=host
    hdmi_force_hotplug=1
    dtoverlay=drm-minipitft13,rotate=0,fps=60
    EOF
  4. Download pibox-framebuffer binary
    curl -s https://raw.githubusercontent.com/kubesail/pibox-os/main/update-framebuffer.sh | sudo bash

Optional if you want the ability to control the top RGB LED

  • Install the RPi.GPIO library.
    sudo apt install python3-RPi.GPIO
  • Open Nano to create a script file.
    nano blinkLED.py
import RPi.GPIO as GPIO
import time

# Red 17
# Green 27
# Blue 23
leds = [17, 27, 23]

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
for led in leds:
    GPIO.setup(led, GPIO.OUT)

for led in leds:
    for i in range(3):
        GPIO.output(led, GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(led, GPIO.LOW)
        time.sleep(0.5)

# Commented next line so the LED stays off.
# Added GPIO.setwarnings(False) at the start of the script
# to hide warnings that the channel is in use on future runs.
# GPIO.cleanup()
  • Run the script and observe the top LED on the front of the PiBox.
    python ./blinkLED.py

Categories
Home Assistant Raspberry Pi

Home Assistant on Raspberry Pi

I can’t believe I have not written up my Home Assistant setup. Perhaps that is a good thing as I needed to become comfortable with the setup and how to use it. I’ve been using it for nearly a year at this point. What prompted me to do a writeup now is I’m setting up another one for my mom so I can control some things remotely and to help her manage some things around the house.

My current installation was on a Raspberry Pi 4 setup to boot from a USB drive. This has been working well, but I’m setting up a Raspberry Pi 5 for mom’s installation with a NVMe SSD drive as the boot device.

Bill of Materials for this project

All product links are affiliate links to Amazon Products.

Installation Steps

  1. The first step is fitting the NVMe hat to the Raspberry Pi 5. I have found that the 3cm cable that came with my hat did not work properly. I purchased a set of cables to swap out and found that the 3cm cable in the new set had the same issue. I tried the longer 5cm cable and the hat worked well. I would have thought that a shorter cable would be better but I suspect that there is an impedance or timing mismatch with the shorter cable.
  2. Write the image to the NVMe drive using the steps outlined at https://www.martinrowan.co.uk/2024/02/installing-home-assistant-on-raspberry-pi-5-nvme-storage/.
  3. Once my Raspberry Pi rebooted, it was running Home Assistant.
    The Raspberry Pi boot screen for Home Assistant
  4. I opened a browser window and navigated to http://homeassistant.local:8123/. It took a couple of minutes before Home Assistant fully loaded.
    Home Assistant Browser Window - Retrying to connect
    Home Assistant Browser Window - Login Screen
  5. Click on the “Create my smart home” button.
  6. Create a user by enter the information and clicking the “Create account” button.
    Home Assistant Browser Window - Create User
  7. Set the home location. You may try using your address. If that does not work, use the latitude and longitude. Once entered, click the “Next” button.
    Home Assistant Browser Window - Home Location
  8. Select information that you are willing to share with the Home Assistant team and click the “Next” button.
    Home Assistant Browser Window - Help us help you
  9. Click the “Finish” button on the devices screen.
    Home Assistant Browser Window - We found compatible devices!

Home Assistant is now setup and ready to be configured.

Categories
Raspberry Pi

Disable On-Screen Keyboard

When using a touchscreen with your Raspberry Pi, depending on the screen size, you may soon be annoyed with the amount of screen real estate the on-screen keyboard utilizes. While it can be useful if you do not have a keyboard attached, it can make it difficult if not impossible to complete forms an know what you are entering.

You may want to connect a physical keyboard via USB or Bluetooth but find that the on-screen keyboard pops up anyway. How do you disable it? Follow the steps below to disable the on-screen keyboard for touch screens.

  1. Go to the Start Menu and select Preferences > Raspberry Pi Configuration.
    Menu - Raspberry Pi Configuration
  2. Go to the Display Tab, then select “Disabled” from the On-screen Keyboard option.
    Disable on-screen Keyboard
  3. Click the “OK” button.
  4. If prompted to reboot, select “Yes”
    Reboot confirmation

You will find that the on-screen keyboard no longer blocks the screen content.

Categories
Photography Raspberry Pi

Revisit Photobooth Project

I had built a Photobooth in 2019 based on Wyolum’s Raspberry Pi Photobooth project as posted in Make Magazine. Caroline Dunn made a YouTube video of her build, which explains different aspects of the project.

I had two events where the photobooths could be used, so I pulled them off the self and tested them. The first one started just fine, but the SD Card soon became corrupted and would no longer boot. I flashed the SD Card with the latest Raspbian OS and installed the software, but it would not run. There have been changes to Raspbian and in particular with Python on the latest Raspbian build which will not allow the previous code to run. The Wyolum TouchSelfie project has not been updated in 6 years, so there have been no updates to allow it to run on the latest Raspbian distribution. There is the option of using the older distribution, but I did not want to take that path.

I moved forward by attempting to make fixes to the software but ran into several issues, which led me to do a complete rewrite. Not all features are working at this time, but the important ones, such as taking the photo, creating collages & animated GIFs, adding frames with logos, and uploading the photos to a Google Album are working. I also have some ideas for taking the project further, which will be covered at the end.

Bill of Materials (BOM)

  • Raspberry Pi 3 or newer
    I would recommend a Raspberry Pi 4 or 5 as it may handle rendering of the photos better than the Raspberry Pi 3, although the Raspberry Pi 3 works well.
  • Raspberry Camera Module 2 or newer with cable
    NOTE: Raspberry Pi 5 requires a different cable than earlier versions.
  • Raspberry Pi 7″ touch screen
    NOTE: The original Raspberry Pi 7″ touch screen (800×600) was used but the new version 2 (720×1280) will work as well. Raspberry Pi 5 requires a different cable than earlier versions.
  • Power Supply
    NOTE: The official Raspberry Pi Power Adapter for the Raspberry Pi 3 was not sufficient. It required a 12W iPad charger to work. Amazon has some Anker 2-Pack Dual Port 12W USB A Charger Blocks that I purchased, which work as well.
  • (Optional) Waveshare Barcode Scanner Module
    Plugs into Raspberry Pi USB port and Acts like a keyboard. Makes it convenient to run specific commands or shortcuts without connecting a keyboard. Also may be used to enter email addresses for sending photos.
  • (Optional) GPS USB Dongle
    Allows geotagging of photos
  • 1/8″ plywood cut with the SVG files at https://github.com/richteel/Photobooth
    Path: /fabricate/Rich/

Write the Raspbian OS to an SD Card

  1. Write the latest Raspbian image to an SD Card using Raspberry Pi Imager.
    • Click the “Choose Device” button and select “Raspberry Pi 3” from the list.
      List of Boards in the Raspberry Pi Imager software
    • Click the “Choose OS” button and select “Raspberry Pi OS (64-bit)”.
      First list of Operating Systems in the Raspberry Pi Imager software
    • Click the “Choose Storage” button and select the SD Card from the list.
      List of available removable storage devices in the Raspberry Pi Imager software
    • Click the “Next” button and follow the prompts. Once the card is validated and you receive a confirmation message that the image has been written to the SD Card, remove the card from the computer and move it to the Raspberry Pi Zero 2 W board and power it on.
      Raspberry Pi Imager confirmation message
  2. Once the Raspberry Pi boots up, and finishes setting up, log into the Raspberry Pi. You may do this directly with an attached keyboard and monitor or by connecting with SSH.
  3. Connect to the Raspberry Pi or use a connected keyboard.
    • If you had edited the settings prior to writing the OS to the SD Card and the setting included the information to connect to your WiFi and enabled SSH, you may SSH into the Raspberry Pi to run the following commands.
    • If you did not setup those options or if wish to use Raspberry Pi Connect, you may connect a keyboard to the Raspberry or use the on-screen keyboard.
      Recommend using a connected keyboard if you working directly with the Pi.
    • Recommend Turning on Raspberry Pi Connect, Sign In, and work from another computer.

Update Raspbian and Install Packages

  1. Update the Raspberry Pi OS and install packages by running the following code.
    sudo apt update -y && sudo apt upgrade -y

    # Install ImageTk, Image from PIL
    sudo apt install python3-pillow
    sudo apt install python3-pil
    sudo apt-get install python3-pil.imagetk
    sudo apt install python3-numpy

    # Install ImageMagick for the 'Animation' mode
    sudo apt-get install imagemagick

    # Install picamera2 depenancies
    sudo apt install libcap-dev
    sudo apt install libcamera-dev
    sudo apt install libcamera-apps

    # Add support for Pi Camera ?
    sudo apt install python3-picamera2

    # Add support to write extended EXIF data
    sudo apt install python3-piexif

    # Create virtual environment for gdata
    python -m venv ~/photobooth
    source ~/photobooth/bin/activate

    # Install google data api and upgrade it
    pip install gdata
    pip install --upgrade google-api-python-client
    pip install --upgrade oauth2client

    # Install ImageTk, Image from PIL
    # Appears we need to do this in the virtual environment too.
    pip install pillow
    # pip install numpy

    # Leave the virtual environment
    deactivate

Setup Google API Access

  1. Open a web browser and log into your Google Account. If you do not have one, create one.
    NOTE: You may want to create a separate account, just for the photobooth to keep it separate from your main account.
  2. Go to the Google API Console
    • If you are not in the correct profile, select the correct profile.
  3. Create a project
    Click on the project selector near the top of the screen, then click the “New project” button in the dialog.
    Google API Console New Project
  4. Enter a name for the project and click the “Create” button.
    Google API New Project Name
  5. Make certain the project is the selected project in the Console. Look at the upper left corner. If the project is not displayed, click and select the project before continuing.
    Google API Select Project
  6. Add access rights to the services you need (namely Gmail Service and Google Photos Library Service)
  7. Click on the “+ Enable APIs and services” button
    Google API Enable APIs and Services
  8. Search for Gmail using the Search box and click on “Gmail API”
    GMail API
  9. Click the “Enable” button to enable the Gmail API
    Enable GMail access
  10. Return to the Google API Console, click the “+ Enable APIs and services” button, search for Photos, and click on “Google Photos Library API”
    Select Photos API
  11. Click the “Enable” button to enable the Google Photos Library API
    Photos API Enable
  12. Return to the Google API Console and select “Credentials” on the left menu.
  13. Click the “+ Create credentials” button on the top and select “OAuth client ID”
    Add OAuth
  14. If you are prompted to “Configure consent screen”, do that, then return to the next step.
    NOTE: The values entered are not important as you will be the only one authenticating to the app.
    • App Information
      • App Name: Only you will see this.
      • User support email: Enter a valid email.
    • Audience
      • External is the only option available
    • Contact Information
      • Enter a valid email
    • Finish
      • Check the checkbox and click the create button
  15. Select the “Desktop app” for Application type, enter a name, and click the “Create” button.
    OAuth Client Id
  16. Copy the Client ID, Client secret, and Download JSON. Keep the information in a safe place.
    OAuth keys
  17. Back to the Credentials, click on the OAuth Client ID that you created.
  18. Select “Audience” from the left panel, and click the “Publish app” button.
  19. Click on the “Confirm” link to publish the app.

Get the Code

  1. Grab the code from GitHub
    wget -P ~/ https://github.com/richteel/Photobooth/archive/refs/heads/main.zip
    unzip ~/main.zip
    mv ~/Photobooth-main ~/pb

    mv ~/pb/home/pi/TouchSelfie ~/
    mv ~/pb/home/pi/Desktop/Photobooth.desktop ~/Desktop/Photobooth.desktop
    mv ~/pb/home/pi/.config/autostart ~/.config/
    mv ~/pb/home/pi/*.sh ~/
    mv ~/pb/home/pi/*.py ~/

    # Clean up empty folders & zip file
    rm -r ~/pb/home
    rm ~/main.zip

    # Make BASH scripts and desktop files executable
    chmod +x ~/*.sh
    chmod +x ~/TouchSelfie/*.sh
    chmod +x ~/Desktop/*.desktop
    chmod +x ~/.config/autostart/*.desktop

    # cd ~/TouchSelfie
    # ./setup.sh

Disable On-Screen Keyboard (Optional)

This step is optional but highly recommended as the on-screen keyboard takes up a significant amount of available screen space, making it difficult to navigate and see the screen. This is especially true with the original Raspberry Pi Touch Screen.

  1. We will turn off the on-screen keyboard to get it out of the way.
    Screenshot of the Raspberry Pi Desktop with on-screen keyboard
    • Go to the Start Menu and select Preferences > Raspberry Pi Configuration.
      Menu - Raspberry Pi Configuration
    • Go to the Display Tab, then select “Disabled” from the On-screen Keyboard option.
      Disable on-screen Keyboard
    • Click the “OK” button.
    • When prompted to reboot, select “Yes”
      Reboot confirmation
    • When the Raspberry Pi reboot, the Photobooth application should be running.
      Photobooth application screenshot
    • Press <ctrl> + q to exit the application and click the “Yes” button on the dialog.
      Photobooth app showing quit dialog

    Authenticate to the Google APIs

    1. Copy the JSON file that was downloaded from the Google API Console and rename it to “google_client_id.json”.
      • If you did not download it, go to the Google API Console, go to Credentials, then click on the Client Id (link).
      • You may transfer the JSON file to the Raspberry Pi using an application such as WinSCP or other method and place it in the /home/pi/TouchSelfie/scripts/ folder.
    2. Copy the “google_client_id.json” file to “OpenSelfie.json”. You may copy it by running the following command in the terminal.
      cp ~/TouchSelfie/scripts/google_client_id.json ~/TouchSelfie/scripts/OpenSelfie.json

      NOTE: There should be two JSON files in the scripts folder, google_client_id.json and OpenSelfie.json.
    3. On the Raspberry Pi, open a terminal and type the following commands to run the setup application.
      cd TouchSelfie
      ./setup.sh

      Terminal window to run setup.sh
    4. The “TouchSelfie – Options” window opens. Select the options you wish to enable, then click the “Next” button.
      NOTES:
      – It is recommended NOT to enable the software keyboard if using the Raspberry Pi touchscreen as screen resolution is too small.
      – Image Effects is not implemented at this time so enabling it has no impact.
    5. The “TouchSelfie – Credentials” window opens. Click the “Get AppID” button.
    6. The “TouchSelfie – Credentials” second window opens. Click the “Launch browser” button.
    7. The browser will open prompting you to log into Google.
    8. Enter your Google Account and click the “Next” button.
    9. Enter your account password and click the “Next” button.
    10. If you have 2-step verification setup, then complete the 2-step verification process.
    11. You will see a page stating that Google hasn’t verified this app. Click the “Advanced” link at the bottom of the page.
    12. Click on the “Go to … (unsafe)” link at the bottom of the page.
    13. Check the “Select all” checkbox to select all the permissions, then click the “Continue” button.
    14. The browser window will state “The authentication flow has completed” and the TouchSelfie – Options screen will be displayed.
      NOTE: There is a new file in the /TouchSelfie/scripts/ folder named, google_credentials.dat.
    15. Modify any options if necessary and click the “Next” button.
    16. Enter the Google Account, verify that the Credentials have green checkmarks, and click the “Next” button.
    17. Enter an email address that you can check and change any items you wish for the test email. Click the “Send test email to test the Gmail API connection. Check your email for the test message, then click the “Next” button.

    18. The select Photo Album screen needs some work yet. The list must be navigated with the up/down keys on the keyboard and selection is done with the spacebar. This will be fixed in a future release. If you do not have an album, choose “<Create New>” and click tab to the “Select” button and press the spacebar. It will create a new album named “TouchSelfie” and add a small single colored small image to the album. Do not delete the image from the album unless you have uploaded another image. There must be an image in the album.
    19. Click the “Test Upload” button. You may go out to Google Photos to verify that the album has been created and the image was uploaded.

    20. If necessary, modify the options for snapshots, then click the “Save” button.
    21. You may now close the terminal window and browser.

    Customizing the frames for the photos and collages

    The \TouchSelfie\logos\ folder has the frames that are applied over the photos.

    • Single Photo: single_logo.png
    • 4 Collage Photo: collage_four_square_logo.png
    • 9 Collage Photo: collage_nine_square_logo.png

    There is a Frames.svg that may be used for creating or updating the photos. The folder also contains some examples from various events.

    Configuration File Settings

    The configuration file is in the /TouchSelfie/scripts/ folder and is named, configuration.json. It is a JSON file with several options.

    • archive_to_all_usb_drives
      • Type: boolean
      • Values: true or false
      • Allows archiving of photos to a USB drive if one is attached to the Raspberry Pi
      • NOTE: This functionality is not behaving properly. Some photos are archived but not all. All are written to the SD card though. This will be fixed in a later release.
    • cameraMake
      • Type: string
      • A quoted string value indicating the make of the camera. You may set this to any value you wish. The value is written to the image Exchangeable Image File Format (EXIF) data.
    • cameraModel
      • Type: string
      • A quoted string value indicating the model of the camera. You may set this to any value you wish. The value is written to the image EXIF data.
    • countdown_before_snap
      • Type: integer
      • The seconds between button press and taking the photo. There needs to be images in the /TouchSelfie/scripts/resources/ folder to support the count. Currently there are 5 images named count_down _1.png to count_down _5.png. The images are 128×128 pixels in size.
    • countdown_inter_snap
      • Type integer
      • The seconds between taking photos for collage images
    • email_body
      • Type: string
      • A quoted string value for the body of the emails being sent from the application.
    • email_subject
      • Type: string
      • A quoted string value for the subject of the emails being sent from the application.
    • enable_effects
      • Type: boolean
      • Values: true or false
      • Indicates if the effects button will be shown.
    • enable_email
      • Type: boolean
      • Values: true or false
      • Indicates if the mail button will be shown.
    • enable_email_logging
      • Type: boolean
      • Values: true or false
      • Indicates if emails will be logged to a file.
    • enable_hardware_buttons
      • Type: boolean
      • Values: true or false
      • Indicates if hardware buttons are available for application functions. Hardware buttons are not supported at this time.
    • enable_print
      • Type: boolean
      • Values: true or false
      • Indicates if the print button will be shown
    • enable_upload
      • Type: boolean
      • Values: true or false
      • Indicates if photos will be uploaded to a Google Album
    • full_screen
      • Type: boolean
      • Values: true or false
      • This is deprecated and may be removed from future releases.
    • gmail_user
      • Type: string
      • A quoted value for the Google Account email address.
    • google_photo_album_id
      • Type: string
      • A quoted text value that is the id of the Google Album. It is a Globally Unique Identifier (GUID) that is inserted by the setup program.
    • google_photo_album_name
      • Type: string
      • A quoted text value of the name of the Photo Album.
    • imageArtist
      • Type: string
      • A quoted string for the name of the artist/photographer. You may set this to any value you wish. The value is written to the image EXIF data.
    • imageComment
      • Type: string
      • A quoted string for the note or comment to be added to the EXIF data. You may set this to any value you wish. The value is written to the image EXIF data.
    • imageDescription
      • Type: string
      • A quoted string for the description to be added to the EXIF data. You may set this to any value you wish. The value is written to the image EXIF data.
    • imageKeyWords
      • Type: string
      • A quoted string for comma separated keywords to be added to the EXIF data. You may set this to any value you wish. The value is written to the image EXIF data.
    • local_archive
      • Type: boolean
      • Values: true or false
      • If true, photos are saved to the SD card in addition to being uploaded to the Google Album. Recommended to have this value set to true.
    • local_archive_dir
      • Type: string
      • A quoted string with the path to the local archive folder.
    • location_lat
      • Type: float
      • A quoted decimal value for the latitude. The value is written to the image EXIF data.
    • location_long
      • Type: float
      • A quoted decimal value for the longitude. The value is written to the image EXIF data.
    • logo_file
    • selected_printer
    • snap_caption
    • software
      • Type: string
      • A quoted string for the software to be added to the EXIF data. You may set this to any value you wish. The value is written to the image EXIF data.
    • test_email_address
      • Type: string
      • A quoted value for the test email address. This is only used in the setup.

    (Optional) Stop Execution File dialog box

    You may want to stop seeing the Execute File dialog when clicking on the Photobooth desktop icon. Below are the steps to stop displaying the dialog.

    1. Click on the File Manager at the top of the screen.
    2. From the menu, select Edit > Preferences
    3. With “General” selected on the left panel, check the “Don’t ask options on launch executable file” option.
    4. Close File Manager and click on the Photobooth icon on the desktop. The application will launch.

    Next Steps

    There are several things that need to be completed and some changes that plan to be made. Below is a list of those items.

    • The code is looking for the client id in two different JSON files. Modify the code so only one copy of the JSON file is needed. The two JSON files are:
      • google_client_id.json
      • OpenSelfie.json
    • Use connected GPS to geotag photos. Currently the configuration file has location_lat and location_long which are used to geotag photos, but that requires that the configuration file is updated when the photobooth is brought to a new location.
    • Add text to the buttons to make it easier for users to know what each button is used for. An alternative may be to add an info button.
    • Email photo(s) function. Would like the option to review and select photos to email.
    • Add effects back into the application.
    • Add support to print photos.
    • Improve the UI to let people know when a photo will be taken for each image of the collage and animation. Not certain how to make this intuitive.
    • Fix the issues with the setup application and add additional options.

    Categories
    Project Raspberry Pi

    Updated Raspberry Pi Camera Wi-Fi Monitor Script

    My Raspberry Pi cameras have been running well for a while now, but I’ve had one camera that seems to be having issues. I did have some issues earlier with two camera and believe it was due to using cheap Micro Center SD cards and having the Wi-Fi monitoring script write to the card every 5 minutes. I used new cards and did not include the Wi-Fi monitoring script. All was well until a couple of weeks ago, when one camera, that is closest to my Wi-Fi router, would not show up in Blue Iris. I unplugged it and plug it in to hard reboot it, but it still would not come back up.

    I finally pulled it out to take a look at it but it seemed to work just fine. It may be that the CD Card was not seated properly, but uncertain why it worked then did not work. If and only if the last state and current state are different, will it update the state file and create a log entry. Below is my resulting script. This is much better than my original version. It is easier to view the log and see when the Wi-Fi went down and when it reconnected.

    #!/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"
    STATEFILE="/var/log/wifistate.txt"
    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'`
    CURRENTSTATE=""
    LASTSTATE=""
    
    if [ -f ${STATEFILE} ]; then
        LASTSTATE=$(cat ${STATEFILE})
    fi
    
    # 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
        CURRENTSTATE="UP"
        UPDOWN="\033[32m UP  \033[0m"
    else
        CURRENTSTATE="DOWN"
        UPDOWN="\033[31mDOWN \033[0m"
        STATUS+=" Attempting wlan0 restart"
        # Restart the wireless interface
        sh -c 'ifconfig wlan0 down; sleep 5; ifconfig wlan0 up'
    fi
    if [ "$CURRENTSTATE" != "$LASTSTATE" ]; then
        echo -e $CURRENTSTATE > $STATEFILE
        echo -e "WiFi @ $DATE: $UPDOWN $TEMP $STATUS" >> ${LOGFILE}
    fi
    echo -e "WiFi @ $DATE: $UPDOWN $TEMP $STATUS"
    
    
    # Check daemon
    # Name of the daemon to check
    DAEMON_NAME="mediamtx"
    LOGFILE="/var/log/mediamtx.log"
    STATEFILE="/var/log/mediamtxstate.txt"
    STATUS=""
    CURRENTSTATE=""
    LASTSTATE=""
    
    if [ -f ${STATEFILE} ]; then
        LASTSTATE=$(cat ${STATEFILE})
    fi
    
    # Check if the daemon is running
    if pgrep -x "$DAEMON_NAME" > /dev/null; then
        CURRENTSTATE="RUNNING"
        STATUS="$DAEMON_NAME is running"
    else
        CURRENTSTATE="NOT RUNNING"
        STATUS="$DAEMON_NAME is not running, restarting..."
        # Use appropriate command to restart the daemon
        sudo systemctl restart "$DAEMON_NAME"
    fi
    if [ "$CURRENTSTATE" != "$LASTSTATE" ]; then
        echo -e $CURRENTSTATE > $STATEFILE
        echo -e "MediaMTX @ $DATE: $STATUS" >> ${LOGFILE}
    fi
    echo -e "MediaMTX @ $DATE: $STATUS"
    
    Categories
    Project Raspberry Pi

    Raspberry Pi Camera Solution

    After looking at the stability issue with MediaMTX, Raspberry Pi Camera Module with Raspberry Pi Zero 2 W, and Blue Iris, I have uncovered what looks like a simple fix.

    I was doing some searching on my issue with the Pi Cameras and found a post with the rpiCameraBitrate set to 1,500,000 rather than 5,000,000, which is the default value. Instead of editing the rpiCameraBitrate value on line 591 of the mediamtx.yml file, I added a new line in the paths > cam block at the end of the file. The end of my mediamtx.yml file now looks like the following.

    paths:
      # example:
      # my_camera:
      #   source: rtsp://my_camera
    
      # Settings under path "all_others" are applied to all paths that
      # do not match another entry.
      cam:
        source: rpiCamera
        rpiCameraWidth: 1280
        rpiCameraHeight: 720
        rpiCameraFPS: 15
        rpiCameraBitrate: 1000000
      all_others:

    Run the following command to edit the mediamtx.yml file.
    nano /home/pi/mediamtx/mediamtx.yml

    My cameras have been stable now for several weeks.

    I did want to show the light sensitivity difference with the Raspberry Pi Cameral Module v3 and ESP32-CAM modules. The top row in the screen capture shows the Raspberry Pi cameras and the bottom row shows the ESP32-CAM modules. The scenes are the same top to bottom.

    Screen capture from Blue Iris software showing the Raspberry Pi Camera and ESP32-CAM video.
    Categories
    Project Raspberry Pi

    Raspberry Pi Camera Status

    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.

    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.

    1. Open a SSH or terminal session to the Raspberry Pi
    2. Open the text editor to create the script file.
      sudo nano /usr/local/bin/wifi_monitor.bsh
    3. 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"
    4. 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.
    5. Save the file and exit the Nano editor.
    6. Make the script executable by running the following command.
      sudo chmod 744 /usr/local/bin/wifi_monitor.bsh
    7. Test the script by running the following command.
      sudo /usr/local/bin/wifi_monitor.bsh
    8. 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

    1. Run the following command to bring up the CRON editor.
      sudo crontab -e
    2. 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]:
      
    3. At the end of the file, add the following line to run the script every 5 minutes.
      */5 * * * * /usr/local/bin/wifi_monitor.bsh
    4. 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
    5. Save the file and exit the text editor.
    6. 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
    7. 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.

    Categories
    Project Raspberry Pi

    Raspberry Pi Camera and Blue Iris

    I wanted to look at replacing some ESP32-CAM cameras that I have in my windows with some Raspberry Pi Cameras. My hope is that I can get better quality and potentially have some usable video at night. I looked at several options but none seem to be satisfactory but the most promising was the open source project, MediaMTX. It was simple to get setup as it just worked. The issue that I have is even tweaking it to only serve on RTSP stream at 640×480, it could still not deliver a continous smooth stream. I constantly received “write queue is full” warnings. I did serveral things in an attempt to allow it to run smoother, but nothing seemed to help. To be fair, I did start with a Raspberry Pi Zero 2 W, which may be underpowered for this task.

    Parts Used in this Project

    Here are some links to the items that I used in this project.

    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. I have also provided links to PiShop.us in case items are out of stock at Amazon.

    Steps to setup the software

    1. Write the latest Raspbian image to an SD Card using Raspberry Pi Imager.
      • Click the “Choose Device” button and select “Raspberry Pi Zero 2 W” from the list.
        List of Boards in the Raspberry Pi Imager software
      • Click the “Choose OS” button and select “Raspberry Pi OS (other)”, then “Raspberry Pi OS Lite (64-bit)”.
        First list of Operating Systems in the Raspberry Pi Imager software Second list for other Operating Systems in the Raspberry Pi Imager software
      • Click the “Choose Storage” button and select the SD Card from the list.
        List of available removable storage devices in the Raspberry Pi Imager software
      • Click the “Next” button and follow the prompts. Once the card is validated and you receive a confirmation message that the image has been written to the SD Card, remove the card from the computer and move it to the Raspberry Pi Zero 2 W board and power it on.
        Raspberry Pi Imager confirmation message
    2. Once the Raspberry Pi boots up, and finishes setting up, log into the Raspberry Pi. You may do this directly with an attached keyboard and monitor or by connecting with SSH.
    3. Update the Raspberry Pi OS by running the following code.
      sudo apt update -y && sudo apt upgrade -y
    4. Download MediaMTX for the Raspberry Pi Zero 2 W by running the following command.
      wget https://github.com/bluenviron/mediamtx/releases/download/v1.10.0/mediamtx_v1.10.0_linux_arm64v8.tar.gz
    5. Make a new directory for the MediaMTX application by running the following command.
      mkdir mediamtx
    6. Unpack the MediaMTX application release into the application folder with the following command.
      tar -C mediamtx -xvzf mediamtx_v1.10.0_linux_arm64v8.tar.gz
    7. Change to the application directory.
      cd mediamtx
    8. Edit the YAML file.
      nano mediamtx.yml
    9. It is recommended to make the following changes to the file as well.
      • Find “protocols: [udp, multicast, tcp]” and change to “protocols: [tcp]”
        (~Line 230)
      • Find “rtmp: yes” and change yes to no
        (~Line 263)
      • Find “hls: yes” and change yes to no
        (~Line 283)
      • Find “srt: yes” and change yes to no
        (~Line 395)
    10. Add these lines at the end of the YAML file to provide a stream for the Raspberry Pi Camera.
      (~Line 698)
      paths:
      # example:
      # my_camera:
      # source: rtsp://my_camera

      # Settings under path "all_others" are applied to all paths that
      # do not match another entry.
      cam:
      source: rpiCamera
      rpiCameraWidth: 1280
      rpiCameraHeight: 720
      rpiCameraFPS: 15
      rpiCameraBitrate: 1500000
      all_others:
    11. Save the file and exit the Nano editor.
    12. Run MediaMTX by typing the following command.
      ./mediamtx
      • You should not see any errors.
      • Any Warnings may be ignored. Common Warning messages are the following.
        • WARN RPiSdn sdn.cpp:40 Using legacy SDN tuning – please consider moving SDN inside rpi.denoise
        • WAR [RTSP] [session 861b8faa] write queue is full
      • Note once you connect to the stream from VLC, Blue Iris, or other viewer, you should see a line similar to the following at the end of the output.
        2024/12/15 19:17:39 INF [RTSP] [session 861b8faa] is reading from path ‘cam’, with TCP, 1 track (H264)
        Terminal session output showing output from MediaMTX
    13. You may validate that the stream is working properly by opening VLC going to the “file “Media” menu and selecting “Open Network Stream…”
      NOTE: I’m running VLC on Windows
      Windows VLC Media Menu items
    14. In the “Please enter a network URL:” textbox, enter the following and press the “Play” button.
      rtsp://<Raspberry Pi Address>:8554/cam
      VLC Open Network Stream Dialog
    15. If everything is working, you should see the video from the camera.
      VLC showing video stream
    16. Once it is verified that everything is working properly, exit mediamtx by pressing the <Ctrl> and C keys on the keyboard.
    17. Now we want MediaMTX to start up when the Raspberry Pi reboots. To run MediaMTX on startup, a service file needs to be created. Run the following command to create and edit the service file.
      sudo nano /etc/systemd/system/mediamtx.service
    18. Add the following content to the file.
      [Unit]
      Description=MediaMTX service
      Wants=network.target
      
      [Service]
      ExecStart=/home/pi/mediamtx/mediamtx /home/pi/mediamtx/mediamtx.yml
      
      [Install]
      WantedBy=multi-user.target
    19. Save and close the file.
    20. To enable the service, the following two commands need to be run.
      sudo systemctl daemon-reload
      sudo systemctl enable mediamtx.service
    21. The following set of commands are useful for viewing the status, restarting, starting, and stopping the service.
      Yes, I know services are called daemons on Linux.
      • sudo systemctl status mediamtx
      • sudo systemctl restart mediamtx
      • sudo systemctl start mediamtx
      • sudo systemctl stop mediamtx

    Connecting to the camera from Blue Iris

    When adding a the camera to Blue Iris, the settings need to be entered properly. Below is a screenshot and the list of settings.

    Blue Iris Network IP camera configuration dialog
    • Select “rtsp://” from the protocol dropdown list.
    • Enter the following for the URL.
      <IP Address>:8554/cam
    • Clear the “User” and “Password” textboxes.
    • Set the “Media/video/RTSP port” to 8554.
    • Leave the “Discovery/ONVIF port” to the default setting.
    • Keep the other options to the default values

    After clicking the “OK” button, the video stream should be displayed in Blue Iris.

    Blue Iris interface showing the video feed from the camera

    References

    I want to call a few resources that helped me get this setup.

    Final Thoughts

    I was able to get things working but I have seen the Raspberry Pi not able to serve up the stream occasionally but it is starting to look like it may be usable. I’m going to make a case for this so that it can mount on a window where I have the ESP32-CAM cameras mounted. If they run well for a month or so, I may replace the three ESP32-CAM cameras that I have. I’m thinking about adding the ability to move the camera’s as well using some servo motors. I will have to determine what would be required to do that with Blue Iris and the Raspberry Pi GPIO pins.

    Categories
    Raspberry Pi Pico

    Raspberry Pi Pico 2

    Last week, the Raspberry Pi announced the release of the Pico 2 and the RP2350 chip. It is exciting to see that Raspberry Pi has released this upgraded chip and board. I can’t wait to give it a go but will probably wait a while before buying one. I still have quite a few 1st generation Pico’s and they have plenty of storage and computing power for my projects. I am interested in one big improvement that Jeff Geerling mentioned in his YouTube review of the Pico 2.

    Jeff mentioned that the Pico 2 uses less power in idle and deep sleep than the original Pico. I have yet to build a low power device, but it is a skill that I would like to master. Currently if I need a battery powered device to last longer, I throw a larger battery at it rather than optimizing the power draw.

    I have not seen anything mentioning the ADC functionality to see if there have been improvements. The original Pico has some peculiarities with the ADC when measuring voltages but as long as you are aware of the limitations, you can work around them. I would like to know if the Pico 2 was redesigned to improve ADC measurements.

    Most likely I will wait until the wireless boards are available and I have a project in mind to make use of the new features. I would like to know if you plan to get a Pico 2 anytime soon and what projects you plan to create with the Pico 2.