So, this is my first post.
I originally got the spark because I wanted to control my garage door. I had been doing it with a Raspberry Pi, but that thing was a power hog. Plus, with the cloud-aspect of the core, things got a lot easier!
I saw the post here:
Which is a great start! But, I ran into a few problems with just using Tinker.
I’d like to share those with you guys. Suggestions are of course welcome!
If you don’t want to read about the process I had, just scroll down to the end to find the schematic, spark code and html/css/js code.
Problem 1)
One of the biggest issues is that depending on how you set up your relay (Normally open / normally closed), the garage door will activate when the core either powers on, or powers off. This is a pretty major security issue if it were to open on a power off situation (say your core dies, or there’s a glitch or something – obviously if power to the house goes out, then it’s not a huge problem because the garage door won’t be powered either :)) . If the garage activates when the core is powered on, well, then it’ll be difficult to ever update your code (assuming you move away from just using Tinker).
Solution 1)
It’d be pretty great if the core would allow you to load code RIGHT at power on, but unfortunately (at least from what I’ve found), it first must connect to the internet before it loads anything. Therefore, you can’t just set your relay HIGH/LOW (depending on how you set it up) at the start so that it doesn’t activate on power on.
So, what I did was use TWO relays that work opposite of each other and bridged them. When the core powers on, the relays are set LOW by default (which is a normally open position). So, what I did was set my first relay to be normally closed and my second relay to be normally open. So, what happens is that the relay chain has an open circuit when both relays are set LOW (which is default on power on). But, when I set the first relay HIGH, it switches from normally open, to closed and thus, completes the circuit, and sends the pulse to the garage door.
This way, no matter what the power situation of the core is, the garage door will never get a pulse unless you WANT it to get one.
Problem 2)
I wanted to be able to monitor whether or not my door was open or closed remotely!
Solution 2)
Reed switch. I put a reed switch on D6, set it to INPUT (which sets the pin HIGH, outputting the 3.3 v). Therefore, I used a normally open switch connected between D6 and GND. I have it set to where if the door is closed, the reed switch is NOT with it’s pair, so therefore, the D6 remains HIGH. When the door is open, the reed pairs meet up, the reed switch becomes “CLOSED” and sends D6 to GND, which then returns a “LOW”.
Problem 3)
As I stated in Problem 1 above, when relay 1 goes HIGH, it completes the circuit to the garage door and simulates pressing the button of the garage door. The issue is, when you press the button, you don’t press and HOLD it, you would press it and then release it!
Solution 3)
So, I wanted to simulate that. Therefore, I wrote a simple function for my core that would send relay 1 HIGH for a second, then bring it back LOW.
int relayPulse(String command){
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay, LOW);
return 1;
}
Problem 4)
This brings me to the last problem I faced. Because I was no longer just using Tinker, I had to be able to control the core outside of the phone app. I wanted to be able to remotely open and close it AND see if it was open or closed!
Solution 4)
Make a web app! Because the core is on the cloud and has API access, this wasn’t too bad to implement.
I had to implement another function in order to check the status of this door by seeing if the reed switch (problem 2) was HIGH or LOW. I threw in there a “neither HIGH nor LOW” which would indicate a disconnected core!
int statusCheck(String command){
int status = digitalRead(reed);
if(status == HIGH){
return 1;
}
else if(status == LOW){
return 0;
}
else{
return -1;
}
}
As for the webapp. I used a hosting site called https://www.bitballoon.com . It’s free, has the ability to make a site LOCKED! via a password you choose (which is nice so you don’t have anyone else opening or closing your garage door ) and is drag and drop! So, you could just edit the “JS” code that I supplied, and drag that folder into bitballoon and you’re done!
The JS does a push request by calling the function “relayPulse” when you push the button on the webapp. It then does a another push request every X seconds (defined in the variable ‘status_check_time’) that runs the function “statusCheck” and returns a 1 if the garage is closed, a 0 if it’s open and a -1 if there is no response (indicating a disconnect). The webpage automatically updates every X seconds and displays the appropriate status on the page. You can also refresh, if you get impatient.
PROJECT FILES
All right! Sorry for the long post, but that’s it. Here are the files of my project:
-
Schematic:
In this case, the switch on the left is the reed, and the button on the right is the garage door button (or you can wire it right up to the garage opener itself. Just make sure to uplug everything before working with these wires!!)
I didn’t have a good relay image to use in fritzing, but here is how I wired up in case the image above isn’t descriptive enough:
- Core Code
I have relay1 set to D0 and the second relay on D3, but you can do whatever you want. Just remember that D2 can’t handle more than 3.3v, not that that SHOULD be an issue here. I just avoided it to be safe.
The relay is on pin D6, but again, do whatever you want.
int relay = D0;
int delayRelay = D3;
int reed = D6;
int relayPulse(String command){
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay, LOW);
return 1;
}
int statusCheck(String command){
int status = digitalRead(reed);
if(status == HIGH){
return 1;
}
else if(status == LOW){
return 0;
}
else{
return -1;
}
}
void setup() {
Spark.function("relay", relayPulse);
Spark.function("status", statusCheck);
pinMode(relay, OUTPUT);
pinMode(delayRelay, OUTPUT);
pinMode(reed, INPUT);
digitalWrite(relay, LOW);
digitalWrite(delayRelay, LOW);
}
void loop() {
}
-
Webapp Code
http://codepen.io/austinpe/pen/Frhxd
Github page: