IOT | Video Streaming

DIY Baby Monitor with Raspberry Pi

Streaming video with Raspberry Pi

Atharva Inamdar

--

“Don’t forget to give time for yourself” is a common piece of advice given by experienced parents. So when it was my time, I got straight to work on my next home project.

I had a Raspberry Pi 1B and 2B+ and a Raspberry Pi camera v1. My RPi 2B+ was already running HomeAssistant and being slightly more powerful could perform some light processing. I decided to use this RPi to stream the video itself. I knew that video streaming is challenging and can be compute-intensive so the first step was to give the onboard GPU about 50% of the memory available. The second step was to see if there are any OSS solutions available.

I discovered two pieces of software: motion and motioneye. Motion is an open-source application that can stream video and perform motion detection. There’s no audio support by design as the purpose is to detect motion from video stream. MotionEye is another opensource software that presents a management and user web interface for Motion. So that’s where I began.

Once both Motion and MotionEye were installed, it was time to check whether it was working. The key to getting both to work smoothly was to make sure MotionEye was running as a service and Motion was running as a daemon. This ensured that Motion could access the RPi camera at all times. MotionEye manages its own copy of Motion config which is used when configuring the motion detection and camera config.

MotionEye interface once configured

Now that a basic stream was available, the next was to look at upgrading the camera. Now the above screenshot was after the camera upgrade. The upgrade was to buy an IR-CUT camera with IR LEDs. This is a camera with an infrared filter controlled with a pin and LEDs to illuminate the cot bed and baby at night. Swapping the Raspberry Pi V1 camera with the new one was easy and didn’t require any other modifications. Sometimes the image can appear with pink tint, this can be solved with setting awb_auto_is_greyworld=1 and disable_camera_led=1 in /boot/config.txt and rebooting. disable_camera_led=1 is important to set when working with IR cameras. It usually controls the IR filter.

Next step is to integrate this with Home Assistant. There are two ways I integrated this; 1) embed MotionEye as a tab. 2) Trigger notifications and track motion as a virtual device.

To add a new tab I used the iframe integration by adding the following into configuration.xml :

panel_iframe:
motioneye:
title: Motion Eye
url: http://raspberrypi:8765/
icon: mdi:cctv
MotionEye embedded into Home Assistant

For the notification, there are two parts to configure. First, we need a way to track a new device whose state is controlled by MotionEye. For this we need two devices to configure: input_boolean and binary_sensor . input_boolean is used to set the status. binary_sensor is used as a sensor device driven by input_boolean . Here’s the config:

binary_sensor:
- platform: template
sensors:
baby_motion:
friendly_name: Baby Motion
value_template: >
{{states('input_boolean.baby_motion')}}
device_class: motion
input_boolean:
baby_motion:
name: baby motion trigger
Devices in Home Assistant

What this means is if input_boolean is toggled, the sensor will show motion or clear as per the motion class.

Putting this together with an Automation, we need a way for MotionEye to trigger it. The simplest way for external triggers is using a Webhook. So with one webhook, it is possible to trigger motion and with another to clear it.

Example Action for automation

To trigger this webhook from MotionEye, I created two simple scripts:

# motion_trigger.sh
#!/usr/bin/env bash
curl -X POST http://raspberrypi:8123/api/webhook/motioneye
# motion_trigger_end.sh
#!/usr/bin/env bash
curl -X POST http://raspberrypi:8123/api/webhook/motioneyeoff

These two scripts are triggered by MotionEye and configured as below:

MotionEye notification trigger scripts

As such Now when MotionEye detects motion, it triggers Home Assistant which notifies me. While there is no audio this is sufficient to alert us wherever I am.

The final build looks like this:

A phone selfie stand and make-shift cardboard holder for camera

--

--