ble-lighthouse: balenaLabs Residency project - build logs

As a part of my balenaLabs residency, I am building a project that will allow me to deploy a fleet of centrally managed (from balenaCloud) bluetooth low energy beacons. I am calling this project :
ble-lighthouse as beacons works similar to lighthouse, putting out signal without waiting for anyone to respond/connect/use etc.

I am planning to create beacons that can broadcast

  • Fixed data (numbers/strings/hex)
  • URLs
  • Sensor Data (temp, humidity etc)
1 Like

I will explain my motivation and intent to do this project as a blog post later and here I’ll just post the build logs and updates as I make progress

First thing first, let’s see how other companies are using ble beacons. A great way I find to do this is download a beacon scanner app and see what’s nearby. I recommend nRF Connect app by Nordic Semiconductor available for android and ios. It’s like a Swiss Army Knife app for ble scanning. A quick scan locally to my surprise shows me a few beacons in my vicinity which i was totally not expecting. It shows Manufacturer is Microsoft and it’s broadcasting some sort of data which I am not aware of and also I cannot figure out this device, it probably belong to someone else around me. But this gave me some idea of how beacon looks like:

2 Likes

**Let’s get to the fun part **

I am choosing debian as my base image and on Linux, bluez is the de-facto utility when it comes to managing bluetooth radios. I’ll prototype on a raspberry pi 4 board because that’s what I have at the moment but I want this project to work on Rpi zero w / w 2 , so we can just throw away some beacons here and there without thinking much about the added cost.

With bluez comes various tools like hcitool, gattool which can be used to create bt peripherals and centrals with GATT profiles, I’ve worked on few before so i know certain bits and pieces but consider reading bluez docs and trying some examples to get a hang of it.

When it comes to creating ble peripherals hcitool is your go-to friend. After reading around I figured that the main characteristics of beacons is they donot connect to ble centrals, hence to achieve that we need to first activate the radio and then put it in non connection mode.

Raspberry pi comes with a ble radio (hci0) which we gonna use, this should work with any other board with balenaOS support ble radio (internal or external)

  • bring the radio up : hcitool hci0 up
  • put the radio in no-connect state hciconfig hci0 leadv 3
  • we also don’t need to scan for other devices so we can turn off the scan feature using hciconfig hci0 noscan

The next thing is mostly to figure out packet structure, it looks like bunch of hex bytes, some of them are functioning flags that some of them contains data type and then actual data.

for ex: bluetooth SIG (special interest group) provides manufacturer ID to it’s partner like Microsoft and ST Microelectronics has their unique ID, using which the scanners can figure out who made these devices/radios. All this can be defined and broadcasted using same hcitool

A typical command might look something like:

hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 30 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5 00 00 00 00 C8

Fortunately, instead of figuring out the hex byte codes for each sort of data, I found a neat little library which does the job. https://github.com/forksociety/PyBeacon , unfortunately it’s not in active development and is quite bloated with features I don’t need for this project, like a beacon scanner, hence I decided to fork and library and modify it accordingly. https://github.com/iayanpahwa/PyBeacon

The library definitely helped with URL and UID broadcast but I still need to figure out way to broadcast RAW data values using which I will send sensor data values of say temperature and humidity sensor .

the hcitool utility from bluez is quite powerful and allows you to define what sort of functionality you want from the radio, for my sensor broadcast part, I’ll be using it directly and maybe once when everything starts working I’ll add that part to my fork of PyBeacon library.

the command for broadcasting as beacon is sudo hcitool -i hci0 cmd 0x08 0x0008 followed by certain structures called AD structures, will require 2 for this purpose, 1 for configuration which shall include manufacturer ID (from SIG), the other AD structure includes UID, the data following it and Tx power.