Raspberry Pi WebCAM MQTT Client with discovery support for HASS integration

Based on the new libcamera Python-API picamera2 for Raspberry Pi cameras I wrote a MQTT client with Home Assistant discovery support .

picam2ctrl features

  • 10 different picam2ctrl entities
  • taking snapshot images
  • capturing MP4 videos incl. audio
  • simple HTTP MJPEG streaming server
  • UDP video streaming
  • motion/occupancy detection by camera
  • timestamp support
  • PAN/TILT camera support with step motor (28BYJ-48) or Waveshares PAN-Tilt Hat
  • scp taken pictures or videos

For more details see README on project page at github.

Home Assistant demo screenshot:

2 Likes

Very nice. I created the (almost ) exact same thing a couple of years ago, using picamera. However, Iā€™ve never found the time to update it to the new camera library, so this is very helpful. Thanks a lot :slight_smile:

Iā€™ve looked at bit more at it and really like it; much more elaborate and neat than my old code. I was wondering if I could make a feature request? Support for a tilt servo. I have a RB-Moto2 controller board that can control two stepper servos. Would be lovely to be have both pan and tilt :slight_smile:

Yes, tilt function is already planned. On the one hand it is easy to integrate a 2nd stepmotor in software e.g. for above mentioned RB-Moto2 controller board. But on the other hand I actually donā€™t have a good idea how construct / integrate the motor that Iā€™m able to tilt the camera since I donā€™t have a 3D printer. To pan only the case like this is simple, but how to add the tilt motor here ?

But I found this Pan-Tilt HAT , I think Iā€™ll integrate this solution. It provides a light sensor, too.

Cool, looking forward to that :slight_smile:

Yeah, not having access to a 3D printer can somewhat limit your designs. Drilling holes in boxes, hefty use of gluegun and the like :smiley:

At my old job I had access to a really nice 3D printer and managed to get a couple of these printed out. I do also have some of those cheap pan-tilt thingies (just without the board), so would be interested in support for that too.

Very interesting project - thank you for sharing. Would it work with the ā€œoldā€ picam as well ? Currently, I only have access to an old picamera and I am eager to try your client and to check, if it would fit for me.

I dontā€™ know any restriction to older Pi Camera hardware but Raspberry Pi OS ā€œBullseyeā€ or newer is required and legacy CAM support must be disabled in raspi-config.

OK, thank you for the quick reply. Will try it ā€¦

It should work with all Raspberry Pi Camera modules.

Link

As part of the new Open Camera System, Raspberry Pi has updated the V4L2 drivers for its v1 (OmniVision OV5647) and v2 (Sony IMX219) camera modules, developed a new HQ camera (Sony IMX477), and is working on upstreaming the Broadcom Unicam CSI-2 and ISP peripheral drivers to support libcamera.

I am trying to get it up and running. My broker runs locally on my NAS and all my sensors etc (some 20+ devices) connect to this broker. Since the only access to the outside world is by VPN, the broker has no user and pass.

When I start pi2camcrtrl.py I get the following messages:

cfgfile = ./config.json
[118:24:01.658510009] [26963]  INFO Camera camera_manager.cpp:299 libcamera v0.0.4+22-923f5d70
[118:24:01.804178138] [26966]  INFO RPI raspberrypi.cpp:1476 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media2 and ISP device /dev/media0
17:07:23 - INFO - configured camera model:ov5647 ,detected: 1
17:07:23 - INFO - Starting up MQTT Service picam2ctrl
17:07:23 - ERROR - [Errno 104] Connection reset by peer
Could not establish MQTT Connection! Try again                     3 xtimes
17:07:26 - ERROR - SSL/TLS has already been configured.
Could not establish MQTT Connection! Try again                     2 xtimes
17:07:29 - ERROR - SSL/TLS has already been configured.
Could not establish MQTT Connection! Try again                     1 xtimes
17:07:29 - ERROR - Could not connect to MQTT Broker 192.168.yyy.xx exit

The MQTT part of my config.json looks like this

    "MQTTBroker":{ 
	"host":"192.168.yyy.xx",
	"port": 1883,
	"username":"",
	"password":"",
	"insecure":true,
	"connection_retries":3,
	"clientkeyfile":"",
	"clientcertfile":""
  },

Any idea what the problem could be ?

Thank you in advance for your help.

1 Like

Mmh, I have no idea. I can connect with username + password and without username + password as well: I removed username & PW and set strings in config.json to ā€œā€ - no problems. My Mosquitto MQTT Broker is running as docker container on other 2nd Raspberry. I only I didnā€™t verify clientkey/cert based connections.

What you can try:

  • double check port settings config.json with mosquito.conf
  • login at your Raspberry and try to ā€˜pingā€™ brokerā€™s ip address

Hi zenzay42,

full pan tilt support is now available since version 0.2.1:

  • you can configure now ULN2003 hardware with 2 step motors
  • alternatively WAVESHAREā€™s pan-tilt hat is supported.

pan-tilt-screen

1 Like

Muy apreciado!

Thank you very much for implementing this. Now, I have to try it out and hopefully will be able to have it on all my Pi Cams :slight_smile:

I need to find some time away from the gardenā€¦ so much to do :house_with_garden:

i use the same configuration as joshmosh
and i get the same message,
ping is ok and config is ok
other mqtt enabled services can connectā€¦ Is authorization required? Can it not use anonymous/guest communication?

Mmh, strange, for me it works fine even without user / pw. Which error is shown in the brokerā€™s log ( mosquitto) ? What kind of broker host , Raspberry Pi, localhost ?

Super exciting! I have been playing around with Pi-TiMoLo (based on the old Picamera library) this week. I wish I had found your project. I opened an Issue on your repo about adding Topics to your repo, thatā€™s how I found Pi-TiMoLo originally.

Iā€™ll be testing tonight and will let you know how I go. Thanks for sharing your project

@irimitenkan , I experienced the same issue as @carlssonn and @joshmosh and opened an issue in GitHub to track coming up with a fix.

Cannot Connect to Default Configured HA Mosquitto Ā· Issue #4 Ā· irimitenkan/picam2ctrl (github.com)

Fix was pretty straight-forwardā€¦

Fix Connection Issues with HA Mosquitto by askpatrickw Ā· Pull Request #5 Ā· irimitenkan/picam2ctrl (github.com)

Hi @irimitenkan !
Does picam2ctrl also create camera and image entities automatically?

No, because you have to setup e.g. a specific IP address or file storepath which depends on your installation. But it is quite easy to create this manually , please see README at the end,chapter ā€œHome Assistant Configurationā€