Sometimes two good ideas can come together to make one great solution. The concept of this project is to build a doggy door that can be controlled with a magnetic lock. This magnetic lock would be controlled either by the home owner or by the pet using a GPS collar. This allows the doggy door to function as intended, while increasing security and allowing you to track your pet if the need arises.
The Magnetic Lock
For this application, the best solution for a locking mechanism was found to be a magnetic lock. It is easily controlled by switching devices, portable, and affordable. Using the particle photon as a controlling device also allows you to avoid costly control mechanisms. The AGPtek Magnetic Lock was the most affordable solution while offering 130lbs of holding force, more than enough for a doggy door.
This is everything that comes included with the magnetic lock device.
You will need a 12V power supply and a relay to allow the particle to control the lock. We used a bank of 8 AA batteries as our power source due to its portability, but you can also just use a traditional power supply plugged into a nearby receptacle.
We begin by connecting the relay to the power and ground pins. We will also designate one pin(D7 here) to control the lock.
Now you will connect the power supply to the magnetic lock.
You can now test the magnetic lock to verify it functions by connecting the two wires together. If everything is correct, the lock will activate and you will not be able to separate the base.
The final step is to connect the magnetic lock assembly with the relay.
Using this code and Mobicle.io, you can now control your lock from any internet connected device anywhere in the world.
int pin = D7;
void setup()
{
pinMode(pin, OUTPUT);
Spark.function("door",lockToggle);
digitalWrite(pin, LOW);
}
int lockToggle(String command) {
if (command=="timedopen") {
digitalWrite(pin,HIGH);
delay(5000);
digitalWrite(pin,LOW);
return 1;
}
else if (command=="open") {
digitalWrite(pin,HIGH);
return 1;
}
else if (command=="close") {
digitalWrite(pin,LOW);
return 0;
}
else {
return -1;
}
}
As you can see, we included three arguments. Close closes the lock, open opens the lock, and the command timedopen will open the lock for 5 seconds and then automatically close it. This will be important for the GPS collar activation.
The electron asset tracker kit can be used for a wide range of applications. It has an on-board accelerometer and includes most of the components needed for a general project.
Since the documentation for assembly is a little sparse, here's a step by step assembly guide.
You are able to use your own sim card but you will need to make some changes to the APN settings. Review the documentation for further instructions.
The GSM antenna is connected to the gold port above. It is a little hard to insert and the cable is fragile, so be careful.
This is the basic setup for an electron. You can now flash the device over serial(preferred) and charge the battery using the Micro USB port.
This picture also shows the addition of an external antenna connection. For this application, and most others, an external antenna will be required because the included antenna only works in the best of conditions.
If more attention was made to the length of the external antenna cable, a much smaller and less obtrusive enclosure could be used.
The code used on the electron is the library available to test the asset tracker. We did not need to add additional features to the code as location was our only goal.
/* -----------------------------------------------------------
This example shows a lot of different features. As configured here
it'll check for a good GPS fix every 10 minutes and publish that data
if there is one. If not, it'll save you data by staying quiet. It also
registers 3 Particle.functions for changing whether it publishes,
reading the battery level, and manually requesting a GPS reading.
---------------------------------------------------------------*/
// Getting the library
#include "AssetTracker/AssetTracker.h"
// Set whether you want the device to publish data to the internet by default here.
// 1 will Particle.publish AND Serial.print, 0 will just Serial.print
// Extremely useful for saving data while developing close enough to have a cable plugged in.
// You can also change this remotely using the Particle.function "tmode" defined in setup()
int transmittingData = 1;
// Used to keep track of the last time we published data
long lastPublish = 0;
// How many minutes between publishes? 10+ recommended for long-time continuous publishing!
int delayMinutes = 1;
// Creating an AssetTracker named 't' for us to reference
AssetTracker t = AssetTracker();
// A FuelGauge named 'fuel' for checking on the battery state
FuelGauge fuel;
// setup() and loop() are both required. setup() runs once when the device starts
// and is used for registering functions and variables and initializing things
void setup() {
// Sets up all the necessary AssetTracker bits
t.begin();
// Enable the GPS module. Defaults to off to save power.
// Takes 1.5s or so because of delays.
t.gpsOn();
// Opens up a Serial port so you can listen over USB
Serial.begin(9600);
// These three functions are useful for remote diagnostics. Read more below.
Particle.function("tmode", transmitMode);
Particle.function("batt", batteryStatus);
Particle.function("gps", gpsPublish);
}
// loop() runs continuously
void loop() {
// You'll need to run this every loop to capture the GPS output
t.updateGPS();
// if the current time - the last time we published is greater than your set delay...
if(millis()-lastPublish > delayMinutes*60*1000){
// Remember when we published
lastPublish = millis();
//String pubAccel = String::format("%d,%d,%d",t.readX(),t.readY(),t.readZ());
//Serial.println(pubAccel);
//Particle.publish("A", pubAccel, 60, PRIVATE);
// Dumps the full NMEA sentence to serial in case you're curious
Serial.println(t.preNMEA());
// GPS requires a "fix" on the satellites to give good data,
// so we should only publish data if there's a fix
if(t.gpsFix()){
// Only publish if we're in transmittingData mode 1;
if(transmittingData){
// Short publish names save data!
Particle.publish("G", t.readLatLon(), 60, PRIVATE);
}
// but always report the data over serial for local development
Serial.println(t.readLatLon());
}
}
}
// Allows you to remotely change whether a device is publishing to the cloud
// or is only reporting data over Serial. Saves data when using only Serial!
// Change the default at the top of the code.
int transmitMode(String command){
transmittingData = atoi(command);
return 1;
}
// Actively ask for a GPS reading if you're impatient. Only publishes if there's
// a GPS fix, otherwise returns '0'
int gpsPublish(String command){
if(t.gpsFix()){
Particle.publish("G", t.readLatLon(), 60, PRIVATE);
// uncomment next line if you want a manual publish to reset delay counter
// lastPublish = millis();
return 1;
}
else { return 0; }
}
// Lets you remotely check the battery status by calling the function "batt"
// Triggers a publish with the info (so subscribe or watch the dashboard)
// and also returns a '1' if there's >10% battery left and a '0' if below
int batteryStatus(String command){
// Publish the battery voltage and percentage of battery remaining
// if you want to be really efficient, just report one of these
// the String::format("%f.2") part gives us a string to publish,
// but with only 2 decimal points to save space
Particle.publish("B",
"v:" + String::format("%.2f",fuel.getVCell()) +
",c:" + String::format("%.2f",fuel.getSoC()),
60, PRIVATE
);
// if there's more than 10% of the battery left, then return 1
if(fuel.getSoC()>10){ return 1;}
// if you're running out of battery, return 0
else { return 0;}
}
For the GPS to report a usable value, that is not in the Indian Ocean, you will need to place the device outside and allow it to calibrate. The GPS Fix light will blink red until it is able to connect to a satellite.
Once that occurs, the device will publish a command G, with the current latitude and longitude of the device.
The is a data cost for using the GSM network on the electron. There are many ways to reduce data usage in the documentation. Also, one could use a different data provider for any high data usage scenarios.
The project goal was to use the specific location of the door to trigger the magnetic lock. So precision and accuracy of the location device was required. In order to test the accuracy of the device and to monitor and log device location, Havoc Studio's asset tracker website was used.
The source code and author of this website can be found here. You can either just use the site the author provided, or run the source code on your own web server. For this project, the team just used Havoc Studio's website. All you need to do is log in and as your device reports data, information will propagate on the map.
Here you can see that the GPS was able to locate my car on campus, and if you zoomed in, could see the exact parking spot my car was located. Obviously the accuracy and precision of the device will allow us to reliably trigger the door mechanism and allow the location of your pet at any time.
The door in this project was used primarily to display the GPS collar and Magnetic Lock were able to communicate. Any users that are interested in replicating this project can use a wide variety of door options with very similar results.
Due to a limited budget, we built our own doggie door from a bedroom door. The door was purchased for $10.00 at the local Habitat Restore. Typically doggy doors are built into exterior doors, but we decided to illustrate our concept on an interior door due to a limited budget. The first step after purchasing the door was to cut out the actual doggy door hole, which was carefully done with a skill saw.
The next step in building our DIY doggy door was to build the door flap. We found the easiest way to do this was to simply use the exact piece of the door that was cut out. This would guarantee a very tight fit, with a small 1/8" gap (thickness of saw blade) on all sides. To make the door slightly more aesthetically pleasing, we framed it out using 1" X 1/4" pieces of red oak that was leftover from another project.
The next step was to figure out a way to incorporate the magnetic lock into the door so that it would function as we desired. We decided to mount the magnet base onto the door and the actual magnet onto the flap.
Our next step was hinging the door so that it could rotate freely in both directions when the magnetic lock was not engaged. A simple bi-fold door hinge was used. The door also had to swing close enough for the magnetic lock to hold when engaged, which we found to be quite difficult.
Once we got the door to swing perfectly and the magnetic lock to function properly, the door was done!! Below are pictures of both the front and back of the finished product. The backside of the door flap was left off in order to illustrate it's inner workings.
As stated in the beginning this is built into an interior door, although the same process could be done to an exterior door just as easily.
Electron/Photon CommunicationThe intermediary used to allow communication in this project between devices was, IFTTT. This choice was made due the excessively simple nature of IFTTT.
IFTTT allows you to make applets that can activate devices based on action occurring on another device.
Our first objective is to create a log on Google Drive that would give us a record of the location data for the pet. This would allow us to view the data in a tabulated form and allow us to track the pet if the GPS signal was lost or the battery ran out, by looking at the last location update.
Now we will create the applet that will control the doggy door. The applet will monitor the published event G, and if the location reported for that instant is equal to the location of the doggy door(that we determine ahead of time), the door will open for five seconds and automatically re-lock.
Your setup process for the GPS activated locking doggy door is now complete.
Lessons Learned
1. Time Delay
There are delays that are introduced in our implementation of a GPS activated doggy door. Due to data limitations, we are forced to limit GPS refreshes to once every minute. Then the IFTTT trigger takes an additional amount of time to activate. This leads to many problems with pets that do not wait at the door until it is opened. If the dog moves around, it runs a risk of not activating the door. If the dog is impatient, this happens:
This delay can be reduced significantly by using publish and subscribe between the two devices, but in our opinion, the delay cannot be diminished enough for a pet to use our solution without training. More traditional designs using RFID might be a smarter solution looking forward.
2. Magnetic Lock Clearance
The model that was chosen for our lock assembly required the armature plate to be within 1/16th of an inch in order to activate. In more traditional door designs, this does not cause any problems. As seen here:
The door in this image will naturally come in contact with the electromagnet and will require very little adjustment.
Our door flap would need to move freely in both directions and still come within 1/16th of an inch of the electromagnet to activate properly. This took a lot of trial and error to get right. There is an option to remove a button on the bottom of the armature plate to alleviate some of the clearance problems.
Comments