Raspberry Pi Construction Cam

In the beginning of 2021 we brought a really old house. This house already deflates heavily – there is no other choice than demolition. Since I think it’d be nice to see the progress as time lapse movie, I wanted to install a camera. The common term for this is construction cam. I’m aware about commercial vendors like Brinno selling those cameras, but (at least for the demoltion) i didn’t want to spend additional money. Therefor, I used parts laying around to build a simple construction camera with a Raspberry Pi 3 A+ and an USB webcam.

Acceptance criteria

There are several things I’d like to achieve / expect from my camera:

  • Take a picture using an USB attached every 30 seconds right after startup
  • Enumerate those pictures without time (there won’t be reliable times)
  • Create a protected WiFi network
  • Serve a list of all taken pictures

The first two criteria are clear – just take pictures that can later be joined to video using its ascending name. I need access to Pi via Phone/Tablet when installed outdoor so I can check if position/angle match my expectations without additional software.

Basic setup

The basic setup is simple. This tutorial expects you to connect via WiFi and run commands via SSH, it differs when using GUI (which consumes additional, probably unnecessary resources.). The steps of the basic setup are:

  • Flash Raspberry Pi OS (e. g. using Raspberry Pi Imager)
  • Create /boot/ssh file to make your Pi SSH accessible
  • Create /boot/wpa_supplicant.conf with your region and credentials:
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
  ssid="WiFi SSID"
  scan_ssid=1
  psk="WiFi Passphrase"
  key_mgmt=WPA-PSK
}
  • Unmount device and boot your Pi
  • Optional (but recommended!): Setup a new password for the user pi
  • Update your Raspberry Pi to the latest version
sudo apt update
sudo apt upgrade -y
sudo reboot

Take Pictures

I know there are some Python scripts or even tools like motion. I didn’t want to learn how to take pictures with Python. Motion did not recognize my camera. So I just use fswebcam and a simple bash scripts to take pictures:

  • Install fswebcam
sudo apt install fswebcam -y
  • Create directory /home/pi/Pictures
  • Create take_picture.sh (or another name, if you prefer) with the following content:
#!/bin/sh
current_index=$(find /home/pi/Pictures -maxdepth 1 -type f | rev | cut -d/ -f1 | rev | sort -n | tail -n 1 | cut -d. -f1)
next_index=$(($current_index + 1))

#take picture
fswebcam --set brightness=75%  --no-banner -r 1920x1080 "/home/pi/Pictures/$next_index.jpg"

#move old picture to target directory (create if necessary)
target_dir=$((current_index/2880))
mkdir -p "/home/pi/Pictures/$target_dir"
mv "/home/pi/Pictures/$current_index.jpg" "/home/pi/Pictures/$target_dir"

This script will count all pictures in /home/pi/Pictures/ and return the amount plus one. The second command will take a picture and save it with the next number. Using this methods all pictures get named with ascending numbers without being dependent on any date/time provider. UPDATE: A friend remarked that using ls gets slower over time. I also noticed this when using the nginx listing. The script now reads the highest filename number, creates the new picture with the next number and moves the old image to a result directory. each directory will contain a maximum of 2880 pictures (one day). This should be sufficient for some days.

  • Make script executable
chmod +x take_picture.sh
  • Add the script to crontab:
* * * * * /home/pi/take_picture.sh
* * * * * ( sleep 30s ; /home/pi/take_picture.sh )

Note: Crontab can only be configured to run every minute. Sleeping 30 seconds while started in the same minute is a workaround for this.

Serve pictures

Now it’s time to serve the pictures. I’ll use nginx and let it create a listing of the taken pictures. I’m lazy and don’t want to setup, manage and configure startup of nginx, so I’ll just use Docker and Compose. Feel free to achieve this by using nginx directly 😉.

  • Install Docker
curl -sSL https://get.docker.com | sh
  • Install Compose
sudo apt install docker-compose -y
  • Create compose file /home/pi/docker-compose.yml
version: '3'
services:
  nginx:
    image: nginx
    restart: always
    ports:
      - '8080:80'
    volumes:
      - '/home/pi/Pictures:/usr/share/nginx/html'
      - '/home/pi/nginx.conf:/etc/nginx/nginx.conf'
  • Create nginx configuration /home/pi/nginx.conf
worker_processes auto;

events {
  worker_connections 1024;
}

http {
  server_tokens off;
  server {
    listen 80;
    root /usr/share/nginx/html;
    location / {
      autoindex on;
    }
  }
}
  • Start the nginx container (might take some time to pull)
docker-compose up -d

Navigate to http://raspberrypi:8080 – you should see the content of /home/pi/Pictures. Thanks to the cronjob there‘ should already be several pictures.

  • Restart the Pi and check if it still takes a picture every 30 seconds. At least on the A+ you won’t be able to install additional software (at least I couldn’t make the Pi be both client and access point).

Run WiFi network

The last step is the creation of a WiFi network. With this I’ll be able to check the pictures when the camera is installed in the target location. Luckily there are several setup scripts that turn your Pi into an access point. I’ll be using RaspAp.

  • Become root (important! The script neither checks for nor switches to root which does a lot of crazy things, I had to reinstall the whole OS 😅)
sudo su
  • Run setup script
wget -q https://git.io/voEUQ -O /tmp/raspap && bash /tmp/raspap

Say yes to everything until it asks you to install VPN or ad blocking – you won’t need them, so decline the installation.

  • Reboot your Pi
  • Search for WiFi
    • SSID: raspi-webgui
    • Pass: ChangeMe
  • Connect to the WiFi
  • Navigate to the Dashboard
    • User: admin
    • Password: secret
  • Change your WiFi Password (and name, if you like)
  • Change the admin password
  • Reboot the Pi again

Verify that your Pi creates pictures after startup. It should be working although you don’t have internet access anymore. You can shutdown your Pi and install it in the expected location.

Final thoughts

I just have a cheap webcam, the quality of the pictures is not the best. For an invest of 0€ (or 35€ if you buy the Pi, the cam and a power supply) this is fine to me. I’ll probably be there every day to take some high resolution pictures. It is just for the feeling and memories 😉

PS: Joining images to movie clip is possible using ffmpeg, but model A+ does not have enough RAM for this task. You have to use another machine for this.

Sources

Setup WiFi before boot: RaspberryTips

Setup and use fswebcam: Raspberrypi-Guide

Workaround to run cron every 30 seconds: Stackoverflow

Setup Docker: Raspberry Pi Website

Directory Listing with nginx: Fuzzyblog

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.