One of the major reasons for utilizing an Azure Iot Edge Gateway is to connect devices to the internet that can’t directly establish a connection themselves. A very common case are Bluetooth beacons, that can provide sensor data through a Bluetooth Low Energy (BLE) connection but are not able to send this information directly to an Azure IoT Hub.
For this scenario a bridge or gateway is required to create an IoT message from the Bluetooth payload. This is called Protocol Translation and can be achieved by creating a custom IoT Edge Module. Interestingly enough Microsoft has still not managed to provide a sample or best practice on how to do this with the IoT Edge Runtime V2, so here’s how we did it in our latest IoT project.
In our case we are using the Open Source Ruuvi Bluetooth Tags since they are robust, affordable and there are already a number of SDKs available for different programming languages. For the Gateway we are using the Dell Edge Gateway 3001 running Ubuntu Server 18.04.
To get Bluetooth working inside a IoT Edge Module there are some pitfalls to be aware of that I will be highlighting throughout this article. The full source code can be found on GitHub. Let’s start with the necessary container configurations to be able to access the devices. After creating a new module, you need to adjust the deployment settings so the docker container runs in the host network.
We chose to build the module using NodeJs & the node-ruuvitag package. This way we don’t need to handle any of the BLE-specific communication & protocol translations ourselves but instead get easy-to-work-with JSON messages whenever a nearby tag sends an update.
Most of the code is just the generated Node boilerplate code that the IoT Edge SDK provides when generating a new module. After the module client initialization, we listen for new Bluetooth tags. Once found, we subscribe to the messages sent by them. Those get wrapped in a new Message object and passed to the IoT Edge Runtime through the defined output for delivery to the Azure IoT Hub.
The only thing left now to do now is to make sure you’re running the app in a properly configured Docker Image with all the Bluetooth tools installed that are required by node-ruuvitag or rather the underlying noble package. I mentioned that we’re using a Dell Gateway with Ubuntu, meaning I have adjusted the default Dockerfile.amd64, but the same setup also works for example on a Raspberry Pi with an ARM base image.
Besides installing the required dependencies note that the node process is being started as root to make sure there are no permission issues when calling the Bluetooth APIs.
If you want to wrap each Ruuvi Tag into their own Azure IoT Hub device identity you can find a post on Identity Translation here:
Again, the full code can be found over at GitHub. Hope this post helped you getting started working with Bluetooth on Azure IoT Edge.