Raspberry Pi Baby Monitor

Raspberry Pi Baby Monitor

 
 

CSS 299  Hardware Projects for the Raspberry Pi

Course Objectives

  • In this course, students will:

    • Configure and secure Raspberry Pi for remote operation

    • Work and be familiar with the Unix/Linux operating systems to perform system configurations, Python

      program development, and execution

    • Use, be familiar with, and build basic electronic circuits

    • Use Raspberry Pi and the Python API to interface digital/analog inputs (sensors), control motors, and other hardware (actuators), and use a variety of displays (output devices).

    • Communicate with hardware devices over the Internet

    • Build moderately sophisticated cyber-physical systems using the Raspberry Pi.

Final Project

ABSTRACT

This project’s main objective is to allow parents to leave a room while their child or children are sleeping, or playing unattended. The parent will be able to view the room through the web camera server set up on the Raspberry Pi. The parent will also be notified via email when the child is crying using the sound sensor connected to the Pi. Many baby monitors that are on the market consist of 2 devices: the microphone and the speaker. The microphone will allow the sounds in the child’s room to be transmitted to the speaker connected in another area of the house; due to this, many parents are therefore unable to move or leave the position where they can hear the sound clearly. The baby monitor we have created not only allows parents to view the room from any device but also notifies the parents if the child is awake.

FINISHED PROJECT

 

Fig 1. Completed Final Project Hardware

 

Project Objectives/Features

The motion sensor will detect an intruder in the room, the camera will allow parents to view the room remotely, and the microphone will detect if the baby cries and inform the parent when the sound is detected. The microphone is from recipe 15.5, the camera is from 1.17, the motion detector is from 12.8, and the email notification is from 16.7. We will combine all of these to create the baby.

Although we initially planned to do this using the Raspberry Pi 3b+, Raspberry Pi Camera V2, a microphone, and a PIR sensor, some components did not work. Our Raspberry Pi 3b+ fried during the process, so we had to switch to a Raspberry Pi 4 keyboard kit. The Raspberry Pi Camera V2 was incompatible with our program, so we switched to a USB webcam. Lastly, we did not add a PIR motion detector due to time constraints.

Approach

Connecting the Camera

The first step to compiling the baby monitor is to connect the Raspberry Camera. We initially used the Raspberry Pi Camera V2. We used the recipe “1.17 Installing the Raspberry Pi Camera Module” from the textbook Raspberry Pi Cookbook: Software and Hardware Problems and Solutions to install the hardware. Then we used the recipe “4.3 Making a Webcam Server” to set the Raspberry Pi to a webcam server. However, the camera was not compatible with the recipe. We tried various other recipes in the book, but they were ineffective. The reason behind the camera not working was due to the apparatus being faulty. We opted for a new USB camera: Logitech C270 HD webcam. We used Motion library–a program that monitors various cameras connected to the PI–got this from a GitHub repository and edited recipe 4.3. We ran the program through the terminal.

Installing Motion

The first step we carried out was to update and reboot the Raspberry Pi to the latest version. We then installed the dependencies so we could then install Motion. 

sudo apt update
sudo apt upgrade
 
sudo apt install autoconf automake build-essential pkgconf libtool git libzip-dev libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev default-libmysqlclient-dev libpq-dev libsqlite3-dev libwebp-dev
 
sudo reboot

We then installed the Motion deb file using the wget command. The wget is used to download files from the server and can work in the background without hindering the current process. We then extracted the files using the dpkg command. 

sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.4.0/$(lsb_release -cs)_motion_4.4.0-1_armhf.deb
sudo dpkg -i $(lsb_release -cs)_motion_4.4.0-1_armhf.deb

Configuring motion

Once motion is installed, we had to configure it and carry out changes to enable the web camera to work. To do this we used the recipe 4.6 in the textbook. We edited Motion’s configuration file through the nano editor in Terminal. 

sudo nano /etc/motion/motion.conf
       damemon off
       stream_localhost off

We wanted the program to run continuously as a service, so we had to turn damemon off. Further, we turned stream_localhost off to stream the webcam server through various devices instead of just the Pi.

After we completed the configuration, we were able to start running Motion. First, we wanted the program to run every time the Raspberry Pi would power on. This would make it easier on the parents, since they would not have to run various commands every time, they turned on/off the Pi. Then we ran motion.

sudo systemctl enable motion
sudo systemctl start motion

You can view the web stream from any device connected in the network by typing in the Raspberry Pi IP address and adding the port number :8081 at the end. 

 

Fig 2. Webcam Server

 

Connecting the Sound Sensor and Sending Emails

In the beginning, we wanted to use a USB microphone that was inbuilt into the Logitech C270 HD webcam, but we discovered that we could not use the camera and the microphone feature simultaneously; therefore, we decided to purchase a KY 038 microphone sound sensor. The device emits a signal if the front of the microphone of the sensor detects noise. A rotary potentiometer can adjust the sensitivity of the sensor.

We initially decided to use recipe 15.5 for the microphone and recipe 16.7 to send messages to the parents using ThingSpeak. However, email would be most appropriate as most devices have the app pre-installed. For this part of the project, we combined recipe 7.16, sending an email from Python, and modified it with the sound code, which would send the notification to the parent when the sound is received.

 

Fig 3. KY 038 Microphone

 

The LED in the KY038 Microphone shows if the sensor is powered and indicates if a noise is detected. By flashing green. This sensor has three functional components on its circuit board: the front sensor unit, which physically measures the environment and outputs it as an analog signal to the second unit, the amplifier, which amplifies the signal depending on the resistance set on the rotary potentiometer and sends it to the analog output of the module.

Code Breakdown

Refer to Appendix to see the full code. We imported RPI.GPIO as GPIO, this module allows us to control the GPIO channels on the Raspberry Pi. Import time allows us to delay the execution of code by seconds. Further, we used the function sleep and import smtplib which allows us to send an email. We set up the pin, channel as 17 because it is the input pin in the Pi, the GPIO.setmode(GPIO.BCM) is for the GPIO numbering and the GPIO.setup(channel,GPIO.IN) sets up the channel port as input. The code includes two functions; send email and the callback function. The send email functions include the credentials of the sender (we created a separate email for the Raspberry Pi) and email address of the receiver, and the body message the user can curate to the receiver. Server = smtplib.smtp(‘smtp.gmail.com’,587) is the server and the port number for a Gmail email address. If perhaps the user wants to change the email and it is a different email, they will have to change the server email and the corresponding port number. The callback function receives the digital output from the sensor, if sound is received, it prints “Sound Detected!” and then runs the send_mail_now() function. GPIO.add_event_detect (channel, GPIO.BOTH, bouncetime=300) lets us know when the pin goes high or low. GPIO.add_event_callback (channel, callback) assigns a function to GPIO pin and then runs the function on change. Lastly, the while true allows an infinite loop which will keep the program running forever.

 

Fig 4. Sound Detection Email

 

Conclusion

We had a lot of challenges that led us to change recipes and exclude other components in the project. A significant challenge was that our Raspberry Pi stopped working. It would power on but not display any output on the screen and signal. Most of our time was consumed figuring out this issue. Once we got a replacement, we discovered the Raspberry Pi Camera V2 was faulty and would not work with the Motion program, which led us to look for another replacement. This process was time-consuming as we had to wait for many of the devices needed for the project. Once we got the webcam, we wanted to use it for both as a webcam and microphone but discovered later that we could only use one of the hardware and not both simultaneously. Due to this, we purchased a KY 038 microphone sound sensor.

Another primary concern for us was the code for the sound sensor. When sound is detected, it sends the email notification multiple times; although this is not optimal due to time constraints, we were unable to find a more feasible solution. We also had to exclude the PIR motion sensor due to time and hardware malfunctions that occurred previously. Although the project began as something very simple, it was difficult due to the setbacks. However, during this time, we were able to learn from our mistakes, reflect on our previous lessons, and improve our skills. Our experience was great, and it showed us that computer science does not only entail us sitting behind screens but also creating things that can be fun and beneficial.

Appendix

# soundDetector.py
import RPi.GPIO as GPIO
import time
import smtplib

time.sleep(10)

channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)

def send_mail_now():
    TO = 'myakvb@gmail.com'
    SUBJECT = 'SOUND DETECTED'
    TEXT = 'Baby is crying!!!'

    gmail_sender = 'myafrozetest@gmail.com'
    gmail_passwd = 'sznifbvqprggwjkg'

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo
    server.login(gmail_sender, gmail_passwd)

    BODY = '\r\n'.join([
           'TO: %s' % TO,
           'From: %s' % gmail_sender ,
           'Subject: %s' % SUBJECT ,
           '',
           TEXT
           ])
    try:
       server.sendmail(gmail_sender, [TO], BODY)
       print('email sent')  
    except:
       print('error')

    server.quit()


def callback(channel):
        if GPIO.input(channel):
                print("Sound Detected!")
                send_mail_now()

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)  
GPIO.add_event_callback(channel, callback)

while True:
        time.sleep(1)