This tutorial is wrote by one of our customer Jaron Horst. Thank him so much for writing such a detailed tutorial!
It’s pretty basic knowledge that there are many, many baby monitors out there. You have versions with audio, video, and even some that will let you view your baby using an application on your phone. However Jaron just bought a new house and he wanted to find a cheap way to help his wife out so she wouldn’t have to try and randomly listen for their son to see if he was awake! Following is the original text:
Thankfully, I had a Raspberry Pi and old webcam sitting around. It took a matter of minutes to set it up with MotionEyeOS and put it on my network. I thought too that I could use the motion detection from the camera to let me know when my son was up. Well, it worked great…. If you liked a very dark screen that told you next to nothing and didn’t have enough light to trigger the motion detection! When it did have enough light, it just flooded us with notifications of movement. Every. Time. He. Moved.
Unwilling to admit defeat, I started to rethink my strategy. For starters, I found that PIR sensor could be bought off ebay for next to nothing. So I added that as my means of looking for movement. In addition, I added a button (an old doorbell trigger) for my wife and I to press before/after every naptime. At this point, I could see when naptime started, wait for the movements to slow down to estimate when my son was asleep, approximate him waking up when I started to detect movements again, and finally know when my wife or I went to get my son out of bed.
This worked really well and was the first point that this thing was actually almost useful! But, we still had some short-commings. For starters, he would be still during naptime but more active throughout the night. This resulted in a lot of false positives throughout the night which quickly got old for my wife and I. I concluded that there really wasn’t any way to make a one-size-fits-all method to detect when he was awake. Also, not having the ability to see in the dark - to see if he was actually awake - was becoming a larger issue.
After a short time, I got the point where I just had to address the webcam and its inability to see in the dark. A camera sensor, by default, can see much more than the human eye. This includes the IR spectrum. Because of this, there is a UV filter placed between the lens and the sensor itself which blocks out the IR light. Knowing this, I found a very cheap, $4.99 (!) webcam, took it apart, and removed the IR filter. Now I had my own cheap IR camera!
Next, I went on Amazon and bought a cheap IR light. Combining this with a set of 433mHz outlet switches and an interface I made the year before with an earlier home automation project, I effectively made my own IR light for my IR camera. Furthermore, since I “knew” when my son fell asleep or might be awake, I could intelligently control the IR light so it wasn’t on the entire time (on when he may be awake, off when asleep).
Finally, I decided that I needed to be intelligent with figuring out when my son was awake. I concluded that the best way to do so was to make it self-learning. I wrote my own software that would analyse my sons’ naps. It would look at a 24 hour period and, based on the history, figure out various stats. For example: what is the average naptime length at 4pm? Based on the naps that happened over 4p, what is the best number of movements to look for in a timeframe to assume my son is awake?
For sessions that overlapped each hour, what is the average length?
What the system thinks is the best Timeframe [“in the last n minutes”]and
Threshold [“n many movements”] for detecting when my son is awake.
As I developed these stats, I could intelligently figure out what the best params are for determining when my son was awake. The results could also change throughout the day to account for differences in his morning nap, afternoon nap, and overnight sleep. It also could continuously do analytics to keep it accurate as his sleep patterns changed over time. Overall, the accuracy is now around 75% but that would be even higher if it wasn’t for human error (sometimes we let him play for a bit in his crib or are unable to immediately go get him -- things the stats just can’t take into account and therefore makes it thinks it was wrong).
Graph of overnight movement. Shows where he fell asleep, how often he tossed and turned, then finally when he woke up.
Vertical lines show when the system assumed he was awake/asleep and alerted us.
The system has been further integrated with other aspects of our home thanks to its modular design. For example, we can automatically disable the doorbell chime during naptime, get notices on our phone when he wakes up, and even ask Alexa if our son is awake yet. The MQTT support allows me to continue adding more integration as my IoT collection expands while the web interface - which is only available on our internal network - can be used on any device with a web browser (including the LattePanda touch screen in our livingroom).
My wife and I are very happy with the final product. We don’t have the security concerns that come with an off-the-shelf product, we’re not locked into some custom device or app for viewing our video feed, and we have something far more intelligent then you can buy just now. It was also a great learning experience for myself as I learned a lot about UI’s, databases, caching, various algorithms and the optimizing of them, as well as other great IoT concepts.
Web-based UI for the current status during a nap. The UI provides me with: when naptime started, what the average naptime length is this time of day, what the current Awake detection settings are, the ability to turn on/off the IR light out-of-band, etc.