Swap Your Old-Fashioned Keychain for a Location-Aware App

In his last post, Mateusz presented our first step to making our office a little bit smarter. We developed and implemented a solution that allows us to open the front gate without leaving our desks, simply by pressing button in a desktop app. After simplifying the process of admitting guests to the premises, we decided that it’s high time to focus on making our own arrival at the office more convenient and geeky. Without further ado, let me introduce the Gate Key project.

raspbeacon5

Non-motorized folks can access our office through the front gate that opens from the outside using an old-fashioned key. We often see our co-workers standing outside the gate, searching through their pockets and bags in search of the elusive little piece of metal. The situation gets even more awkward when you’re commuting to the office by bike (and we have a strong cycling club here at Macoscope 🚲) or carrying some bulky packages in your hands. We felt that this current state of affairs irritates our technically-inclined selves so we started thinking about amending the situation. We asked ourselves, “What would be a perfect solution to our problem?” Well, the answer was simple: the gate should open automatically when an authorized person approaches it.

Smart Home iOS Application

Thanks to the hard work of Mateusz, we now had the possibility to open the front gate’s electric using any device connected to network. That allowed us to think about a mobile application to help us with our problem. Calling a remote service is not even anywhere close to rocket science, a more ambitious task would be forcing the app to do it automatically, only in the right exact moment it’s needed, without any interaction from the user. The app should just sit in your pocket and do the job for you.

Gate_Key_Springboard

The app needs to detect that we are within about 2 meters from the gate. Additionally, the check should be performed even when the app is not running or even when it was killed by the system and we wouldn’t want the process to drain the device’s battery too much. All in all, this looks like a special task for location services.

Say Hello to iBeacon

We needed our measurements to be accurate to within 1 meter, so we turned to iBeacon location technology. Transmitters that work with this standard broadcast a set of information identifying them via Bluetooth Low Energy wireless communication channels. A beacon (or beacons) can define regions that can be tracked by iOS devices via location services. Notably, iOS takes care of beacon region monitoring even if the app itself not alive and, as stated in the documentation, it wakes it up if need be:

Regions associated with your app are tracked at all times, including when the app isn’t running. If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event.

Prototyping a Location-Aware App

Tempted with this impressive list of capabilities, we set out to obtain a representative of the iBeacon species. We chose a custom device based on Nordic Semiconductor‘s BLE chip, but any model available on the market should do (Estimote is probably the most recognizable manufacturer). After furnishing it with a little bit of protection from the elements, the beacon began its duty on the inner side of our fence, right next to the front gate.

The first version of the Gate Key app we developed was based on responding to beacon region entry notifications. When notified about such an event, the app opened the gate by communicating with the Raspberry Pi via a dedicated API. When the app was ready, we started testing it in real-life scenarios. The testing process was accompanied by efforts to calibrate and tweak the beacon’s parameters using a configuration app provided by the manufacturer.

As is often the case, bringing our seemingly straightforward idea to life turned out to be much more complicated than we initially thought. There was so much variance in both distance from the beacon and time spent within its range that the system required to send an event to the app registered for beacon region notification, that we were unable to utilize either of them as a trigger for sending a request to opening the gate. Neither manipulating the beacon’s transmitting power nor the frequency of advertising data brought us any satisfactory results. The app was opening the electric lock in an almost random manner: sometimes far too early, other times the lock clicked only after we’ve spent over a dozen of seconds in front of the gate. This was not the experience we expected from the entire setup.

Enhancing the App with Geofencing

In a manner identical to registration for beacon region events, apps can also be notified about entry/exit into specific geographical regions, that is circles of a specified radius around a known point (a radius of 1-400m works best). Adding monitoring for a region centered directly in front of the gate to our headquarters, allowed the app to be woken up when the device it’s running on finds itself a couple of dozens meters from the gate. This is the proper moment to start beacon ranging.

Ranging is the process of actively scanning for beacons. While ranging, location services will let the app know about devices discovered within range with a 1s refresh period. With each update event, the app receives information about received signal strength (rssi), relative distance to the beacon (proximity), and its accuracy.

After investing some time to record and analyze data we collected during field tests, we were able to determine the range of values that, with a high degree of probability, let the device assume that it is somewhere in the vicinity of the entrance. Armed with this new knowledge, we could now get the app to send out a request to open the gate at the correct moment 😎. After completing its task, the app will stop its background activity until the next region entry event.

Conclusion

We managed to develop an elegant solution that simplifies access to our office. Now, our building is little bit smarter – it knows when someone stands in front of the gate and if that someone has proper authorization, the building can now open the gate for them. The development of the Gate Key app was fun and also a source of some very illuminating lessons:

  • do not use region entry/exit event for timing-sensitive tasks – the moment when your app receives them is highly unpredictable,
  • when utilizing Bluetooth connection parameters for your app’s logic, reserve some time for field tests and tweaking border values.

Related Resources

Location and Maps Programming Guide: Region Monitoring and iBeacon

Getting Started with iBeacon


Got inspired? E-mail us and we’ll get in touch to find out how our design and development services can drive business value for you.

Automated UI Testing with Jenkins CI
Drawing a GCB Watch Face with Android Wear