I’ve finally got in to some long awaited IoT with items I bought over 4 years ago that were lying dormant in storage containers. I backed Spark Core on Kickstarter on 31 May 2013, that’s how long I’ve been meaning to do something with it. Finally that time arrived.
Together with a Spark Core Relay Shield and a garage door magnetic reed switch which I bought around the same time, four years later I was ready to go. (Note that the Spark Core by Particle is discontinued, it’s now a Photon with a new relay shield)
After me and my wife set out to re do our front garden, we thought lights were in order too. What better way to finally use the Spark Core and relay shield to turn them on (at sunset) and off automatically every night. The lights will utilise one relay switch on the relay shield. I thought I’d wire the garage remote to the second relay switch, with a reed switch detecting if the garage door is open or closed. Let’s explore how this all went down.
Note that I will be using a discontinued Spark Core, but just substitute Photon whenever you read Spark Core for your purposes.
- Front garden lights to automatically turn on at sunset, and turn off after midnight
- Create an app to control the garden lights and garage door
- If I leave home and forget to close the garage door, be notified via a push notification
- If I approach home at night, and the front garden lights are not on (ie after midnight), then turn them on for 15 minutes.
- Open or close the garage door via the app
- Toggle the front garden lights on or off via the app
- Enable or disable sunset lights mode (ie have lights automatically turn on at sunset or not)
Spark Core Microcontroller Firmware Programming
To control the relays and detect if the garage door is open, I first had to wire and program the Spark Core microcontroller. You can program firmware for the Spark Core online at https://build.particle.io with lots of documentation to go with it.
Basically the Spark Core is an always connected to the internet, through the Particle Cloud, IoT device. It’s highly available, performant and secure, and you can interact with it via a RESTful API and via real-time event streams to IFTTT, Azure IoT Hub, webhooks, etc.
I created a dedicated guest WiFi network for IoT devices, just in case! I’ve got Ubiquiti UniFi Access Points so this was a breeze to do.
I’ll show the code later on, which is all open source on my GitHub, but let’s get started with the hardware first.
There are 3 main hardware components attached to the Spark Core:
- Garden lights, which include:
- Magnetic Reed Switch to detect if door is open
- Crack open the garage remote and solder some wires to the switch to simulate pressing it via a relay switch.
Finding out how you are going to thread the wire from the garden to the 12V transformer will probably be the hardest part here. I had the help of a sparky when I did my backyard lights, and we somehow managed to get the wiring go inside the cavity of the brickwork from underneath the concrete slab and in to the roof. I don’t think I wanted to do that again this time as it was hard work, so I neatly made the cable run up behind a drain pipe and in to the roof. Then it was fed to the garage and back down through the ceiling to be attached to the relay switch and then to the 12V transformer inside the NBN cabinet box.
I’m old school so I like soldering joints together and sealing them with heatshrink tubing (and a hair dryer) which has good electrical insulation and protection. So that’s how I went about wiring all the lights. Lights should be wired in a parallel circuit. Below are various pics wiring the garden end of the cable to the various lights.
The garage end of the cable would then have one of the two wires cut, and each end of the cut ends terminated to the relay switch to the COMM and NO (normally open) terminals, and then terminated to the 12V transformer. So when the relay switch is activated, it will close the circuit and power the lights. Do not try and attach 240V to the terminals. This is not safe and I doubt you’ll be a licensed electrician to do this. The 12V transformer theoretically, if it was perfect, should consume no power when there is no load. But these garden light transformers are not perfect, and I measured the drain at around 2.7W, which is probably around $4 of power a year which is not much. Compared to iPhone chargers, they are obviously perfect and consume no power with no drain when unloaded. I measured this. 😉
Magnetic Reed Switch (Garage Door Sensor)
This was fun, and I tested it first on a breadboard with a small commodity reed switch from Jaycar.
I attached the reed switch to the D4 pin of the Spark Core (the relay switches occupy D0 to D3, but note the new relay shield switches occupy D3 to D6). You need some additional work here to get the digital reading spot on. Obviously one of the reed switch wires will attach to ground (GND pin), and the other to the D4 pin. To provide the 3.3V to make that pin read HIGH when the reed switch is closed, you’ll need to wire the 3V3 to D4 through a 10k resistor.
Now when the reed switch is closed (garage door in the closed position), the reading on D4 will be HIGH. When the garage door opens, D4 will read LOW.
Below are photos of the setup. Always make sure you have disconnected all power to the garage door before working in close range to any potentially moving parts. An accident might happen, or someone might come home unexpectedly and activate the roller door. Safety first!
You’ll need a steady hand here, take apart the garage remote and solder the wire ends to the small switch that activates the door. Make sure solder does not spread to the switch casing or anywhere else.
I used “bell wire” from Jaycar for the remote and reed switch wiring. Once all soldered, and with the help of a multimeter confirming the joints are all soldered properly without the solder spreading and joining with other parts, I tied a knot in the wire so any tugs won’t rip the wires out, put back the remote body, and attach it back in the garage.
Complete Spark Core Setup
Here is the complete setup of the Spark Core, affixed to the inside of my NBN cabinet box in the garage with Command large picture hanging strips. Same strips were used for the 12V garden light transformer, and the magnetic reed switch for the garage door sensor. Beats drilling in to steel or walls!
The 12V garden light wire goes through relay switch 1, with the garage remote wire terminated on relay switch 2, and the reed switch soldered to the GND and D4 pins. We are now all set to start programming the Spark Core microcontroller!
Spark Core Microcontroller Firmware
There’s plenty of documentation by Particle, the gist of it all is to create Particle cloud functions you call via a restful API or through IFTTT (If This Then That) applets, as IFTTT fully supports the Particle cloud, including publishing events.
These are the functions I created for the microcontroller. Note the function names can only be a max of 8 characters. Also Postman is a great tool to test these out, or you can use the Particle console web interface.
- Fired from IFTTT using the Weather Underground service on the sunset trigger
- Turns on the front garden lights if sunsetModeActive is true
- Sets isNighttime to true
- Creates an internal event to turn lights off after midnight (ie: if lights turned on at 7:08pm, they will turn off at 12:08am)
- Fired from IFTTT using the Weather Underground service on the sunrise trigger
- Sets isNighttime to false
- Turns off the front garden lights if they were still on
- Fired when the app or IFTTT detects you are approaching home
- If it is night time, and the front garden lights are not on, it turns the front garden lights on for 15 minutes.
- Used by the app to set sunsetModeActive true or false
- Turns the front garden lights on
- Used by the app to toggle the front garden lights on or off
- Turns the garage remote button relay switch on for 1 second, simulating pressing the garage button
- When you leave home, you can set IFTTT to call this function, which then publishes an event to IFTTT to send a push notification to you via the IFTTT app to notify the garage door is open
- Turn a relay (1, 2, 3 or 4) on via the API
- Turn a relay (1, 2, 3 or 4) off via the API
- Check if a relay (1, 2, 3 or 4) is on or off
There is also a cloud variable called currentState which is used by the app to get the state. It just returns a pipe delimited string of "1|0|0:0|CLOSED".
 sunsetMode active or not
 are front garden lights on or off
 if 0:0, there is no event to turn the garden lights off, else it will be something like 0:08 that signifies lights will turn off at 12:08am
 the state of the garage door, open or closed
With all of these functions, you can then solely use IFTTT, or use IFTTT for the Weather Underground service only, and the mobile app for everything else.
Source code for the Particle firmware is available here: https://github.com/michaeldimoudis/my-home-app/tree/master/particle-src knock yourself out playing with it :)
The Mobile App
The final part of this project was the mobile app. It allows you to:
- Set sunset mode active or not
- Toggle the front garden lights
- Know what time the lights will automatically turn off
- Know if the garage door is open or not, and press the garage door remote button
- Via geo location (iOS only), alert you if you left home with the garage door open, or turn the lights on at night when you approach home.
It’s a native app called "My Home App" built in Xamarin Forms and works on iOS and Android. Source code is available here: https://github.com/michaeldimoudis/my-home-app/tree/master/xamarin-app-src
I updated the iOS (not Android) app with geolocation services with a geo fence around my home (and also played with beacons), so I didn’t have to rely on IFTTT to know when I leave or approach home. IFTTT is simple but a bit slower. The iOS app with a geo fence around my home works a treat! I tried beacons, but they are not that reliable for a larger circular region such as a home. The app kept thinking I left and came home when I’m at the backyard or slightly out of range of the beacon etc. There's also a debugging option in the Settings app under "My Home App" so you can test the geo location is firing when it does not have any work do (ie: if you did close the garage door after leaving home).
I would love to get Siri integration, an Apple Watch app, and possibly Apple HomeKit support. Watch the GitHub repo to see if I get these going. Or you can help and submit a PR 😉
All the code is on my GitHub at https://github.com/michaeldimoudis/my-home-app, if you have any questions or more ideas, just add a comment. Also I would love if someone could send a PR for the Android app with the equivalent geo fencing code that's in the iOS app 😉
Here is the final result of the front garden, with 5x 3W in ground garden lights. I actually have 7x more 1W in ground lights still coming from China, to put near the letter box, in the middle of the green shrubs, and at the right separation between those other green shrubs. I've already fed the extra cable for these, so it will make it easier once the lights come.
Even though this is a safe low voltage project, always remember safety first! Especially if you are working near the garage door, turn the power off! And don't play with 240V please
(my next IoT project with a Particle Photon might be a home security system, stay tuned!)