Intro: Cat Door
This simple project that can track a cat's in-and-out activity using an off-the-shelf cat door, Kinoma Element, a basic LED, and two sets of magnetic reed sensors. The optional mobile companion app, also built with KinomaJS, tells you where your cat currently is and keeps track of how much time your cat spends outside.
Step 1: Cat Door Design
The magnetic switch set contains a reed switch piece and a magnet piece. When the magnet piece comes within 20mm of the reed switch piece, the circuit will be completed and the movement is captured.
We need to use two sets of magnetic switches to capture the movement of the door flap in each direction. This makes it easy to trace the direction the door flap swings and therefore determine whether the cat has gone in or out.
The two reed switch pieces are installed on the frame. They should be above the flap and on opposite sides. The magnet pieces are attached to the flap, also on opposite sides. When the flap swings up, they need to be close enough to trigger the reed switch. It may take a few test runs to figure out exactly where they should be.
Step 2: Wiring
The reed switch needs a pull-down resistor (100 ohm) connected to ground. See image 1 for a wiring diagram.
We add an LED to show the current status of the cat. When the cat goes out of the house, the LED will be turned on. When it returns, the LED will be turned off. The LED also needs a pull-down resistor.
Image 3 above shows the wiring we used. We soldered the 1x8 header onto a small piece of protoboard that fits inside the door and connects to both magnetic reed switches and the LED.
Before putting everything together. We need to drill a hole for the LED light and cut an opening for the headers on the door. The suggested location of both is highlighted in pink in image 4 above.
The last 3 pictures above may be used as reference to complete the sensor installation.
Step 3: Kinoma Element App
The Kinoma Element app consists of just two files:
- main.js: the application file
- catdoor.js: the BLL that interacts with the magnetic reed sensors and LED
Most of the code in main.js is just configuring the pins using the Pins module. Image 1 from above is a screenshot of the code that specifies that we want to use the BLL catdoor.js and all the pins it uses.
Once configured, we call Pins.share, which will allow other devices to discover this application and make remote calls to the BLL. See image 2 above.
The next line calls the updateStatus function in catdoor.js every 100ms. See image 3 above.
The BLL keeps track of the cat's location in a variable called currState. The updateStatusfunction checks if either switch is high, then changes the currState variable and toggles the light on/off accordingly. See image 4 above.
Step 4: Companion Mobile App
Much of the companion mobile app code is defining the UI. See the KinomaJS tutorials for in-depth explanations of building user interfaces using the KinomaJS containment hierarchy.
The onLaunch function of the application's behavior contains the code that discovers the Kinoma Element application and calls the checkCatStatus function of the BLL. This function returns 1 if the cat last came in and 0 if the cat last went out. See the image above for a screenshot of the code.
Step 5: Finish
Once you've wired everything up, run the catdoor-element sample application on your Kinoma Element from Kinoma Code. The light should toggle on and off when you flip the door back and forth. The catdoor-mobile app can be packaged for iOS or Android devices by following the instructions here.
If you got stuck along with way, we’re here to help — come visit us at our forum and let us know how we can help.