gmoraleda / BabyPi

🐣 Baby monitor using a Raspberry Pi, the NoIR camera module πŸŽ₯ and a DHT22 temperature sensor 🌑

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

About

Baby monitor using a Raspberry Pi and the NoIR camera module. This is intended as a summary of the steps that I followed to get a Raspberry Pi 3 working as a baby monitor. There are many guides out there, but for some reason or another, none of them was up to date and I could not get them working.

Shopping list

Installation

Basic setup

This whole project has been done with the Pi being managed via SSH. It is really convenient. There are four main steps to get the Pi alive from scratch:

  1. Install Raspbian in the micro SD card.
  2. Create a wpa_supplicant.conf file to connect the Pi to our WiFi network.
  3. Enable SSH to access the Pi remotely via terminal or with an SFTP client. On macOS I'm using Cyberduck.

Camera and DHT22

With the basic installation up and running we can shut down the Pi and start connecting the camera module. Once the camera module is installed and tested (this is pretty straightforward), the next step is to connect the temperature sensor to the GPIO board.

The version I bought is already prepared to be connected to the GPIO directly, but in case you have the version with 4 pins you need to install a resistor. For the 3-pin version, the wiring is done as follow:

(+)   -> Pin 1
(out) -> Pin 7
(-)   -> Pin 6

From there on I followed the instructions here.

The python script (we need to install python and the Adafruit_DHT library) used to read the temperature and humidity from the sensor is really simple:

import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
    print("{1:0.1f}C {0:0.1f}%".format(humidity, temperature), end='')
else:
    print("Failed to retrieve data from humidity sensor")

*Notice that even the sensor is connected to the physical PIN 7, that PIN corresponds to GPIO4, therefore DHT_PIN = 4

Store this in a file called humidity.py and, if everything went as supposed, we should be able to query the sensor running the following command:

python3 ~/humidity.py

Schedule the reading

With that in place we want to schedule a job using Cron to read the temperature every 5 minutes and store it in a log file. I created a second shell script to perform the readings and copy them to a log file and to a text file (user_annotate.txt) that will be shown in the camera live stream:


temperature_script.sh
#! /bin/bash

log="/home/pi/monitor/logs/"

# Run the client
python3 /home/pi/monitor/temperature.py > temperature.txt
cp temperature.txt /dev/shm/mjpeg/user_annotate.txt

# Output data to a log file
TEMPERATURE=`echo "$OUTPUT"`    
echo "$(date +"%Y-%m-%d %T" ): ""$TEMPERATURE" >>"$log"temperature.log

We need to open the cron table (sudo crontab -e), and add the following line at the end:

*/5 * * * * /home/pi/monitor/temperature_script.sh

Make sure you write the right path there.

RPi Cam Web Interface

This piece of software allows us to connect to the camera using a browser. After following the installations instructions, we can access our camera typing the IP of our Raspberry Pi on any device connected to the same network. I'm using the standard configuration, using an Apache server.

One the camera is running and visible there is only one missing step: adding the sensor information to the camera. There is an annotation prepared to display the content of /dev/shm/mjpeg/user_annotate.txt (therefore we copy it as part of the Cron script).

Under Camera Settings -> Annotation add %a to show the user annotation.

It is also convenient to start RPi Cam on every boot of the Raspberry Pi.

Final thoughts

With everything connected and working this is how it looks like: Browser Interface

Final Setup

The sensor needs to stay away from the Raspberry Pi case in order to provide accurate readings. The Raspberry Pi produces heat. Right now is hanging on the side. The Lego case opens many possibilities, so I could build a side box to hold the sensor.

The NoIR camera is able to record at night using IR lamps. The next step of this project could be installing a couple of IR led that automatically turn on at night using a light sensor or scheduling it after sunset (using some weather API).

There is no audio streaming. Although I bought a USB microphone, I haven't decided yet on how to attach the audio streaming to the camera feed.

About

🐣 Baby monitor using a Raspberry Pi, the NoIR camera module πŸŽ₯ and a DHT22 temperature sensor 🌑