A Robot With A Camera ? That You Control via The Internet ?
This is actually something that was completed a short time ago but I’ve only just got around to writing it up.
The idea was to use a Pan / Tilt HAT and a camera to make a home CCTV system, make it accessible / controllable from the Internet and contain it in something unusual looking; something that I wouldn’t mind having on a shelf.
I forgot to capture the initial unassembled pieces so here are:
- 1 x Pi3A
- 1 x Pan / Tilt HAT
- 1 x Camera module
Already partially assembled
Not shown are:
- 1 x NeoPixel Stick
- A diffuser (which came with the NeoPixel Stick)
Added some extra long nylon bolts so that the Pi can be mounted underneath the HAT.
The side view, now fully assembled.
It’s quite a squeeze but it all fits.
The fully assembled top view with the clearly visible NeoPixel Stick.
As the whole thing is to be encased and impossible to adjust or repair, I made sure that the connectors wouldn’t budge.
Did I mention hot glue? Here again making sure that the nylon bolts on the camera module stay in place.
The whole unit in its final resting place, standing tall!
Bit of a close up of the Pan / Tilt unit and camera.
A big thank-you to ScrewedSculpts for his work on the robot, I just did the nerdy bits.
Making it Move and Operating Remotely
There are quite a few guides on how to do this, and I found some of the a bit confusing.
After a few bungled attempts, I distilled them into a few simple commands.
The obvious starting place is to install the Pimoroni libraries:
curl https://get.pimoroni.com/pantilthat | bash
And check that it’s working:
cd /home/foo/Pimoroni/pantilthat/examples
python smooth.py
The RPi-Cam-Web-Interface is also needed:
NOTE - Enabling the Camera
Bullseye OS has replaced the camera stack which stopped the raspimjpeg working.
Legacy camera support should be enabled.
Do this within raspi-config under Interface.
If this shows just “Enable camera” then update raspi-config itself from its menu item.
The interface option should now show enable Legacy camera support.
The Pi will reboot.
The install.sh script will detect a Bullseye OS, set the right PHP version and create a missing directory needed by this software to run.
Clone the code from github and enable and run the install script with the following:
git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
cd RPi_Cam_Web_Interface
./install.sh
Configure accordingly with the cam folder at ‘/html/wherever-you-like’.
OK & Wait for ‘Start’.
Locally, at ‘http://Your-Pi-IP-Address/html/wherever-you-like’ there should be an image.
Also on the Pi, in a browser, at URL ‘http://127.0.0.1/html/wherever-you-like’ there should be an image.
Configure the Pan / Tilt HAT for RPi-Cam-Web-Interface
Firstly, rename pipan_off
to enable the browser direction arrows (needs a browser refresh):
cd /var/www/html/wherever-you-like
sudo mv pipan_off pipan_on
Add a FIFO pipe:
cd /var/www/html/wherever-you-like
sudo mknod FIFO_pipan p
sudo chmod 666 FIFO_pipan
Create a pipan-pipe.py on the Pi:
nano pipan_pipe.py
The one I used came from this post:
https://forums.pimoroni.com/t/rpi3-camery-pimioroni-adafruit-pan-tilt-rpi-cam-web-interfac/3263/13
The important line is this one:
pipein = open("/var/www/html/wherever-you-like/FIFO_pipan", 'r')
Save it somewhere sensible (mine went in my Home folder).
Check that it’s working:
cd /home/foo
python pipan_pipe.py
Edit /etc/rc.local
and add the following line above the exit-command:
(Change the path to the directory where you saved the pipan-file).
sudo nano /etc/rc.local
python /home/foo/pipan_pipe.py &
If you see this, then put the above line ^^ above it.
#START RASPIMJPEG SECTION
It may need a reboot but the Pan / Tilt HAT should now be operable via a browser.
Connecting to the Outside World
As the ‘RPi-Cam-Web-Interface’ comes with a web server, it can serve requests from the internet.
On my router port 80 traffic goes to a web server where it determines where to send that request internally.
An easier route would be to direct port 80* traffic to the Pi where all of the above magic takes place
If it is port 80 that was chosen to serve requests when installing ‘RPi-Cam-Web-Interface’.
NeoPixel Stick Menu
I added a bash menu to control the lights.
At the moment the options link to the Pimoroni examples, all apart from the ‘Sleep’ which turns the pixels off and returns the head back to its starting position.
The menu:
#!/bin/bash
HEIGHT=15
WIDTH=40
CHOICE_HEIGHT=4
BACKTITLE="robocam Neopixel Lights"
TITLE="Neopixel Stick Examples"
MENU="Choose one of the following options:"
OPTIONS=(1 "360 White"
2 "360 Rainbow"
3 "Smooth Sweep"
4 "Staggered Sweep"
5 "Sleep")
CHOICE=$(dialog --clear \
--backtitle "$BACKTITLE" \
--title "$TITLE" \
--menu "$MENU" \
$HEIGHT $WIDTH $CHOICE_HEIGHT \
"${OPTIONS[@]}" \
2>&1 >/dev/tty)
clear
case $CHOICE in
1)
python ~/Pimoroni/pantilthat/examples/grbw.py
;;
2)
~/Pimoroni/pantilthat/examples/neopixels.py
;;
3)
~/Pimoroni/pantilthat/examples/smooth.py
;;
4)
~/Pimoroni/pantilthat/examples/timeout.py
;;
5)
~/Pimoroni/pantilthat/examples/sleep.py
;;
esac
The ‘Sleep’ bit:
#!/usr/bin/env python
import pantilthat
pantilthat.light_mode(pantilthat.WS2812)
pantilthat.light_type(pantilthat.GRBW)
r, g, b, w = 0, 0, 0, 0
while True:
for x in range(18):
pantilthat.set_pixel(x, r, g, b, w)
pantilthat.show()
p = 0.00
t = 0.00
pantilthat.pan(p)
pantilthat.tilt(t)
I hope that the above helps anyone looking to do something similar.
If anyone finds an error please let me know and I’ll amend it.
Finally:
These are both excellent reading and flesh out the above a bit more:
https://elinux.org/RPi-Cam-Web-Interface#Pan-Tilt_or_Pi-Light