PiCamera setupLink
Warning
This is a work in progress!
Getting startedLink
To set up the PiCamera you first need to install a fresh OS on your Raspberry Pi Zero W.
Burn a new Raspberry Pi OS Lite imageLink
We recommend the Raspberry Pi Imager tool to burn a new Raspberry Pi OS (32-bit) Lite. You can find it here.
Or you can download an official ZIP here.
Accessing the PiZeroLink
A - Enable SSH remote accessLink
After installing the Raspberry Pi OS, add an empty file ssh
in the boot
partition to enable SSH access.
Warning
As we will later use the Wi-Fi from the PiZero to create an Access Point, we recommend to use the ethernet port to SSH in the PiZero.
B - Use local accessLink
Otherwise, use a screen and keyboard to access the PiZero.
Raspberry Pi OS setupLink
Before installing the software, you have to configure some Raspberry Pi OS settings & change the password.
Change the passwordLink
For security reasons, you have to change the pi
user password because the default is known by everyone!
Use the raspi-config
tool to do it:
sudo raspi-config
Warning
The default password is raspberry
!
Edit the timezone & localesLink
Change the timezone to get the right time from the PiZero clock! You may also change the locales to suits your needs.
Create the Wi-Fi Access PointLink
We will now create the AP using command lines following the tutorial from: https://learn.sparkfun.com/tutorials/setting-up-a-raspberry-pi-3-as-an-access-point/all.
You may also be interested in doing this with a graphical interface, and we would highly recommend raspap-webgui.
Install PackagesLink
To install the required packages, enter the following into the console:
sudo apt-get -y install hostapd dnsmasq
Set Static IP AddressLink
Edit the dhcpcd.conf
file:
sudo nano /etc/dhcpcd.conf
At the bottom of the file, add:
denyinterfaces wlan0
Save and exit by pressing Ctrl+X and Y when asked.
Next, we need to tell the Raspberry Pi to set a static IP address for the Wi-Fi interface. Open the interfaces file with the following command:
sudo nano /etc/network/interfaces
At the bottom of that file, add the following:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
Configure HostapdLink
We need to set up hostapd to tell it to broadcast a particular SSID and allow Wi-Fi connections on a certain channel. Edit the hostapd.conf file (this will create a new file, as one likely does not exist yet) with this command:
sudo nano /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=romi_hotspot
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=raspberry
rsn_pairwise=CCMP
Save and exit by pressing Ctrl+X and Y when asked.
Unfortunately, hostapd
does not know where to find this configuration file, so we need to provide its location to the hostapd startup script.
Open /etc/default/hostapd
:
sudo nano /etc/default/hostapd
Find the line #DAEMON_CONF="" and replace it with:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Save and exit by pressing Ctrl+X and Y when asked.
Configure DnsmasqLink
Dnsmasq will help us automatically assign IP addresses as new devices connect to our network as well as work as a translation between network names and IP addresses.
The .conf
file that comes with Dnsmasq has a lot of good information in it, so it might be worthwhile to save it (as a backup) rather than delete it.
After saving it, open a new one for editing:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
sudo nano /etc/dnsmasq.conf
In the blank file, paste in the text below.
interface=wlan0
listen-address=192.168.0.1
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.0.100,192.168.0.200,24h
Test Wi-Fi connectionLink
Restart the Raspberry Pi using the following command:
sudo reboot
After your Pi restarts (no need to log in), you should see romi_hotspot
appear as a potential wireless network from your computer.
Connect to it (the network password is raspberry, unless you changed it in the hostapd.conf
file).
Then try to SSH it with:
ssh pi@192.138.0.1
Install Python3 & pipLink
You will need the Python3 interpreter (Python>=3.6) to run the PiCamera server & pip
to install the PiCamera package.
sudo apt update && sudo apt upgrade -y
sudo apt install python3 python3-pip
This will update the package manager, upgrade the system libraries & install Python3 & pip.
Install the PiCamera packageLink
Once you have pip
you can install the picamera
Python3 package:
pip3 install picamera
Note
We do not create an isolated environment in this case since the sole purpose of the PiZero will be to act as a responsive image server.
Camera serve Python codeLink
To capture and serve the images from the PiCamera, we use this Python script:
To upload it to your PiZero, from a terminal:
wget https://gist.github.com/jlegrand62/c24e454922f0cf203d6f9ed49f95ecc1/raw/c4cda40ff56984188dca928693852f3f7a317fa4/picamera_server.py
Now (test) start the server with:
python3 picamera_server.py
Todo
Explain how to execute python3 picamera_server.py
command at PiZero boot.