Software apps and online services
Hand tools and fabrication machines
We have a string of lights in our basement which, for or a long while now, we have needed to manually plug and unplug whenever we needed a little more light down there. There had to be a better way to do this so I started looking into building some solution.
I knew we needed at least some relay wired up to a motion sensor with at least some minimal microcontroller to turn the lights on and off automatically. It would be great if it could also be monitored and controlled from anywhere (i.e. internet connected somehow) -- bonus points for something fun that would be wife and kids approved.
I had a Particle Photon already so that would cover me for the internet connected bit with little effort but it was still to be stuffed into some boring box -- until I happened upon an R2-D2 measuring cup set (of all things!) which could be used as the enclosure with a little modification.
An so, the Little Sentry Droid was born. Overall it's functioning perfectly for our needs. It turns the lights on when motion is sensed and turns the lights back off after a set delay. I can check the status of the trigger (lights on) and motion (pir) and sound (microphone) sensors from anywhere thanks to the Particle Cloud API. I can also turn the lights on or off or even make noise or turn his head with the push of a button on my phone.
This project isn't super difficult (unless you want to take the amp design and/or sound detection beyond what I've been able to manage -- I'm still learning a lot), but it does require a great deal of patience as there is a good deal of cutting and gluing (and a few Lego bricks) involved. I _would_ use a multi-strand wire next time instead of a solid core because I had some trouble with the stiffness of the solid core wire when finalizing the assembly.
I can't emphasize this enough, make good use of solderless breadboards to get the design working before committing to a more permanent solution. Half-size boards worked great since they were closer in size to what I would need to fit inside the enclosure I had in mind
There are two boards in this design, one main board with the Photon, LED wiring, servo, microphone and audio connections, and a second board for the amplifier circuit.
After getting prototypes working on solderless breadboards, measure and cut a protoboard for each. Scoring first with a box knife then breaking with small pliers seemed to work well. Be very careful when using the knife. It's really easy to slip up when scoring a slippery uneven surface like a protoboard.
To measure the main board I held a perma-proto board above the main cup and made some marks, trying to get a perfect fit so when pushing the micro USB cable into the back of the droid the board would be pressed against the far side already to resist any repeated force applied.
The amplifier board on the other hand I just made as small as possible to fit on the inside wall of the same cup.
Assembling the main board
I assembled the main board in multiple stages. First the full solderless breadboard prototype, then the perma-proto board assembled with the PIR microphone and server still wired through the solderless breadboard to verify everything was still functioning and finally making more permanent connections as fit into the enclosure.
Don't make any permanent connection to the enclosure yet, you'll want to make all cuts first so you don't need to try to protect the components from bits of plastic while making cuts for the micro USB, screw terminal wire routing and PIR sensor here.
Assembling the amplifier board
Follow the same steps as the main board. Solderless breadboard, perma proto and external test then eventual permenant placement in the 1 cup section along with the main board and other components (it eventually gets pretty snug in there).
For testing the audio and amplifier I used the sfx code in a simple bit of firmware which made a sound every few seconds in a loop.
Now with the two boards nearly completely assembled it's time to modify the enclosure, route wires, mount components and build a solution for the head which will allow it to rotate via the servo and not be permanently stuck on in case anything goes wrong with prolonged use. I'll try to be clear with all of the routing and assembly but I'm sure I'll miss some detail so let me know in the comments if anything's not clear
Starting from the bottom, the screw terminals are in the base, with a small hole out the back for the wires and a hole in the bottom for access to the screws. The terminal block itself is glued to the bottom of the bottom (1/4) cup into a small hole for the metal leads and wires to route up into the next/main (1) cup. Remember, plan -> double check the plan -> cut -> solder/glue. So don't glue yet, not until the holes are cut in the main (1) cup at least for the micro USB (in the back) trigger/screw term (in the bottom) and PIR (in the front).
The main cup will need to be modified so measure (x2) and cut out the holes for the micro USB (in the back) trigger/screw term (in the bottom) and PIR (in the front).
*Now* it's okay to assemble the main and bottom sections. I used Sugru for the bottom of the main board since the Photon headers didn't make for a smooth bottom. Three blobs made it easy to get fairly level.
With the main section done, it's time for the head. Make holes in the head for the microphone and RGB LED. To mount the head to the servo I borrowed a couple of blocks from the kids. The small blocks fit nicely onto the large, bottom to bottom.
There's one last cup to modify, the one in between the main body and the head. This one is maybe the most difficult to get just right as it's the one the servo attaches to. Cut a hole in the bottom that's just the right size for the servo so the pivot point is directly in the middle then make a couple of relief cuts for the microphone and LED wires to sneak by the servo.
With the servo hole cut, the head components can be routed through, the servo mounted (with screws -- pre-drill first to avoid cracking the plastic) and microphone and RGB LED glued in place.
Finally I used a few small dabs of E6000 on the outside of the seams between the lower cups (not the head!) to keep everything together. It's just enough to keep the droid from falling apart but still easy enough to cleanly break later if I need to get inside.
By now the firmware has already been flashed to the Photon during the prototyping steps but it's far from done.
The sound detection bit needs more work. It was not working reliably enough to use it for turning on the lights (still getting too many false-positives) that I've moved it to only keep the lights on if noise is picked up while the trigger is active.
I also want it to be smarter about keeping the lights on while I'm in the basement but not in range of the PIR sensor. I might have it subscribe to presence related events and have other sensors around the house (motion/sound/beacons?) so it can do more than _just_ turn the lights on when it senses motion then turn off with a five minute delay. Well, it _does_ do a little more now via Particle Cloud functions but still, I'd like it to do more.
One of the great features of the Particle Photon (and Core and Electron) is the ability to remotely flash the firmware so I can update the little droid anytime!
Not only can I remotely flash the firmware but with the Particle Cloud I have exposed a few functions and variables which I can access from anywhere with the Particle Cloud API.
Some functions currently exposed include:
- turnon: turn the lights on with a 'forced' flag so they stay on until explicitly turned off
- turnon: turn the lights off
- beep: make some noise
- look: make the head move a bit
And a few variables to check on the state of the droid:
- motion: motion has recently been detected
- sound: sound has recently been detected
- trigger: the lights are on
- forced: the lights have been explicitly turned on
All of that remote functionality is enabled with just a small bit of code:
// Set up cloud functions Particle.function("turnon", turnOn); Particle.function("turnoff", turnOff); Particle.function("beep", makeBeeps); Particle.function("look", makeMovement); // ...and variables Particle.variable("motion", motionActive); Particle.variable("sound", soundActive); Particle.variable("trigger", triggerActive); Particle.variable("forced", forcedActive);
I have also added a fair bit of debug and data (i.e. potentially more useful) event publishing throughout. With the ability to remotely flash the firmware I can always tweak all of this later.Remote control
Since I've exposed a few functions I wanted to make quick use of them from my phone. I _could_ build a native or web app (Particle folks provide a few nice SDKs) but there is also a Particle IFTTT channel! On top of that there's a very useful simple mobile app from IFTTT called the DO Button which allows for one-touch recipe execution from a phone. In just a few minutes I was able to turn the lights on or off or make the droid beep -- all from my phone!
How does the little droid measure up?
I'm happy with how it turned out. I like it, the kids love if (they thank it every time it turns on the lights for them) and my wife gets a kick out of it. I really love that I can keep tweaking the firmware without needing to have a physical connection to it and the Photon offers still a lot more functionality so this will definitely be one of those projects that continues to evolve. Maybe other little droids will start to appear around the house. :)