Home
Features
Python Interface for GPIO Circuits
This package provides two related capabilities. First, it provides a high-level, event-driven Python interface for GPIO circuits running on the Raspberry Pi. Sensors, motors, LEDs, switches, and many other components are covered.
Remote Control of GPIO Circuits via REST/HTML/JavaScript
Second, this package enables remote control of GPIO circuits via REST APIs invoked from HTML/JavaScript front-ends. Want to control your circuit remotely from your phone? Look no further. This package auto-generates HTML/JavaScript for GPIO circuits based on Material Design for Bootstrap. These HTML/JavaScript elements can be embedded in full web pages for remote control of the circuit. The remote control screen for the Freenove Smart Car is shown below:
As another example, consider the 3D-printed robotic arm that I designed for the car:
Additional CAD parts that I have designed are listed here.
Use and Development
The raspberry-py
package can be consumed in two ways:
- Add
raspberry-py
to a project as a PyPI dependency. This is the best approach if you only want to use the functionality provided byraspberry-py
. Theraspberry-py
package is available here, and an example of adding the package dependency is provided here. - Fork the present repository and then install it locally. This
is the best approach if you want to enhance and/or fix the functionality provided by
raspberry-py
. In the following,XXXX
is the user account into which the repository is forked:git clone git@github.com:XXXX/raspberry-py.git cd raspberry-py virtualenv -p python3.9 venv . venv/bin/activate pip install -U pip pip install -e .
From here, you can push back to your fork and submit a pull request to the original if desired.
Operating System Configuration
Ubuntu
The raspberry-py
package should be compatible with the standard
Raspberry Pi OS; however, I have been using the Ubuntu installation described
here (ignore the “Install RLAI” section).
GPIO
By default, Ubuntu does not give the user permission to interact with the GPIO pins of the Raspberry Pi. To grant GPIO permissions when the Raspberry Pi boots:
- Edit
/etc/udev/rules.d/99-gpiomem.rules
as follows to assign allgpio*
device to thedialout
group, which the user is a member of by default:KERNEL=="gpio*", OWNER="root", GROUP="dialout"
- Reboot for the new permissions to take effect.
Use of I2C with the Raspberry Pi (e.g., page 111 of the tutorial) requires
configuration with the raspi-config
utility, which is installed by default in the Raspberry Pi OS but not in Ubuntu.
Install raspi-config
for Ubuntu with the following commands:
sudo apt install lua5.1
wget http://archive.raspberrypi.org/debian/pool/main/r/raspi-config/raspi-config_20211019_all.deb
sudo dpkg -i raspi-config_20211019_all.deb
A full listing of the latest raspi-config
packages can be found
here. The user will also need to be added to the
i2c
group with sudo usermod -a -G i2c ubuntu
(then restart for the change to take effect).
Enabling and testing the Raspberry Pi video camera:
- Modify boot config:
sudo emacs /boot/firmware/config.txt
and addstart_x=1
andgpu_mem=256
at the end. - Enable camera:
sudo apt install raspi-config
, thenraspi-config
, then enable the camera. - Give permission:
sudo usermod -a -G video ubuntu
- Restart:
sudo shutdown -r now
- Test:
raspistill -o test.jpg
mjpg_streamer
The mjpg_streamer
utility is an efficient way to stream video from various input devices (e.g., Raspberry Pi camera
module or USB webcam) to various output devices (e.g., web browser). Install as follows:
sudo apt install subversion libjpeg-turbo8-dev imagemagick ffmpeg libv4l-dev cmake libgphoto2-dev libopencv-dev libsdl-dev libprotobuf-c-dev v4l-utils
git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer-experimental/
export LD_LIBRARY_PATH=.
make
List connected video devices:
v4l2-ctl --list-devices
mmal service 16.1 (platform:bcm2835-v4l2):
/dev/video2
WEB CAM: WEB CAM (usb-0000:01:00.0-1.1):
/dev/video0
/dev/video1
/dev/media0
The first (connected to /dev/video2
) is
a Raspberry Pi camera module. The second (connected to
/dev/video0
) is a USB webcam.
To stream the Raspberry Pi camera module:
./mjpg_streamer -i "input_uvc.so -d /dev/video2 -fps 15" -o "output_http.so -p 8081 -w ./www"
Alternatively, to stream the USB webcam:
./mjpg_streamer -i "input_uvc.so -d /dev/video0 -fps 15" -o "./output_http.so -p 8081 -w ./www"
In both of the above:
-d /dev/video*
selects the video input device.-f 15
runs the camera at 15 frames per second.-p 8081
serves the MJPG stream on port 8081 of all IP interfaces.-w ./www
points the streamer at thewww
directory.
Once the streamer is up and running, navigate to http://xxxx:8081/
for an overview or
http://xxxx:8081/?action=stream
for a dedicated stream (where xxxx
is the IP address of the Raspberry Pi).
It can happen that a process is holding a /dev/video*
device, which prevents your camera from connecting. List video
devices:
sudo fuser /dev/video*
/dev/video0: 1418m
Dig a bit more:
ps aux | grep 1418
root 1418 0.0 0.8 304784 65344 ? Sl 20:36 0:02 ...
Then kill the process if you wish:
sudo kill 1418
References: