Home » Software » Raspberry Pi 3B+ Headless OV5647 Camera Setup

Raspberry Pi 3B+ Headless OV5647 Camera Setup

Raspberry OS struggles with the OV5647 Camera stream on the RPi 3. This headless solution will solve the problem although it won’t do much more.

Create a brand new 32bit Raspberry Pi Lite OS with SSH access and/or Raspberry connect. Boot it up then let the fun begin:

Enabling headless ov5674 camera

Modify the config.txt file with these lines:

camera_auto_detect=0
dtoverlay=ov5647

sudo nano /boot/firmware/config.txt
# For more options and information see
# http://rptl.io/configtxt
# Some settings may impact device functionality. See link above for details

# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# Additional overlays and parameters are documented
# /boot/firmware/overlays/README

# Automatically load overlays for detected cameras
camera_auto_detect=0

# Automatically load overlays for detected DSI displays
display_auto_detect=1

# Automatically load initramfs files, if found
auto_initramfs=1
dtoverlay=ov5647

Save then exit.

sudo apt update
sudo apt install -y rpicam-apps

Create the camera service:

sudo nano /etc/systemd/system/camera.service

Add the following lines:

[Unit]
Description=Direct HDMI Camera Feed
After=multi-user.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/rpicam-hello -t 0 --preview kms --fullscreen
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target

Save, exit then enable the service:

sudo systemctl daemon-reload
sudo systemctl enable camera.service

Reboot:

sudo reboot

Add extra wifi network to the pi

sudo nmcli connection add type wifi con-name "wifi_name" ssid 'wifi_password'

sudo nmcli connection modify "wifi_name" wifi-sec.key-mgmt wpa-psk wifi-sec.psk 'wifi_password'

nmcli connection show

Some extras to make the sd card more resilient:

sudo raspi-config

Navigate and Enable the Overlay

  1. Use the arrow keys to scroll down to Performance Options (on some older OS versions, this is located under Advanced Options) and press Enter.
  2. Select Overlay File System and press Enter. This will take some time.
  3. The menu will ask: “Would you like the overlay file system to be enabled?” Select Yes.
  4. The system will process for a moment, then confirm: “The overlay file system is enabled.” Select Ok.
Write-Protect the Boot Partition
  1. Directly after enabling the overlay, the tool will ask a second question: “Would you like the boot partition to be write-protected?”
  2. Select Yes.
  3. Select Ok to confirm.

Reboot to Finalize

  1. Use the right arrow key to highlight Finish on the main menu and hit Enter.
  2. Select Yes when prompted to reboot the system.

How to Disable It in the Future (If we need to make changes)

Because the SD card is completely read-only now, we cannot alter scripts or adjust camera configurations. If we ever need to change your camera.service file later on, we must temporarily turn off the write protection:

  1. Run sudo raspi-config.
  2. Go back to Performance Options → Overlay File System.
  3. Select No to turn off the overlay.
  4. Select No to remove write protection from the boot partition.
  5. Reboot the system. We are now in read-write mode. Make the changes, and then re-run the steps above to lock it down again.

Make Home Assistant check if the camera service is running

Add to configuration.yaml:

command_line:
  - binary_sensor:
      name: "PiName"
      device_class: connectivity
      command: ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -o ConnectTimeout=3 picam@192.168.0.xx "sudo systemctl is-active --quiet camera.service && echo ON" 2>/dev/null || echo OFF
      payload_on: "ON"
      payload_off: "OFF"
      scan_interval: 30
      command_timeout: 5

ssh into the raspberry:

since we run the service as root we need to change some privileges:

sudo visudo
picam ALL=(ALL) NOPASSWD: /usr/bin/systemctl is-active --quiet camera.service

As an alternative if we decide to run the full desktop with usb camera and cheese here are two options to check the pi:

command_line:
## This sensor is on a heavy size a bit. Not recommended but works too.
  - binary_sensor:
      name: "PiCam1"
      device_class: connectivity
      command: 'ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -o ConnectTimeout=3 username@192.168.0.xx "echo ON" || echo OFF'
      payload_on: "ON"
      payload_off: "OFF"
      scan_interval: 30
##This sensor only checks if the pi is on. Simple light check. Recommended.
  - binary_sensor:
      name: "PiCam2"
      device_class: connectivity
      command: 'nc -z -w 3 192.168.0.xx 22 && echo ON || echo OFF'
      payload_on: "ON"
      payload_off: "OFF"
      scan_interval: 30
      command_timeout: 4