Raspberry Pi Camera for Sourdough Monitoring



All right, the last step to complete the great Sourdough Monitoring Project (SMP): capturing actual footage. We don't need a real video but just a sequence of still images. The goal is to collect enough of those on disk to later be able to assemble them into a time-laps video of the dough growing. First I'll show you how to enable camera support in software, then how to attach the camera, and finally finally how to capture images to disk. As a bonus, I'll share a PHP script you can use to receive images on a server on the internet so you can look at your dough from the other side of the world. <span id="more-5450"></span> # Enabling the Camera Support in Software Execute `raspi-config` and select "Interface Options", then "Camera", and finally answer the question if you want to enable the camera interface with "Yes". You can now use the `vcgencmd` program to confirm this worked: ```bash $ vcgencmd get_camera supported=1 detected=0 ``` Next we want to turn that nasty `0` after `detected` into a pleasant `1`. # Attaching the Camera First, identify the camera port on your Raspberry Pi. Some googling around for data sheets should do the trick. For both my Model 1 B 2.0 (CPU Revision `000e`) and my Model 2 B 1.1, it was perpendicular to the edge of the HDMI port directly next to it: <img decoding="async" src="/wp-content/uploads/2020/09/1-find-camera-port.jpg" class="aligncenter size-full" style="position:relative;border:4px solid #ddd;width:300px;height:400px;z-index:20;display:block" /> <hr style="position: relative; top:-220px; border:2px solid #ddd;z-index:0;margin-bottom:-10px"> Now don't be like me and try to jam in the cable as hard as you can but instead notice that you can pull up those white nobs there. After pulling them up, the cable glides in like a swan. Now, unlike me, make sure to _really_ put the cable in, all the way! And finally, press down those white nobs again. <img fetchpriority="high" decoding="async" src="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/step-1-pull-up.jpg" alt="Image showing where to pull on the board" width="266" height="200" class="size-full wp-image-5558" style="float:left;border:4px solid #ddd; margin:4px 2px 0 2px;" /> <img decoding="async" src="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/step-2-insert-cable.jpg" alt="Image showing cable orientation" width="267" height="200" class="size-full wp-image-5559" style="float:left;border:4px solid #ddd; margin:4px 2px 0 2px;" /> <img decoding="async" src="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/step-3-slide-cable-in.jpg" alt="Image showing how to slide cable in" width="266" height="200" class="size-full wp-image-5560" style="float:left;border:4px solid #ddd; margin:4px 2px 0 2px;" /> <img loading="lazy" decoding="async" src="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/step-4-push-down.jpg" alt="Image showing where to push to attach the cable" width="267" height="200" class="size-full wp-image-5561" style="float:left;border:4px solid #ddd; margin:4px 2px 0 2px;" /> <br style="clear:left"/> Also, make sure to put the cable in with the silver metal connectors facing the direction of the circuit path visible on the board. The photos above show the correct way for a Model 1 B 2.0. Boot up your Pi and hope for the following output: ```bash $ vcgencmd get_camera supported=1 detected=1 ``` # Capture Images To capture an image you can simply use the command `raspistill`. It supports all kinds of options like rotation, scaling and more fancy filters. But for now, we will just use the `-o` switch to set the file name: ```bash raspistill -o image.png ``` Since I need do to that regularly, say, every minute and upload the resulting image to a server together with the <a href="https://blag.nullteilerfrei.de/2020/09/27/temperature-measurement-on-raspberry-pi/">temperature measured from a sensor </a>, I created a short shell script that will use `curl` to do all the heavy network-related lifting: ```bash #!/bin/bash set -e cd /home/pi IMAGE="image.png" TEMPERATURE=$(python3 temperature.py) raspistill -o "$IMAGE" -q 100 -n -w 532 -h 400 -rot 0 curl -F "upload=@$IMAGE" "https://example.com/upload.php?temperature=$TEMPERATURE" ``` # Receive Images on a Server on the Internet The following is the simple PHP script `upload.php` that can be used with the above `curl` command to write the captured image to a directory called `data` and log the measured temperature together with the timestamp to a file called `temperature.log`. It will also generate a static HTML page that you can then browse to. ```php <?php function error_out() { header('HTTP/1.1 500 Internal Server Error'); exit(); } if ( !isset($_FILES['upload']) || !isset($_FILES['upload']['error']) || $_FILES['upload']['error'] !== 0) error_out(); $image_file = 'data/img-' . ( (new Datetime())->format('YmdHis')) . '.jpg'; if ( !@move_uploaded_file($_FILES['upload']['tmp_name'], $image_file)) error_out(); $timestamp = (new Datetime())->format('Y-m-d H:i:s'); $temperature = isset($_GET['temperature']) ? $_GET['temperature'] + 0 : null; if ($temperature) { $fp = fopen('temperatures.log', 'a'); if ( !$fp) error_out(); fwrite($fp, $timestamp . ': ' . $temperature . "\n"); fclose($fp); $temperature_out = number_format($temperature / 1000., 1) . '°C'; } else { $temperature_out = '<i>unknown</i>'; } $content = <<<EOF <html> <head> <title>Sauercam</title> <style type="text/css"> body { background: black; color: white; } </style> </head> <body> <h1>Sauercam</h1> <h3>{{ now }}</h3> <p>Temperature: {{ temperature }}</p> <img decoding="async" src="{{ image_file }}" /> </body> </html> EOF; $content = str_replace('{{ image_file }}', $image_file, $content); $content = str_replace('{{ temperature }}', $temperature_out, $content); $content = str_replace('{{ now }}', $timestamp, $content); $fp = fopen('index.html', 'w'); if ( !$fp) { error_out(); } fputs($fp, $content); fclose($fp); ``` If you just place this on your web server every person on the internet can upload their files to your box, so you want to put it behind HTTP basic auth or something similar. Finally some glamour shots of this pile wires: <img loading="lazy" decoding="async" src="/wp-content/uploads/2020/09/raspberry-pi-camera-1.jpg" alt="" width="600" height="450" class="aligncenter size-large wp-image-5603" style="position:relative;border:4px solid #ddd;width:600px;height:450px;z-index:20;display:block" srcset="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-1.jpg 600w, https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-1-300x225.jpg 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /> <img loading="lazy" decoding="async" src="/wp-content/uploads/2020/09/raspberry-pi-camera-2.jpg" alt="" width="600" height="450" class="aligncenter size-large wp-image-5604" style="position:relative;border:4px solid #ddd;width:600px;height:450px;z-index:20;display:block" srcset="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-2.jpg 600w, https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-2-300x225.jpg 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /> <img loading="lazy" decoding="async" src="/wp-content/uploads/2020/09/raspberry-pi-camera-3.jpg" alt="" width="600" height="450" class="aligncenter size-full wp-image-5601" style="position:relative;border:4px solid #ddd;width:600px;height:450px;z-index:20;display:block" srcset="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-3.jpg 600w, https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-3-300x225.jpg 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /> <img loading="lazy" decoding="async" src="/wp-content/uploads/2020/09/raspberry-pi-camera-4.jpg" alt="" width="450" height="600" class="aligncenter size-large wp-image-5602" style="position:relative;border:4px solid #ddd;width:450px;height:600px;z-index:20;display:block" srcset="https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-4.jpg 450w, https://blag.nullteilerfrei.de/wp-content/uploads/2020/09/raspberry-pi-camera-4-225x300.jpg 225w" sizes="auto, (max-width: 450px) 100vw, 450px" />

Leave a Reply

Your email address will not be published. Required fields are marked *