使用树莓派构建一个婴儿监视器
树莓派

> 
> 比一般的视频监控还要好，这种 DIY 型号还有婴儿房间的自动室温控制功能。
> 
> 
> 


![](/data/attachment/album/201805/24/174758uck2cleyykckgomy.png)


香港很湿热，即便是晚上，许多人为了更舒适，在家里也使用空调。当我的大儿子还是一个小婴儿的时候，他卧室的空调还是需要手动控制的，没有温度自动调节的功能。它的控制器只有开或者关，让空调整个晚上持续运行会导致房间过冷，并且也浪费能源和钱。


我决定使用一个基于 [树莓派](https://opensource.com/tags/raspberry-pi) 的 [物联网](https://opensource.com/tags/internet-things) 解决方案去修复这个问题。后来我进一步为它添加了一个[婴儿监视器](https://opensource.com/article/17/9/gonimo)插件。在这篇文章中，我将解释我是如何做的，它的代码在 [我的 GitHub](https://github.com/jervine/rpi-temp-humid-monitor) 页面上。


### 设计空调控制器


解决我的问题的第一个部分是使用了一个 Orvibo S20 [可通过 WiFi 连接的智能插头](https://www.amazon.co.uk/marsboy-S20-Automation-Control-Smartphone/dp/B01LXKPUDK/ref=sr_1_1/258-6082934-2585109?ie=UTF8&amp;qid=1520578769&amp;sr=8-1&amp;keywords=orvibo+s20)和智能手机应用程序。虽然这样可以让我通过远程来控制空调，但是它还是手动处理的，而我希望尝试让它自动化。我在 [Instructables](http://www.instructables.com/id/Raspberry-Pi-Temperature-Humidity-Network-Monitor/) 上找到了一个满足我的需求的项目：他使用树莓派从一个 [AM2302 传感器](https://www.adafruit.com/product/393) 上测量附近的温度和湿度，并将它们记录到一个 MySQL 数据库中。


使用压接头将温度/湿度传感器连接到树莓派的相应 GPIO 针脚上。幸运的是，AM2302 传感器有一个用于读取的 [开源软件](https://github.com/adafruit/Adafruit_Python_DHT)，并且同时提供了 [Python](https://opensource.com/tags/python) 示例。


与我的项目放在一起的用于 [AM2302 传感器](https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/legacy/Adafruit_DHT_Driver_Python) 接口的软件已经更新了，并且我使用的原始代码现在应该已经过时了，停止维护了。这个代码是由一个小的二进制组成，用于连接到传感器以及解释读取并返回正确值的 Python 脚本。


![Raspberry Pi, sensor, and Python code](/data/attachment/album/201805/24/174759en8sz580cqsscc50.png "Raspberry Pi, sensor, and Python code")


*树莓派、传感器、以及用于构建温度/湿度监视器的 Python 代码。*


将传感器连接到树莓派，这些 Python 代码能够正确地返回温度和湿度读数。将 Python 连接到 MySQL 数据库很简单，并且也有大量的使用 python-mysql 绑定的代码示例。因为我需要持续地监视温度和湿度，所以我写软件来实现这些。


事实上，最终我用了两个解决方案，一是作为一个持续运行的进程，周期性（一般是间隔一分钟）地获取传感器数据，另一种是让 Python 脚本运行一次然后退出。我决定使用第二种方法，并使用 cron 去每分钟调用一次这个脚本。之所以选择这种方法的主要原因是，（通过循环实现的）持续的脚本偶尔会不返回读数，这将导致尝试读取传感器的进程出现堆积，最终可能会导致系统挂起而缺乏可用资源。


我也找到了可以用程序来控制我的智能插头的一个 [Perl 脚本](https://github.com/franc-carter/bauhn-wifi)。它是解决这种问题所需的一部分，因此当某些温度/湿度达到触发条件，将触发这个 Perl 脚本。在做了一些测试之后，我决定去设计一个独立的 `checking` 脚本，从 MySQL 去拉取最新的数据，然后根据返回的值去设置智能开关为开或关。将插头控制逻辑与传感器读取脚本分开，意味着它们是各自独立运行的，就算是传感器读取脚本写的有问题也没事。


配置一个打开/关闭空调的温度值是很有意义的，因此，我将这些值转移到控制脚本读取的配置文件中。我也发现，虽然传感器的值一般都很准确，但是，偶尔也会出现返回不正确读数的情况。这个传感器脚本被修改为不向 MySQL 数据库中写入与前值差异非常大的值。同样也在配置文件中写入了连续读取的温度/湿度之间允许的最大差异值，如果读取的值处于这些限制值以外，这些值写不会提交到数据库中。


虽然，做这个自动调节器似乎花费了很多努力，但是，这意味着，记录到 MySQL 数据库的数据是有效的、可用于进一步去分析识别用户使用模式的有用数据。可以用多种图形方式去展示来自 MySQL 数据库中的数据，而我决定使用 [Google Chart](https://developers.google.com/chart/) 在一个 Web 页面上显示数据。


![Temperature and humidity chart](/data/attachment/album/201805/24/174759ukjrbb3wpjwjebo2.png "Temperature and humidity chart")


*过去六小时内测量到的温度和湿度*


### 添加一个婴儿监视摄像头


树莓派开放的性能意味着我可以不断地为它增加功能 —— 并且我有大量的未使用的可用 GPIO 针脚。我的下一个创意是去添加一个摄像头模块，将它放在孩子的卧室中，配置它去监视婴儿。


我需要一个能够在黑暗环境中工作的摄像头，而 [Pi Noir](https://www.raspberrypi.org/products/pi-noir-camera-v2/) 摄像头模块是非常适合这种条件的。Pi Noir 除了没有红外过滤之外，同样也是树莓派常用的摄像头模块。这意味着它在白天时的图像可能有点偏紫色，但是它可以在黑暗中借助红外灯来显示图像。


现在我需要一个红外光源。由于树莓派非常流行，并且进入门槛很低，因此它有大量的外围配件和插件。也有适合它的各种红外光源，我注意到的其中一个是 [Bright Pi](https://www.pi-supply.com/product/bright-pi-bright-white-ir-camera-light-raspberry-pi/)。它可以从树莓派上供电，并且它很适合为树莓派的摄像头模块提供红外光和普通光。它唯一的缺点是太考验我的焊接技能了。


我的焊接技能还是不错的，但是可能花费的时间比其他人更长。我成功地连接了外壳上所有的红外 LEDs，并将它们连接到树莓派的 GPIO 针脚上。这意味着树莓派能够编程控制红外 LED 是否点亮，以及它的亮度。


通过一个 Web 流去公开捕获的视频也很有意义，因为这样我就可以从 Web 页面上查看温度和湿度的读数图表。进一步研究之后，我选择了一个使用 M-JPEG 捕获器的 [流软件](https://elinux.org/RPi-Cam-Web-Interface)。通过 Web 页面公开 JPG 源，我可以在我的智能手机上去连接摄像头查看程序，去查看摄像头的输出。


### 做最后的修饰


没有哪个树莓派项目都已经完成了还没有为它选择一个合适的外壳，并且它有各种零件。在大量搜索和比较之后，有了一个显然的 [赢家](https://smarticase.com/collections/all/products/smartipi-kit-3)：SmartPi 的乐高积木式外壳。乐高的兼容性可以让我去安装温度/湿度传感器和摄像头。下面是最终的成果图：


![](/data/attachment/album/201805/24/174800xa1eyqu3upglie7i.png)


在这以后，我对我的这个作品作了一些改变和更新：


* 我将它从树莓派 2 Model B 升级到了 [树莓派 3](https://opensource.com/article/18/3/raspberry-pi-3b-model-news)，这意味着我可以使用 USB WiFi 模块。
* 我用一个 [TP-Link HS110](https://www.tp-link.com/uk/products/details/cat-5258_HS110.html) 智能插头替换了 Orvibo S20。
* 我也将树莓派插到了一个智能插头上，这样我就可以远程重启/重置它了。
* 我从树莓派上将 MySQL 数据库移走了，它现在运行在一个 NAS 设备上的容器中。
* 我增加了一个非常 [灵活的三角夹](https://www.amazon.com/Flexpod-Flexible-Tripod-Discontinued-Manufacturer/dp/B000JC8WYA)，这样我就可以调整到最佳角度。
* 我重新编译了 USB WiFi 模块，禁用了板载 LED 指示灯，这就是升级到树莓派 3 的其中一个好处。
* 我因此为我的第二个孩子设计了另外一个监视器。
* 因为没有时间去折腾，我为我的第三个孩子购买了夜用摄像头。


想学习更多的东西吗？所有的代码都在 [我的 GitHub](https://github.com/jervine/rpi-temp-humid-monitor) 页面上。


想分享你的树莓派项目吗？[将你的故事和创意发送给我们](http://opensource.com/story)。




---


via: <https://opensource.com/article/18/3/build-baby-monitor-raspberry-pi>


作者：[Jonathan Ervine](https://opensource.com/users/jervine) 译者：[qhwdw](https://github.com/qhwdw) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Hong Kong can be hot and humid, even at night, and many people use air conditioning to make their homes more bearable. When my oldest son was a baby, the air conditioning unit in his bedroom had manual controls and no thermostat functionality. It was either on or off, and allowing it to run continuously overnight caused the room to get cold and wasted energy and money.

I decided to fix this problem with an [Internet of Things](https://opensource.com/tags/internet-things) solution based on a [Raspberry Pi](https://opensource.com/tags/raspberry-pi). Later I took it a step further with a [baby monitor](https://opensource.com/article/17/9/gonimo) add-on. In this article, I'll explain how I did it, and the code is [available on my GitHub](https://github.com/jervine/rpi-temp-humid-monitor) page.

## Setting up the air conditioner controller

I solved the first part of my problem with an Orvibo S20 [WiFi-connected smart plug](https://www.amazon.co.uk/marsboy-S20-Automation-Control-Smartphone/dp/B01LXKPUDK/ref=sr_1_1/258-6082934-2585109?ie=UTF8&qid=1520578769&sr=8-1&keywords=orvibo+s20) and smartphone application. Although this allowed me to control the air conditioning unit remotely, it was still a manual process, and I wanted to try and automate it. I found a project on [Instructables](http://www.instructables.com/id/Raspberry-Pi-Temperature-Humidity-Network-Monitor/) that seemed to match my requirements: It used a Raspberry Pi to measure local temperature and humidity readings from an [AM2302 sensor](https://www.adafruit.com/product/393) and record them to a MySQL database.

Using crimp terminal contacts with crimp housings made it a pinch to connect the temperature/humidity sensor to the correct GPIO pins on the Raspberry Pi. Fortunately, the AM2302 sensor has [open source software](https://github.com/adafruit/Adafruit_Python_DHT) for taking readings, with helpful [Python](https://opensource.com/tags/python) examples.

The software for [interfacing with the AM2302 sensor](https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/legacy/Adafruit_DHT_Driver_Python) has been updated since I put my project together, and the original code I used is now considered legacy and unmaintained. The code is made up of a small binary object to connect to the sensor and some Python scripts to interpret the readings and return the correct values.

![Raspberry Pi, sensor, and Python code](https://opensource.com/sites/default/files/u128651/pibabymonitor_materials.png)

opensource.com

With the sensor connected to the Raspberry Pi, the Python code can correctly return temperature and humidity readings. Connecting Python to a MySQL database is straightforward, and there are plenty of code examples that use the `python-`

`mysql`

bindings. Because I needed to monitor the temperature and humidity continuously, I wrote software to do this.

In fact, I ended up with two solutions, one that would run continuously as a process and periodically poll the sensor (typically at one-minute intervals), and another Python script that ran once and exited. I decided to use the run-once-and-exit approach coupled with cron to call this script every minute. The main reason was that the continuous (looped) script occasionally would not return a reading, which could lead to a buildup of processes trying to read the sensor, and that would eventually cause a system to hang due to lack of available resources.

I also found a convenient [Perl script](https://github.com/franc-carter/bauhn-wifi) to programmatically control my smart plug. This was an essential piece of the jigsaw, as it meant I could trigger the Perl script if certain temperature and/or humidity conditions were met. After some testing, I decided to create a separate `checking`

script that would pull the latest values from the MySQL database and set the smart plug on or off depending upon the values returned. Separating the logic to run the plug control script from the sensor-reading script also meant that it operated independently and would continue to run, even if the sensor-reading script developed problems.

It made sense to make the temperature at which the air conditioner would switch on/off configurable, so I moved these values to a configuration file that the control script read. I also found that, although the sensor was generally accurate, occasionally it would return incorrect readings. The sensor script was modified to not write temperature or humidity values to the MySQL database that were significantly different from the previous values. Likewise the allowed variance of temperature or humidity between consecutive readings was set in a general configuration file, and if the reading was outside these limits the values would not be committed to the database.

Although this seemed like quite a lot of effort to make a thermostat, recording the data to a MySQL database meant it was available for further analysis to identify usage patterns. There are many graphing options available to present data from a MySQL database, and I decided to use [Google Chart ](https://developers.google.com/chart/)to display the data on a web page.

![Temperature and humidity chart](https://opensource.com/sites/default/files/u128651/pibabymonitor_temp-humidity.png)

opensource.com

## Adding a baby monitor camera


The open nature of the Raspberry Pi meant I could continue to add functionality—and I had plenty of open GPIO pins available. My next idea was to add a camera module to set it up as a baby monitor, given that the device was already in the baby's bedroom.

I needed a camera that works in the dark, and the [Pi Noir](https://www.raspberrypi.org/products/pi-noir-camera-v2/) camera module is perfect for this. The Pi Noir is the same as the Raspberry Pi's regular camera module, except it doesn't have an infrared (IR) filter. This means daytime images may have a slightly purple tint, but it will display images lit with IR light in the dark.

Now I needed a source of IR light. Due to the Pi's popularity and low barrier of entry, there are a huge number of peripherals and add-ons for it. Of the many IR sources available, the one that caught my attention was the [Bright Pi](https://www.pi-supply.com/product/bright-pi-bright-white-ir-camera-light-raspberry-pi/). It draws power from the Raspberry Pi and fits around the camera Pi module to provide a source of IR and normal light. The only drawback was I needed to dust off my rusty soldering skills.

It might have taken me longer than most, but my soldering skills were up to it, and I was able to successfully attach all the IR LEDs to the housing and connect the IR light source to the Pi's GPIO pins. This also meant that the Pi could programmatically control when the IR LEDs were lit, as well as their light intensity.

It also made sense to have the video capture exposed via a web stream so I could watch it from the web page with the temperature and humidity readings chart. After further research, I chose to use a [streaming software](https://elinux.org/RPi-Cam-Web-Interface) that used M-JPEG captures. Exposing the JPG source via the web page also allowed me to connect camera viewer applications on my smartphone to view the camera output there, as well.

## Putting on the finishing touches

No Raspberry Pi project is complete without selecting an appropriate case for the Pi and its various components. After a lot of searching and comparing, there was one clear [winner](https://smarticase.com/collections/all/products/smartipi-kit-3): SmartPi's Lego-style case. The Lego compatibility allowed me to build mounts for the temperature/humidity sensor and camera. Here's the final outcome:

![Completed Raspberry Pi baby monitoring system](https://opensource.com/sites/default/files/u128651/pibabymonitor_case.png)

opensource.com

Since then, I've made other changes and updates to my setup:

- I upgraded from a Raspberry Pi 2 Model B to a
[Raspberry Pi 3](https://opensource.com/article/18/3/raspberry-pi-3b-model-news), which meant I could do away with the USB WiFi module. - I replaced the Orvibo S20 with a
[TP-Link HS110](https://www.tp-link.com/uk/products/details/cat-5258_HS110.html)smart plug. - I also plugged the Pi into a smart plug so I can do remote reboots/resets.
- I migrated the MySQL database off the Raspberry Pi, and it now runs in a container on a NAS device.
- I added a
[flexible tripod](https://www.amazon.com/Flexpod-Flexible-Tripod-Discontinued-Manufacturer/dp/B000JC8WYA)to allow for the best camera angle. - I recompiled the USB WiFi module to disable the onboard flashing LED, which was one of the main advantages to upgrading to a Raspberry Pi 3.
- I've since built another monitor for my second child.
- I bought a bespoke night camera for my third child … due to lack of time.

Want to learn more? All the code is [available on my GitHub](https://github.com/jervine/rpi-temp-humid-monitor) page.

*Do you have a Raspberry Pi project to share? Send us your story idea.*

## Comments are closed.