Skip to main content
Rpi-Cam-MQTT
------------

Mqtt agent for RPi-Cam-Web-Interface (https://elinux.org/RPi-Cam-Web-Interface) project.

The agent runs on a raspberry pi where RPi-Cam-Web-Interface is installed and running.

The main objective is to provide an mqtt interface to the FIFO file (/var/www/FIFO11) to inject commands and get the status from status_mjpeg.txt.

Additionally, the support for pantilt is also available (check below).

The agent has been developed with the specific purpose to simplify the integration with homeassistant; this is made possible through the dedicated HA camera platform "rpicam" (https://gitlab.com/gbus/ha-rpicam), currently under development.
Anyway, the generic nature of the mqtt messages makes it possible to use any mqtt controller to read and send info with the agent.

When connected to an MQTT server it is possible to:
- get the camera status
- send remote mqtt commands to control the camera
- send remote mqtt commands to control pantilt (if available)
- list pan/tilt views (if pantilt available and views configured)

The topics are:
- _`rpicam/<camera_name>/status`_, status from /dev/shm/mjpeg/status_mjpeg.txt
- _`rpicam/<camera_name>`_, topic used to publish commands
- _`rpicam/<camera_name>/pt`_, topic used to publish pan/tilt commands
- _`rpicam/<camera_name>/pt/views`_, list of configured views
- _`rpicam/<camera_name>/events/motion`_, motion events

Installation
------------

- From pypi

Ensure that the command pip3 is installed and run:

sudo pip3 install rpicammqtt


- Development code from gitlab

Install the requirements, clone the repo and run the setup:

sudo apt-get install git python3-setuptools python3-yaml
git clone git@gitlab.com:gbus/rpi-cam-mqtt.git

cd rpi-cam-mqtt
sudo python3 setup.py install

Configuration
-------------

The configuration is looked up in:

/etc/rpi-cam-mqtt/rpi-cam-mqtt.yaml
$HOME/.rpi-cam-mqtt/rpi-cam-mqtt.yaml

Create the preferred directory:

sudo mkdir /etc/rpi-cam-mqtt
or
mkdir $HOME/.rpi-cam-mqtt

cd into it and add the files:

sudo wget https://gitlab.com/gbus/rpi-cam-mqtt/raw/master/rpicammqtt/config/rpi-cam-mqtt.yaml
sudo wget https://gitlab.com/gbus/rpi-cam-mqtt/raw/master/rpicammqtt/config/Adafruit_PCA9685.yaml

In rpi-cam-mqtt.yaml change at least the following:

- Camera name
- MQTT server address
- MQTT user/pw

Configure the service to start at boot:

sudo wget -o /etc/systemd/system/rpi-cam-mqtt.service https://gitlab.com/gbus/rpi-cam-mqtt/raw/master/rpicammqtt/systemd/rpi-cam-mqtt.service
sudo systemctl daemon-reload
sudo systemctl enable rpi-cam-mqtt
sudo systemctl start rpi-cam-mqtt

Motion events
-------------

When RPi-Cam-Web-Interface is set in monitoring mode, a macro is executed every time motion starts or stops.
`rpicam` is able to publish these events as MQTT message on a dedicated topic:

rpicam/<camera_name>/events/motion

The macro relies on the netcat command:

sudo apt-get install netcat

Add to the macro path the following script:

cat <webpath>/macros/motion_event.sh
(usually webpath = /var/www/html)

#!/bin/bash
printf $1 | /bin/netcat 127.0.0.1 8888

ensure the script `motion_event.sh` is executable.


Pan/Tilt
--------

Pan/Tilt can be enabled and it is based on the PCA9685 controller and the Adafruit library.
It is possible to predefine pan/tilt pair values with memorable names so that the camera can be instructed to point to a specific location (ie: point to "front_window").
The configuration looks like this:

pantilt:
views:
front_window:
- 560
- 355
door:
- 420
- 355

The pan/tilt values can be easily identified running the command "configpt", available from your path after the installation of the module.

This allows you to move the arrow keys while watching the live feed from the camera and get the pan/tilt values:

# configpt



Pan: 540 Tilt: 470


When the views are noted down with the correct pan/tilt values press q to exit the command.


Testing
-------

- Test camera status and PIPE commands

An easy way to test the agent is to check the topics for your camera with an mqtt client, ie with mosquitto:

mosquitto_sub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME/status
mosquitto_sub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME/pt/views
or all of it:

mosquitto_sub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -F 'Topic: %t\nMsg: %p\n' -t rpicam/CAMERA_NAME/#


It is possible to send any commands you would normally send to the FIFO file as described in the documentation (https://elinux.org/RPi-Cam-Web-Interface#Pipe), ie:

Disable the camera:
mosquitto_pub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME -m "ru 0"

Enable motion detection:
mosquitto_pub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME -m "md 1"

Set a new annotation and add a background to the text:
mosquitto_pub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME -m "an Entrance hall %Y.%M.%D_%h:%m:%s"
mosquitto_pub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME -m "ab 1"

When the agent receives the string commands, they are parsed before being sent to the pipe. The filtering is based on the type schema and the limits defined in data/rpi-cam-info.json

- Test pan/tilt views and commands

The camera can be pointed to different corners by setting values for pan and tilt like this:

mosquitto_pub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME/pt -m "{\"pan\": 380, \"tilt\": 300}"

Alternatively, one of views can be retreived:

mosquitto_sub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME/pt/views

...and used:

mosquitto_sub -h MQTTSRV_IP -p 1883 -u USERNAME -P PASSWORD -q 1 -t rpicam/CAMERA_NAME/pt -m "{\"view\": \"front_window\"}"

Debug
-----

After the agent is started by systemctl check for process errors:

systemctl status rpi-cam-mqtt

If the process is correctly run, check the logs:

tail -f /var/log/rpi-cam-mqtt.log

Increase the logging level in the configuration if needed:

logging:
level: ERROR|INFO|DEBUG


Metadata

Release files for rpicammqtt 0.10.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rpicammqtt 0.10.1
File Size Uploaded
rpicammqtt-0.10.1.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rpicammqtt 0.10.1
File Interpreter ABI Platform
rpicammqtt-0.10.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.1 kB

Release files / rpicammqtt-0.10.1.tar.gz

Download URL rpicammqtt-0.10.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b0fb18599a9a4550fd24f857d12fb4f09f398a0de0277af58bd1746bc18b8c6e
BLAKE2b-256 checksum
How to use checksums
df67ede5c97bfe571e70fcb78c9df8723c002c3c5a2574dd630300db176ffa53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / rpicammqtt-0.10.1-py3-none-any.whl

Download URL rpicammqtt-0.10.1-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4df64a6e07b150c1c30433f5dc2d26f03f494c3df76b8ab2d43f402a5a65eebb
BLAKE2b-256 checksum
How to use checksums
b3e1dcb3bc5907359b62172a4c6a6f7942eff02fe7cf993e9b974640d1c0fbe5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files

0.9.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page