Package: picamera-mqtt
picamera-mqttMQTT-based control and image capture with local and remote networked Raspberry Pi cameras.
Deployment scripts needs to be run from a Raspberry Pi. Other scripts can be run from any computer.MQTT Broker Server SetupTo run the MQTT broker server, editdeploy/config/broker.confand then rundeploy/mqtt_broker.sh.Broker AutostartTo automatically run the MQTT broker when the host Raspberry Pi starts up,
install themqtt_broker.servicesystemd unit:cd ~/Desktop/picamera-mqtt
sudo cp deploy/systemd/mqtt_broker.service /etc/systemd/system/mqtt_broker.service
sudo systemctl enable mqtt_brokerYou can manually start the service with systemd, view the status of the service with systemd,
view its output logs with journalctl, or kill the script with systemd:sudo systemctl start mqtt_broker
systemctl status mqtt_broker
journalctl -u mqtt_broker
sudo systemctl stop mqtt_brokerCamera Client Deployment SetupThese instructions are for setting up a Raspberry Pi to deploy a camera imaging client.PreparationYou will need to install some packages on the Raspberry Pi, as follows:sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git python3-pip
sudo apt-get install vim byobu wicd-curses # optional, but makes your life easierYou will need to use theraspi-configtool to change the password of the pi user,
set the locale toen_US UTF-8, set the keyboard layout toEnglish (US),
change the hostname of the Raspberry Pi, and set the Raspberry Pi to wait for an
internet connection upon startup.You will need to edit the/etc/wpa_supplicant/wpa_supplicant.confconfiguration
file to connect to wi-fi if you are deploying the Raspberry Pi with a Wi-Fi connection.Client Hardware SetupConnect a Raspberry Pi Camera to the Raspberry Pi.Client Software SetupClone this repo, for example with:cd ~/Desktop
git clone https://github.com/ethanjli/picamera-mqtt.gitInstall required dependencies fromrequirements_deployment.txt, for example with:cd ~/Desktop/picamera-mqtt
sudo pip3 install -r requirements_deployment.txtThen confirm that the camera works correctly by running thecapture.pyscript,
as follows:cd ~/Desktop/picamera-mqtt
python3 -m picamera_mqtt.tests.imaging.captureThis should cause the appearance of two files,capture_pil.jpgandcapture_final.jpg, in your working directory.Client ConfigYou will need to edit thedeploy/config/settings.jsonconfig file such that:Thehostnameparameter is eitherlocalhost(if your MQTT broker is running
on the same computer as your client) or the IP address of the computer running
your MQTT broker.Theclient_nameparameter and thetarget_nameparameter both should saycamera_n, wherenshould be replaced with a unique id number of your camera
client. This id number will be used to uniquely label each camera stream.Camera Client Software AutostartTo automatically run the camera MQTT client when the Raspberry Pi starts up,
install themqtt_imaging.servicesystemd unit:cd ~/Desktop/picamera-mqtt
sudo cp deploy/systemd/mqtt_imaging.service /etc/systemd/system/mqtt_imaging.service
sudo systemctl enable mqtt_imagingYou can manually start the service with systemd, view the status of the service with systemd,
view its output logs with journalctl, or kill the script with systemd:sudo systemctl start mqtt_imaging
systemctl status mqtt_imaging
journalctl -u mqtt_imaging
sudo systemctl stop mqtt_imagingAd Hoc Wi-Fi Network SetupTo acquire images from multiple Raspberry Pi cameras, each camera should be
connected to a Raspberry Pi board. A Raspberry Pi 3 B+ should be running themqtt_brokerservice, and it can also run themqtt_imagingservice for the
camera attached to it. The other Raspberry Pi boards (e.g. Raspberry Pi Zero W)
should be running themqtt_imagingservice for their respective cameras. Then
the Raspberry Pi 3 needs to be configured to host a simple ad hoc wi-fi network.Enabling the Ad Hoc NetworkNOTE: This currently doesn't work correctly. Instead of setting up an ad hoc
network, the Raspberry Pi boards all need to be connected to some wireless
access point.The details of enabling and disabling the ad hoc network are managed by thesimondlevy/RPiAdHocWiFi repo:cd ~/Desktop
git clone https://github.com/simondlevy/RPiAdHocWiFi.gitTo enable the ad hoc network, run the following and reboot the Raspberry Pi:cd ~/Desktop/RPiAdHocWiFi
sudo ./install.shThis will create an ad hoc network with SSIDRaspberry-Pi-1, and the Raspberry Pi
will be accessible at IP address192.168.2.2.Connecting to the Ad Hoc NetworkTo configure the other Raspberry Pi boards, copydeploy/adhoc/rc.localto/etc/rc.local:cd ~/Desktop/picamera-mqtt
sudo cp deploy/adhoc/rc.local /etc/rc.localand copydeploy/adhoc/wpa_supplicant.confto/etc/wpa_supplicant/wpa_supplicant.conf:cd ~/Desktop/picamera-mqtt
sudo cp deploy/adhoc/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.confSystem TestsWith an operational camera client connected to the MQTT broker server, you can run
a few tests on the computer running the MQTT broker server to confirm correct image
transfer:cd ~/Desktop/picamera-mqtt
python3 -m picamera_mqtt.tests.mqtt_clients.mock_host --interval 8 --config settings_localhost.jsonThis will exercise roundtrip communication to and from the camera client with
client namecamera_1by sending image acquisition messages to the camera client
every 8 seconds and receiving (and discarding) images captured by the camera client.To save these images, run the following test:cd ~/Desktop/picamera-mqtt
python3 -m picamera_mqtt.tools.timelapse_host --interval 15 --number 5This will take a timelapse of 5 images spaced out at 15-second intervals from
all camera clients connected to the broker with client namescamera_1,camera_2,
andcamera_3; the client names queried can be changed by editing the config file.
By default, images will be saved to thedatadirectory, but you can change this
with the--output_dirflag to specify a different path.If you only want to capture image snapshots at a single time point, run the following test:cd ~/Desktop/picamera-mqtt
python3 -m picamera_mqtt.tools.acquire_hostBy default, images will be saved to thedatadirectory, but you can change this
with the--output_dirflag to specify a different path. By default, images will
be saved with filenames withacquireat the start, but you can change this with
the--output_prefixflag to specify a different filename prefix.System AdministrationYou can remotely send deployment management commands to the Raspberry Pi client
by sending messages over thedeploymenttopic. Thepicamera_mqtt/tools/deploy/mqtt_send_deploymentscript lets you do this
from the command-line, as follows:cd ~/hand-hygiene/intervention
python3 -m intervention_system.tools.mqtt_send_deployment shutdown --target_name camera_1 # shut down the Raspberry Pi running camera 1
python3 -m intervention_system.tools.mqtt_send_deployment reboot --target_name camera_1 # reboot the Raspberry Pi running camera 1
python3 -m intervention_system.tools.mqtt_send_deployment stop --target_name camera_1 # stop the illumination client running camera 1
python3 -m intervention_system.tools.mqtt_send_deployment restart --target_name camera_1 # restart the illumination client running camera 1
python3 -m intervention_system.tools.mqtt_send_deployment "git pull" --target_name camera_1 # update the repo and restart the illumination client running camera 1Camera Parameter AdjustmentYou can remotely send camera parameter update commands to the Raspberry Pi client
by sending messages over thecontroltopic. Thepicamera_mqtt/tools/deploy/mqtt_send_camera_paramsscript lets you do this
from the command-line, as follows:cd ~/hand-hygiene/intervention
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 # query camera parameters from camera 1
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 --roi_zoom 1.5 # set camera zoom factor to 1.5 on camera 1
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 --shutter_speed 200 # set shutter speed to 200 ms on camera 1
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 --iso 200 # set ISO to 200 on camera 1
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 --resolution_width 1920 --resolution_height 1080 # set the image resolution to 1920x1080 on camera 1
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 --awb_gain_red 2.0 # set AWB red gain to 2.0 on camera 1
python3 -m intervention_system.tools.mqtt_send_camera_params --target_name camera_1 --awb_gain_blue 2.0 # set AWB blue gain to 2.0 on camera 1Note that all camera param flags can be combined into a single command if you wish.