DIY Projects

Raspberry Pi Zero W and Node-RED MQTT Surveillance Camera for Home Assistant

Best deal at:
CHF 6.81
level advanced

Since version 1.3, the Raspberry Pi Zero has a camera connector on which the 8MP camera of the Raspberry Pi can be connected by means of an adapter. Home Assistant supports many cameras. In this project, we will be manufacturing – very quickly – a mini surveillance camera using Node-RED. Node-RED will be used to take a snapshot at regular intervals and send the image to Home Assistant by an MQTT message.

Why an MQTT Camera for Home Assistant?

Home Assistant supports many types of camera (full list). Since version 0.46, Netgear’s Arlo cameras are partially supported.

To make a DIY surveillance camera from a Raspberry Pi Zero W, we have several solutions at our disposal:

To write this article, I started (probably like everyone!) To install Motion on my Raspberry Pi Zero and built-in Home Assistant video stream using the Generic MJPEG IP Camera component. But it does not work. Currently, (unless I’m mistaken), Home Assistant does not display any images on the views. In the detail view only the image (video stream) is displayed.

So I turned to MQTT. The advantage of MQTT is that it is possible to create a smart connected surveillance camera. Using MQTT to communicate with Home Assistant, you can add other functions to the camera:

Why Node-RED. Simply because there will be absolutely no programming to do! Three nodes are enough to create a connected surveillance camera!

Equipment used

There are three versions of the Raspberry Pi Zero. If you take the train en route, here is a short summary

  1. The first version of the Raspberry Pi Zero is marketed at a price of $6 in 2015.
  2. A few months later, the foundation adds a camera connector. This is version 1.3.
  3. In 2017, the Raspberry Pi Zero W integrating WiFi and Bluetooth 4.0 is launched at the price of $13.

Which camera to choose for the Raspberry Pi Zero W?

Here are some camera models you can buy to make a surveillance camera from a Raspberry Pi Zero. The NoIR model is best suited as it allows, with additional lighting (2 infrared LED for example) to obtain a night surveillance camera up to 10 m approx.

Camera v2.1 official Camera v2.1 NoIR official

Asian clone with 2x Red LED

Connector CSI CSI CSI, compatible with Raspberry
Dimensions 25 × 24 × 9 mm 25 × 24 × 9 mm 25 x 24 x 26 mm

25 x 75 x 26mm (avec Leds)

Resolution 8MP

2592 × 1944 pixels


3280 × 2464 pixels


2592 × 1944 pixels

Sensor CMOS Sony IMX219 CMOS Sony IMX219 CMOS OV5647
Video resolution (max.) 1080p30 1080p30 1080p30
Sensor size 1/4 pouces 1/4 pouces 1/4 pouces
Driver V4L2  V4L2  V4L2
Focal 3.60 mm 3.60 mm 3.60 mm
Vision field 53°(horiz.) x 41°(vert.) 53°(horiz.) x 41°(vert.) 75,7° (horiz.)
Approx. price From $29 From $31 From €21

The camera connector that equips the Raspberry Pi Zero W is different from the Raspberry Pi 3 (or earlier). There is a version specific to Zero. In fact, it is the connection cable that changes. If you already have an Asian camera or clone, you can buy a compatible tablecloth for about $2.

Prepare the Raspberry Pi Zero W

If you have just purchased your Raspberry Pi Zero W, follow these steps to prepare your system:

Activate the camera

Start by activating the camera using raspi-config

sudo raspi-config

Go to option (5) Interfacing Option

Then activate the camera module (P1)

This done, run the following command to change the driver of the camera

sudo modprobe bcm2835-v4l2

The loading of the driver at the start is made permanent by executing

echo "bcm2835-v4l2" | Sudo tee -a /etc/modules

Now restart the system to activate the changes

sudo reboot

Install the Python Picamera library

For this project, we will use the flow node-red-contrib-camerapi which allows to take a snapshot using the Python Picamera library (presented in detail in this tutorial). Run this command to install it.

sudo apt-get install python-picamera python3-picamera

Install the node-red-contrib-camerapi module

Open the palette manager

Enter the camera in the search field and then start installing the module by pressing install.

Everything is ready to encode your surveillance camera.

Video capture with Node-RED

Now that everything works, we will use Node-RED to capture an image at regular intervals. MQTT is not designed to support a video stream. On the other hand, as messages will circulate on your local network and potentially be sent to your smartphone when you leave your home, it is necessary to limit the size of the image. It’s a shame when you have an 8MP camera, but we will have to limit the resolution. If you are using the mobile app for iOS, limit it to 320 x 240 pixels. For use on a LAN only, you can increase the resolution to 640 x 480 pixels.

It’s gone for programming. Drop an Inject node. Select the repeat type and specify the shooting interval. As for the resolution, it is useless to be too greedy. A shot every 5 seconds is enough to see what’s in a room.

In the search box, type camera to find the module’s take-up node

Place it on the flow and double click and edit the parameters like this:

As you can see, there is no compression. This is also a reason to limit the size of the image.

To test that everything works well, you can plug in a node debug and deploy flow. In the debug tab, you must get an object containing a number table (the buffer of the image). If you get an empty string, go to the end of the tutorial to identify and fix the problem.

Publish image by MQTT

MQTT is natively supported by Node-RED. Look for the MQTT flow in the palette and place an MQTT output node on the flow.

MQTT is natively supported by Node-RED. Look for the MQTT flow in the palette and place an MQTT output node on the flow.

Open the node to edit it.

Click the pencil to set up a new connection. On the first tab, enter the IP address (or Internet address if you are using an online Broker)

If you have set up a password authentication, go to the second tab and fill in the Username and Password fields

Note. Password security does not encrypt massages that pass through the local network. To do this, you need to enable the SSL encryption of the broker

Validate the connection by clicking Add. Back on the MQTT node control panel, select the connection from the list. Finally, indicate an MQTT Topic. Here, all images will be published on the Topic camera/pizerow . As the images are published regularly, you can leave the QoS on 0 (the messages are sent without warranty of reception) and retain on false (when a new client connects, it will not be able to recover the old messages – images). Click Done to finish.

The programming of your camera is complete. Deploy Flow

Flow Code

Paste this code and modify the parameters before deploying the flow

[{"id":"63d2ff76.6564e","type":"inject","z":"6f9a15f1.e2763c","name":"Every 5 secondes","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"x":160.5,"y":175,"wires":[["8d852b1a.b9bdf8"]]},{"id":"8d852b1a.b9bdf8","type":"camerapi-takephoto","z":"6f9a15f1.e2763c","filemode":"0","filename":"","filedefpath":"1","filepath":"","fileformat":"jpeg","resolution":"1","rotation":"0","fliph":"0","flipv":"0","brightness":"50","contrast":"0","sharpness":"0","imageeffect":"none","name":"","x":403.5,"y":215,"wires":[["f74b1572.e5bce8","50d0f5e5.e9b3ac"]]},{"id":"f74b1572.e5bce8","type":"mqtt out","z":"6f9a15f1.e2763c","name":"","topic":"camera/pizerow","qos":"0","retain":"false","broker":"4b7f2fe2.bb49d","x":635.5,"y":252,"wires":[]},{"id":"50d0f5e5.e9b3ac","type":"debug","z":"6f9a15f1.e2763c","name":"","active":false,"console":"false","complete":"false","x":631.5,"y":165,"wires":[]},{"id":"4b7f2fe2.bb49d","type":"mqtt-broker","z":"","broker":"","port":"","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Include the MQTT camera at Home Assistant

Open the Home Assistant configuration file

sudo nano /home/homeassistant/.homeasssistant/configuration.yaml

Start by adding a mqtt section and specify the connection parameters to your broker. If you have password enabled, fill in username and password, otherwise delete the settings.

   Broker: localhost
   Port: 1883
   Keepalive: 60
   Username: <user>
   Password: <password>
   Protocol: 3.1.1

Then add a new camera section. Specify the topic on which Home Assistant must retrieve the images. Here camera / pizerow

   Platform: mqtt
   Topic: camera / pizerow
   Name: pizerow

Add the camera to a group using. Here, this will give camera.pizerow. To change the displayed label, add the

sudo systemctl restart home-assistant@homeassistant

After rebooting, the camera is fully integrated into Home Assistant

In case of problems with the camerapi module

The camerapi module for Node-RED is based on the Picamera library, if it is not installed, you will have this message

0 | node-red | CameraPi (err): Error: Command failed: python /home/pi/.node-red/node_modules/node-red-contrib-camerapi/lib/python/ pic_722dcbe7-15d6-477c-bd22-29510bbadce1.jpg / Home / ft / Pictures / jpeg 320 240 0 0 0 50 0 0 none
0 | node-red | Traceback (most recent call last):
0 | node-red | File "/home/pi/.node-red/node_modules/node-red-contrib-camerapi/lib/python/", line 1, in <module>
0 | node-red | Import picamera
0 | node-red | ImportError: No module named picamera

The Pictures directory does not exist on Raspbian Jessie Lite by default. You may get this error message when you first deploy flow Node-RED.

0|node-red | CameraPi (log): Generate - /home/pi/Pictures/pic_ee62d58d-ad4d-40ff-b403-19e1faf10902.jpg
0|node-red | 26 Jun 10:07:38 - [info] [camerapi-takephoto:7c67fe4a.f9f59] python /home/pi/.node-red/node_modules/node-red-contrib-camerapi/lib/python/ pic_ee62d58d-ad4d-40ff-b403-19e1faf10902.jpg /home/pi/Pictures/ jpeg 320 240 0 0 0 50 0 0 none
0|node-red | CameraPi (err): Error: Command failed: python /home/pi/.node-red/node_modules/node-red-contrib-camerapi/lib/python/ pic_ee62d58d-ad4d-40ff-b403-19e1faf10902.jpg /home/pi/Pictures/ jpeg 320 240 0 0 0 50 0 0 none
0|node-red | Traceback (most recent call last):
0|node-red |   File "/home/pi/.node-red/node_modules/node-red-contrib-camerapi/lib/python/", line 1, in <module>
0|node-red |     import picamera
0|node-red | ImportError: No module named picamera

Open a Terminal and run this command to create the missing folder

mkdir /home/pi/Pictures

Save the configuration (XTRL + X then Y – O) and restart Home Assistant


And now, thanks to Node-RED and MQTT, we have just manufactured a surveillance camera connected 100% DIY. You have a total control of the images that are sent to your home automation server or on your smartphone when you leave your home. In the next tutorial, we will add a motion detector that will send (or simply) a notification, an email or trigger a scenario on the home automation server.

Click to rate this post!
[Total: 0 Average: 0]
Exit mobile version