Software apps and online services
Hand tools and fabrication machines
My daughter is a huge Star Wars fan (even though she hasn't seen any of the movies yet). Not surprisingly, she was all over it when I asked if she wanted to build a Star Wars themed project. I don't know how many times she has made me play the Imperial March (Darth Vader's theme song) when she is wearing her Darth Vader costume so that she can make a grand entrance into the room. So, we quickly settled on this project idea - automatically detect someone coming home and then trigger the playing of the Imperial March just before the person walks into the house.
This is a fairly simple project so it's well suited for beginners. I found it interesting because it's the first time I have used the publish / subscribe pattern to / from the cloud. It was also the first time I integrated different types of micro controllers into a single project. I love the Photon's simplicity and ease of use when it comes to creating a cloud connected solution. And you can't beat the variety and magnitude of add-on hardware available for the Arduino. Being able to put them together so easily enabled this project to come together quickly.
Step 1: Assemble the sensor
Time: 10 minutes
Parts: Photon, 220 Ohm resistor, LED, PIR motion sensor, breadboard, male / male jumper wires, USB micro B cable
First, connect the LED to the Photon. Mount your Photon into the breadboard. Then connect the resistor between a Photon's ground pin and an open row on the breadboard. Finally, connect the long leg of the LED to a digital PIN on the Photon (I used pin D0) and the short leg of the LED to the open row to which the resistor is connected. Frankly, the LED is optional but I use it to indicate the state of the sensor as the PIR sensor itself provides no indication of when motion is detected nor does the Photon provide any indication when it publishes the event to the cloud.
Next, connect the PIR sensor to the Photon. Mount the PIR sensor to the breadboard - I use right angled female header pins to connect the PIR sensor to the breadboard as this allows me to "secure" the PIR sensor to the breadboard vertically. (If you don't have right angle header pins, then use some double sided tape or some sticky tack to mount your PIR sensor to the table or whatever you are mounting it to and use female / male jumper wires straight from the PIR sensor's male pins to the breadboard.) Use jumper wires to connect the PIR's GND to a Photon's GND pin, the PIR's 5V pin to the Photon's Vin pin, and the PIR's signal pin to a digital pin on the Photon (I used pin D7).
Double check the data sheet for your sensor to make sure you correctly identify the sensor's 5V, GND, and signal pin as their order varies by manufacturer. Even though my PIR sensor operates at 5 volts, it's signal pin operates at only 3.3V which is why it can be connected directly to a digital pin on the Photon. Again, double check the data sheet for your sensor to ensure that the signal's voltage is 3.3V.
Power up the assembly via the Photon's USB Micro B connector to either a PC or a wall outlet. Open up the Particle IDE and paste in sensor code provided below. Then flash the code to the Photon.
Now, if you wave your hand in front of the sensor, the LED should light up. Also, you can check your logs on the Particle Dashboard to see that the event was published.
Time: 10 minutes
Parts: Photon, 220 Ohm resistor, LED, breadboard, male / female jumper wires
Connect the LED and resistor to the photon just as you did in the prior step. Again, the LED is optional but I find it useful to indicate when an event is received from the Particle cloud. Next, connect the male end of the jumper wires to Photon's Vin, GND, and a digital pin (I used pin D7). The digital pin from the Photon will connect to one of the Music Maker's GPIO pins to trigger the music. The Photon's Vin and GND connections will be used to power the Photon from the Arduino.
But first, before connecting to the Arduino, power up the assembly via the Photon's USB Micro B connector to either a PC or a wall outlet. Open up the Particle IDE and paste in trigger code provided below. Then flash the code to the Photon - ensure you have selected the correct device to flash the code to now that you have two devices connected to the Particle IDE.
Step 3: Assemble the Arduino and Music Maker shield
Time: 20 minutes
Parts: Arduino Uno, Adafruit Music Maker shield, Photon, 220 Ohm resistor, LED, mini breadboard, jumper wires, USB cable
Tools: soldering iron
Header pins are included with Music Maker shield, however you must solder them. In addition to the header pins to connect the shield to the Arduino, I added male header pins on top of the shield for GPIO and power connections from the Photon. (If I had female header pins, I would have use them to so that I would only need male / male jumper wires. But, I only had male header pins on hand (in addition to male / female jumper wires) so I went that route instead.)
Connect the jumper wire from the Photon's digital pin to a GPIO pin on the shield (I used pin 7 on the shield). Connect the jumper from the Photon's Vin to the 5v pin on the shield and the GND jumper to the shield's GND pin.
Finally, connect your speaker(s) to the terminal block(s) on the shield - there is one terminal block for a right speaker and one for a left speaker. I just connected one speaker - I salvaged a small speaker from one of my daughter's broken toys before we threw it out.
Connect the Arduino to your development machine, paste the Arduino sketch below into the Arduino IDE, and upload it.
That's it. Go walk in front of the sensor and sit back and wait for the music to start.About the Code
As I mentioned earlier, this was the first time I used the publish / subscribe pattern. It was quite easy to do with the Photon. In the sensor code, when the PIR sensor's signal pin goes HIGH, I turn on the LED and publish an event to the Particle cloud. Publishing an event just requires one line:
published = Particle.publish("motion-detected", NULL, 60, PRIVATE);
Initially, I didn't make the publish private - I didn't care as there is no personally identifiable information. But, when I went to subscribe to the event, I was getting a lot of false alarms - apparently, a lot of other users are publishing "motion" oriented events. So, I switched the subscribe call to only subscribe to events from my devices by adding the "MY_DEVICES" parameter:
Particle.subscribe("motion", soundAlarm, MY_DEVICES);
And in order for the subscribe function to limit itself only to my devices, I had to make the publish private.
The trigger code simply calls the event handler - soundAlarm - when the motion event occurs. All the event handler does is turn on the LED and turn the GPIO pin on the Music Maker shield HIGH. The Arduino sketch detects the GPIO pin being HIGH and then waits 60 seconds (for me to park my car, gather my things, and enter the house) before playing the MP3 file on the SD card.
Deploy the Project
I setup the sensor in the garage on the shelves right next to where I park my car. I placed the trigger and the Arduino with Music Maker shield and speaker on the kitchen counter as my daughter is usually at the kitchen table when I get home. Both locations have wall outlets nearby which made it easy to power both parts of the project.