
Better than regular video surveillance, this DIY model also has automatic room temperature control for the baby’s room.
Hong Kong is very humid and hot, even at night, many people use air conditioning at home for comfort. When my older son was a small baby, the air conditioning in his bedroom still needed to be manually controlled, without an automatic temperature regulation feature. Its controller only had on or off, letting the air conditioning run all night would cause the room to become too cold and waste energy and money.
I decided to use an IoT solution based on Raspberry Pi to fix this problem. Later, I further added a baby monitor plugin for it. In this article, I will explain how I did it, and the code is on my GitHub page.
Designing the air conditioning controller
The first part of solving my problem was using an Orvibo S20 WiFi-connected smart plug and smartphone app. While this allowed me to control the air conditioning remotely, it was still manual, and I wanted to try to automate it. I found a project on Instructables that met my needs: it used Raspberry Pi to measure the nearby temperature and humidity from an AM2302 sensor and logged them into a MySQL database.
Connect the temperature/humidity sensor to the corresponding GPIO pins on the Raspberry Pi using crimp connectors. Fortunately, the AM2302 sensor has open-source software for reading and also provides Python examples.
The software for interfacing with the AM2302 sensor that came with my project has been updated, and the original code I used is now outdated and no longer maintained. This code consists of a small binary for connecting to the sensor and a Python script for interpreting the readings and returning the correct values.
Raspberry Pi, sensor, and Python code used to build the temperature/humidity monitor.
Connecting the sensor to the Raspberry Pi, these Python codes can correctly return temperature and humidity readings. Connecting Python to the MySQL database is straightforward, and there are plenty of code examples using python-mysql bindings. Since I needed to continuously monitor the temperature and humidity, I wrote software to achieve this.
In fact, I ended up using two solutions, one as a continuously running process that periodically (generally every minute) fetches sensor data, and the other is to have the Python script run once and then exit. I decided to use the second method and use cron to call this script once a minute. The main reason for choosing this method is that a continuously running script (implemented through a loop) occasionally fails to return readings, which can lead to a backlog in processes trying to read the sensor, potentially causing the system to hang due to lack of available resources.
I also found a Perl script that could programmatically control my smart plug. It is part of the solution needed for this problem, so when certain temperature/humidity triggers are reached, this Perl script will be triggered. After doing some tests, I decided to design an independent checking
script to pull the latest data from MySQL and then set the smart switch to on or off based on the returned values. Separating the plug control logic from the sensor reading script means they can run independently, so even if there is a problem with the sensor reading script, it’s not an issue.
Setting a temperature value to turn the air conditioning on/off makes a lot of sense, so I moved these values to a configuration file read by the control script. I also found that while the sensor’s values are generally accurate, there are occasional incorrect readings. This sensor script was modified not to write values to the MySQL database that differ significantly from previous values. Similarly, a maximum allowable difference value between consecutive temperature/humidity readings was written in the configuration file; if the readings fall outside these limits, they will not be submitted to the database.
Although creating this automatic regulator seemed to take a lot of effort, it means that the data recorded in the MySQL database is valid and can be used for further analysis to identify user usage patterns. The data from the MySQL database can be displayed in various graphical formats, and I decided to use Google Chart to display the data on a web page.
Temperature and humidity measured over the last six hours
Adding a baby monitor camera
The open performance of Raspberry Pi means I can continuously add features to it — and I have plenty of unused GPIO pins available. My next idea was to add a camera module, placing it in the child’s bedroom, configured to monitor the baby.
I needed a camera that could work in dark environments, and the Pi Noir camera module is very suitable for these conditions. The Pi Noir, besides lacking infrared filtering, is also a commonly used camera module for Raspberry Pi. This means that its images during the day may appear slightly purple, but it can display images in the dark with the help of infrared lights.
Now I needed an infrared light source. Due to the popularity of Raspberry Pi and its low entry barrier, there are plenty of peripheral accessories and plugins available. There are various infrared light sources suitable for it, one of which I noticed is Bright Pi. It can be powered from the Raspberry Pi and is suitable for providing infrared and regular light for the Raspberry Pi camera module. Its only downside is that it tests my soldering skills.
My soldering skills are decent, but it may take me longer than others. I successfully connected all the infrared LEDs on the casing and connected them to the GPIO pins on the Raspberry Pi. This means the Raspberry Pi can programmatically control whether the infrared LEDs are lit and their brightness.
Making the captured video publicly available through a web stream is also meaningful, as I can view the temperature and humidity reading charts from the web page. After further research, I chose a streaming software that uses an M-JPEG capture. By exposing the JPG source on the web page, I can connect to the camera viewing program on my smartphone to view the camera output.
Making final touches
No Raspberry Pi project is complete without choosing a suitable case for it, and there are various parts. After extensive searching and comparison, there was an obvious winner: the SmartPi LEGO-style case. The LEGO compatibility allows me to install the temperature/humidity sensor and camera. Here is the final result:
After this, I made some changes and updates to my creation:
Want to learn more? All the code is on my GitHub page.
Want to share your Raspberry Pi project? Send us your stories and ideas.
via: https://opensource.com/article/18/3/build-baby-monitor-raspberry-pi
Author: Jonathan Ervine[22] Translator: qhwdw Proofreader: wxy
This article is originally translated by LCTT, proudly presented by Linux China