Boilerplate for complex hardware prototypes (RPi 5, 2 cameras, sensors, webapp)

shnydercom

Jonathan Schneider

Posted on August 20, 2024

Boilerplate for complex hardware prototypes (RPi 5, 2 cameras, sensors, webapp)

You can't touch your raspberry pi 5, but you can touch your phone.
You can't hold 2 cameras, but you need control.
You know some part will break, but not sure which part.
Oh and maybe it'll float away and you'll need to catch it.

-- The ocean

If Use Cases similar to these are appearing in your project, I might have some code to get you started. Feel free to modify to your needs: Code on github (sbc-closed-system-controller)

I wrote this code because I had to throw a Raspberry Pi 5 in the waves while it was monitoring a prototype with 2 cameras and some sensors. Here's a video of the result:

Tech stack choice

  • A Python web backend (FastAPI) was chosen because the sensors I've used have good python support. The camera libraries for the raspberry pi 5 are also still in development and not yet as mature as the ones prior to the Pi5. However, only the Pi5 has two connectors for cameras over CSI/DSI, and synchronized recording from two cameras was important. Installing the libraries from the latest git branch can be done with this command: pip install git+https://github.com/raspberrypi/picamera2.git@next
  • A React WebApp that would poll individual sensors over REST was chosen to avoid having to refresh for updates. That is a drawback you see with some html templating engines. The Raspberry Pi would go underwater, and risked loosing the WiFi connection provided by the phone's hotspot. Having the frontend downloaded and only periodically updating its data allowed for each called endpoint to fail without the UI breaking down. It wasn't sure if the sensor and camera connection would survive when the prototype went underwater. This turned out to be easier for testing as well. Lastly, I'm used to React from my regular work as a frontend developer. The built frontend code can be served from FastAPI by using a symbolic link: ln -s ../../client-web-app/dist .
  • A service description to run the server at start of the Raspberry Pi. This actually took some searching before I found the right section, so here's the link

How the system behaves

  • The first behaviour when using 2 cameras on a Pi5 with a powerbank is... shutdowns. Most of them happen because of low voltage. The reason can be found in the system log: journalctl -S -10m --system.
  • failures are expected, so recordings are chopped into 5 second bits. The sensor readings go into a .csv-file, the raw .h264-files and their timestamps are saved separately. If everything had failed and got soaked in salt water, then the last barrier of water-proofing would have been the SD-card, which in theory should be waterproof. Luckily it didn't come to that. The combination of the files to bigger files is done with the python script convert_recordings.py, in the root of the repository.
  • The recording can be started and stopped from the web interface. The cameras stream a lowres-image to the web interface, both while recording and when idle. The latest sensor readings are summarized on the web interface as well.
  • There is a servo/PWM-controller board as well. This is used to control a pan-/tilt-head for one of the cameras, which uses 2 servos. The remaining channels are used for controlling the strength of LEDs to light the inner part.
  • It's possible to use a smart watch to display webapps on your local network. So far I only know how to do that with Samsung devices: Use Samsung internet to add the web address of the Pi to your favorites, while it's inside your hotspot's network. Samsung internet can be opened on the smartwatch as well, with limited functionality. For example the video display will have significant delay and the corners of the App are very hard to reach, so important buttons should be centered.

and that's about it

Conclusion

I hope this code helps someone get started with their own project. Hopefully you don't have to build something under water :)

Putting electronics in the water though is something I do not recommend, because waterproofing the components ended up taking the majority of the time I needed for this project. Extra care needs to be taken that the glues aren't water-solulable and other parts aren't harmful to the environment either.

In the end, the prototype showed that electricity can be generated in the surf on the shoreline. Recording the inside and outside of a wavebreaker produced some footage that makes this so much easier to explain.

So I hope you'll enjoy watching the above video. And let me know if this code has been helpful, I'd like to hear about similar use cases! :)

💖 💪 🙅 🚩
shnydercom
Jonathan Schneider

Posted on August 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related