BLE presence detection
BLE presence detection
I wanted my home automation system to detect when I arrive home so as to unlock my door automatically or let me know if I leave the apartment without turning the A/C off. I figured my smartphone could be used to achieve this since it is equipped with a vast array of sensors and connectivity options. For example, I could use an app like OwnTracks which would send an MQTT message once I enter a certain perimeter around my apartment. However, constantly monitoring one's GPS location is wasteful in terms of battery life. Another option would be to ping my phone's IP address to check if it is connected to the WiFi, but smartphones can sometimes take a while to connect. So, I decided to use BLE advertising, which does not use too much battery while still remaining constantly active.
To detect my phone's BLE advertisings, I used an ESP32 for its built-in BLE capabilities. The ESP32's code is fairly simple: it listens for BLE advertising and gets their UUIDs. If the UUID matches that of my phone, it sends an MQTT message to the home automation system to tell it I'm home. The main challenge here was that the ESP32 cannot scan for BLE advertising and be connected to the WiFi at the same time so I had to make it connect and disconnect accordingly. The code is available on GitHub.
To send BLE advertising, I made a simple android application. To make sure that the advertising would continue even when the phone is in sleep mode, I made the advertising as a service. I have almost no experience with mobile development so the application is probably riddled with bugs. The code is probably not up to date with current Android versions, but is nevertheless available on GitHub.