The rules and other information can be found at ODTUG GeekAThon 2017.
Problem
My son Alex attends a school where the students have some ‘bonus features’, or as the school puts it: “Educating Exceptional People”. There are some students at Alex’s school who sometimes try to wander away. Obviously, this could be a problem but the school staff is extremely well trained and they keep a close watchful eye on all of the students. Still, I’d like to try and make their lives a little easier, and the students a little safer.
There are commercial systems available that could notify the administration and/or lock doors when a beacon worn by a student is detected in a hazardous zone, such as leaving the school. That sounds perfect. There’s just one problem: those systems can be very expensive.
Proposal
Implement a student tracking and door lock automation system that can operate on inexpensive components and open source the software. I will set up a test environment at my house and my son will test it with me.
Desired Features
- Central to the whole system is a way to detect a beacon when it enters a specific area such as near an exit door or a faculty-only area.
- Ability to send notifications.
- Ability to trigger a physical event such as a door lock or audible alert.
- Log beacon detection events in a database.
- Beacon Id.
- Distance from the scanner.
- Timestamp.
- Affordable components.
Initial Idea
After browsing the web for a while I decided I would set up multiple scanners with overlapping zones then use trilateration (I like saying that word) to determine the position of the beacon.
I would set up multiple scanners, measure the distances between them and plug that data into my database. When a scanner detects a beacon it would use my ORDS service to POST its own id, the beacon id and the calculated distance to the beacon. On the database, I would use Oracle Spatial queries to determine the location of the beacon. Finally, I would compare the beacon location to defined zones in my house and trigger the alerts/actions for the zones.
I have a tendency to over-engineer my projects. I once built a doghouse that weighed close to 200 lbs. (It was awesome.)
After getting most of this working, I realized that I could achieve the project goals by simply placing a single scanner near each zone and let that scanner initiate the alert actions for its zone. Sometimes less is more.
Hardware
I already had a bunch of Raspberry Pi so I decided to use a couple of my Pi 3s. Since I’m always looking for an excuse to buy more toys, I decided to get a Pi Zero W.
I have a z-wave enabled deadbolt and a Z-Stick USB hub that I can control using Home-Assistant.io. For the audio notification, I’ll push a ‘text to speech’ action to my Sonos speaker. I can make the Sonos say anything I want, this entertains me a lot, my family… not so much.
Software
- Raspbian Linux
- Linux modules
- bluetooth
- bluez
- libbluetooth-dev
- libudev-dev
- Linux modules
- NodeJS
- NodeJS Modules
- bleacon
- request
- NodeJS Modules
- IFTTT.com
- Home-Assistant.io
- Oracle Database
- Oracle Rest Data Services (ORDS)
The installation instructions are in the GitHub repo.
Database
The beacons are set to transmit every two seconds and can be detected by multiple sensors. I always like to keep track of my data so of course, I’m pushing it to a database. I’m using an Oracle Cloud Database with an ORDS (Oracle Rest Data Services) front end to collect the data. When a Raspberry Pi detects a beacon, it will calculate the distance then POST the data to the database. The database will automatically record a time-stamp when the record is inserted.
1 2 3 |
{"scannerId": scannerID, //The ide for this scanner "beaconId": beaconId, //The detected beacon's ID "distance": distance} //Calculated distance between the beacon and scanner |
This is included in the current code and it’s what I need to collect the data for the “Initial Idea” section above.
If I decide to implement the feature to track the beacon’s position throughout my house. I just need to determine the fixed position of each scanner relative to a point in my house and using the data I’m already collecting, run an Oracle Spatial query that defines a circle from each scanner with a radius of the distance to the beacon. Where the circles overlap is the approximate location of the beacon. The official term (linked above) is Trilateration, but you can think of it as a Venn Diagram.
How I Deployed the System
If you’d rather, you can watch the video and skip this section.
I configured and positioned three Raspberry Pi through my house. I put a Pi 3 in the hallway outside of the bedrooms, a Pi 3 near the front door and a Pi Zero W outside on the front porch.
- The first Raspberry Pi 3 in the hallway is set to trigger an alert when the beacon is approximately 2 meters away. This alert will send a notification through IFTTT* to the app on my phone.
- The Raspberry Pi 3 near the front door fires an alert when the beacon is approximately 1 meter away.
This alert has three actions:- Send the ‘lock’ command to the deadbolt through the REST interface of Home-Assistant.io using Z-Wave. Home-Assistant.io and the Z-Wave USB dongle are also installed on this Pi.
- Set the Sonos volume to max and send ‘Locking the front door’ to the Sonos speaker using the text to speech function in Home-Assistant.io.
- Send a notification through IFTTT to my cell phone.
- The Raspberry Pi Zero W outside near the front door will trigger an alert when the beacon is approximately 1 meter away.
This alert has three actions:- Send the ‘unlock’ command to the deadbolt through the REST interface of Home-Assistant.io using Z-Wave.
(If Alex makes it outside, I want the door unlocked so he can come back in.) - Set the Sonos volume to max and send ‘Unlocking the front door’ to the Sonos speaker using the text to speech function in Home-Assistant.io.
- Send a notification through IFTTT to my cell phone.
- Send the ‘unlock’ command to the deadbolt through the REST interface of Home-Assistant.io using Z-Wave.
*IFTTT can also send a text message but the free tier only allows a limited number of texts to be sent each month. I chose to use notifications through their Android app since they are unlimited and I would have burned through the text quota the first time I forgot to limit how often I send a notification. In a live situation, it could send out multiple texts.
Challenges
I had intended to use OpenHab for the home automation features of the project, but when I built the project there was a bug in the Z-Wave addon that made interacting with the deadbolt more difficult. I tried out Home-Assistant.io and so far I really like it. Each application has its own strengths and weaknesses, but they both run on a Raspberry Pi so I may use both for future projects. I’d like to mention they are both open source which is an added bonus.
The beacon distance tracking is not as accurate as I hoped, but it’s fine for this project. The signal can be degraded by walls, bodies or other objects being between the beacon and scanner. To improve the accuracy, I implemented a weighted rolling average function as part of the distance calculation to smooth out some of the spikes. Deploying more scanners would also greatly improve the accuracy if I implement the position tracking.
Future Improvements
- Add an Oracle JET front end for configuration and control of the system.
- Add a map display that can show the beacons live.
- Change the Alert/Action code to be more generic and provide a mechanism to define them in the front end.
- Find a small inexpensive wearable BLE beacon or design one with a small rechargeable battery and a 3D printed enclosure.
Final Thoughts
If you decided not to participate in the GeekAThon this year please join in next year. It is a great way to learn some new skills and have fun at the same time. I am sure parts of what I described above sound intimidating. But if you’d like to try your hand at this or similar projects, don’t hesitate to contact me for help. And while I can’t speak for the other GeekAThon participants, this year or past years, I am certain they will be eager to help you, too.
This project has been a lot of fun, I learned a lot. I’m looking forward to next year!