Distraction is something that comes fairly easy to me. No matter how interesting whatever I’m working on might be, I’ll always be able to find distractions from it. This is my way of saying that Rube Goldbot is still in progress, just not the primary thing I’m working on.
One thing that’s been distracting me is learning some more fundamentals: I’ve started playing around with more basic electronics stuff, and am in the process of upping my game there. Rube is a very complex project (and will ultimately be a fairly expensive one) so I’d like to build it correctly.
Another thing that’s been distracting me has been my ongoing home automation project. Specifically, while it’s kind of neat to be able to talk to my lights, I’d much rather I didn’t have to tell my home what I wanted, and that it figured it out by watching me and seeing what I do.
For example, when I get up in the middle of the night and head to the bathroom, I’d like it if the bathroom light would come on at about 1% brightness and then turn off after I leave. Right now, I have to either talk to Alexa and tell her to turn on the bathroom light – the formulating of words requiring a little bit of cognitive overhead that wakes me up a bit – and then she turns it on full blast, which finishes waking me up. Or I can turn on the lights by the mirror, by hand, but ugh? Use my hands? Why would I do that when I have robots?
My requirements are thus:
- Detects when there is motion in the bathroom and turns on the light
- If the time is between 10 PM and 6 AM, the light will come on maybe 1-10% strength
- If the time is outside of that, the light will come on at full strength
- The light will stay on for 5 minutes from the last time it detected motion
From a technical standpoint, then, I’d have to:
- Have some way of detecting motion
- Have whatever detects the motion somehow connect to my Philips Hue bridge and tell the bathroom light to turn on/off
- Be smart enough to know what time it is
- Be able to keep track of how long it’s been since motion was detected
Some quick googling for motion sensors brought me to a set of 5 “PIR Infrared Motion Detectors” for about $6 off of Amazon. Add in some male-to-female jumper wires and I’m relatively set from the “detect motion” part.
My thought was that I would be able to control everything directly from an Arduino. Plug the motion sensor into an Arduino and have the output go to one of the analog inputs on the Arduino and I can know whether there is motion or not. That part is trivial – read the value from the analog input and if it’s greater than 0, handle turning on the light.
For my first pass, I used an Arduino Uno as the controller and an LED as the stand-in for my lightbulb. The code is trivial – read the A0 input, spit the output to the serial window, and if A0 is > 0, turn pin 13 (where I have my LED hooked in) to high. Otherwise, turn pin 13 to low. I put it on a 1 second delay so the light would be on/off for a second. I hooked it up and… the lightbulb was on even when I wasn’t moving. Looking at the serial window output, there was a rhythmic pattern to the numbers and I realized that the motion sensor, being IR, was detecting my breathing (heat). Okay, I can work with that – I gave it a threshold value above which to turn on the light, and now it ignored my breathing unless I exhaled directly onto the sensor.
Second pass was to add in a delay so that it would only stay lit for 30 seconds after the last time it detected motion. This was also similarly trivial: I declared an (ugh) global variable called “countdown” that would contain a countdown of 30,000 milliseconds. In the part of the code that executes when motion is detected I told it to set countdown back to 30. In the main loop, I added another conditional before the “turn it off” that said don’t turn the light off unless countdown was <= 0, and then to subtract 1 from countdown. Worked like a charm.
I’ll do a few more refinements to the sensor part, but it’s basically done. I’m going to swap out the Arduino Uno for an Arduino Pro Mini or Pro Nano, since there’s just a ton of crap on the Uno that I don’t need. I also want to monkey around with sensitivity levels and the like, but that’s similarly trivial.
The next bit is to figure out how to have it connect to my Phillips Hue. To do that I’ll have to add a wifi breakout board to the sensor package and then program that to know the Phillips API. Except that feels pretty dumb to me – what if I change to a different hub? What if the API changes? What if I want the sensor to also trigger multiple other actions? What if, for example, it’s 6 AM and I get up to use the bathroom and I want the light to come on AND I want my coffee maker to start making coffee AND I want my bedroom lights to come on AND I want Alexa to give me a weather update? That’s a lot of different APIs for a Pro Mini to keep up with, and I’d also have to keep on taking the sensor down and reprogramming it whenever I added a new device, and it’s just a hassle.
So, I think I’m going to make something else that the sensor can talk to. My thinking right now is that I’ll have a Raspberry Pi running (probably) a Python+Flask based web service for the sensor to talk to. The sensor will only know it needs to talk to the Pi and say “there is motion that is meaningful!” or “there has not been meaningful motion for the last 5 minutes!” and then the Pi can figure out what that means. The Pi is, compared to the Arduino, a rocket surgeon, and has a huge amount of memory to be able to handle all the different tasks I’d need.
So, next steps:
- Create an endpoint for the sensor kit (need a name for that) to talk to
- Add wifi to the sensor kit and tell it how to talk to the endpoint
- Make the sensor kit use a Pro Mini
- Make the endpoint smart talk to the Phillips Hue API
- Maybe figure out how much power the sensor kit needs – batteries? Wall wart?