Skip to main content

MQTT-based control and image capture with local and remote networked Raspberry Pi cameras.

Project description

picamera-mqtt

MQTT-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 Setup

To run the MQTT broker server, edit deploy/config/broker.conf and then run deploy/mqtt_broker.sh.

Broker Autostart

To automatically run the MQTT broker when the host Raspberry Pi starts up, install the mqtt_broker.service systemd unit:

cd ~/Desktop/picamera-mqtt
sudo cp deploy/systemd/mqtt_broker.service /etc/systemd/system/mqtt_broker.service
sudo systemctl enable mqtt_broker

You 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_broker

Camera Client Deployment Setup

These instructions are for setting up a Raspberry Pi to deploy a camera imaging client.

Preparation

You 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 # optional, but makes your life easier

You will need to use the raspi-config tool to change the password of the pi user, set the locale to en_US UTF-8, set the keyboard layout to English (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.conf configuration file to connect to wi-fi if you are deploying the Raspberry Pi with a Wi-Fi connection.

Client Hardware Setup

Connect a Raspberry Pi Camera to the Raspberry Pi.

Client Software Setup

Clone this repo, for example with:

cd ~/Desktop
git clone https://github.com/ethanjli/picamera-mqtt.git

Install required dependencies from requirements_deployment.txt, for example with:

cd ~/Desktop/picamera-mqtt
sudo pip3 install -r requirements_deployment.txt

Then confirm that the camera works correctly by running the capture.py script, as follows:

cd ~/Desktop/picamera-mqtt
python3 -m picamera_mqtt.tests.imaging.capture

This should cause the appearance of two files, capture_pil.jpg and capture_final.jpg, in your working directory.

Client Config

You will need to edit the deploy/config/settings.json config file such that:

  • The hostname parameter is either localhost (if your MQTT broker is running on the same computer as your client) or the IP address of the computer running your MQTT broker.
  • The client_name parameter and the target_name parameter both should say camera_n, where n should 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 Autostart

To automatically run the camera MQTT client when the Raspberry Pi starts up, install the mqtt_imaging.service systemd unit:

cd ~/Desktop/picamera-mqtt
sudo cp deploy/systemd/mqtt_imaging.service /etc/systemd/system/mqtt_imaging.service
sudo systemctl enable mqtt_imaging

You 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_imaging

System Tests

With 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.json

This will exercise roundtrip communication to and from the camera client with client name camera_1 by 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 5

This will take a timelapse of 5 images spaced out at 15-second intervals from all camera clients connected to the broker with client names camera_1, camera_2, and camera_3; the client names queried can be changed by editing the config file. By default, images will be saved to the data directory, but you can change this with the --output_dir flag 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_host

By default, images will be saved to the data directory, but you can change this with the --output_dir flag to specify a different path. By default, images will be saved with filenames with acquire at the start, but you can change this with the --output_prefix flag to specify a different filename prefix.

System Administration

You can remotely send deployment management commands to the Raspberry Pi client by sending messages over the deployment topic. The picamera_mqtt/tools/deploy/mqtt_send_deployment script 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 1

Camera Parameter Adjustment

You can remotely send camera parameter update commands to the Raspberry Pi client by sending messages over the control topic. The picamera_mqtt/tools/deploy/mqtt_send_camera_params script 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 1

Note that all camera param flags can be combined into a single command if you wish.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

picamera-mqtt-0.0.4.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

picamera_mqtt-0.0.4-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file picamera-mqtt-0.0.4.tar.gz.

File metadata

  • Download URL: picamera-mqtt-0.0.4.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7

File hashes

Hashes for picamera-mqtt-0.0.4.tar.gz
Algorithm Hash digest
SHA256 89cb3a7b44a9007cf64d7cbfa90ef0d7099c35f66a48fcba7f734afc3549a45f
MD5 a314602c5ac4d92c3650fcae834b84d5
BLAKE2b-256 4a3996e8d2474e7682d78621f002f1365a6c1d799ef1737aac51cc50cbbd1977

See more details on using hashes here.

File details

Details for the file picamera_mqtt-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: picamera_mqtt-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7

File hashes

Hashes for picamera_mqtt-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 64ae57d25d1473875777b8e13ff91b28fef374ce356e1f6134378ceabbffa0a9
MD5 501bc9866eee32d9e6ab177079f69124
BLAKE2b-256 8fd17636db92f69c3971774ec9d3816f9cdccca083e6c856d962734e81fe1f83

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page