This blog post introduces how to create cheap bluetooth beacons with nRF24L01+ wireless modules. nRF24L01+ is a 2.4GHz wireless module from Nordic semiconductors and happens to share a very similar protocol as BLE packets. This similarity in packets along with some tinkering in code can enable these modules to act as a ble beacon advertising a valid packet. This hack is first published at "Bit-Banging" Bluetooth Low Energy - Dmitry Grinberg and from then there has been various ports to make this work on a variety of platforms. But there exists some compromises in packet length and data that can be transmitted. More technical details can be found at Lijun // Using nRF24L01+ as A Bluetooth Low Energy Broadcaster/Beacon. Most exciting thing is these modules are available for a little more than $1 at many outlets making this one of the cheapest way to fake BLE beacons.
In this post I uses an arduino with nRF24 and uses a library I wrote some time ago to fake BLE advertisement packets. I intend to use these in later stage of the project as bluetooth tags attached to objects and BLE presence detection.
Hardware Setup
Hardware setup is fairly string forward. You just have to connect nRF modules as the the way you uses it normally with arduino.
Pin connections are given below:
nRF24L01+ | Arduino (nano) |
---|---|
VCC | 3.3V |
GND | GND |
CSN | D10 |
CE | D09 |
MISO | D12 |
MOSI | D11 |
SCK | D13 |
IRQ | n/a |
My setup looks like this:
Downloading library
I have written a small library to make the procedure easy. You can get it from : https://github.com/v-i-s-h/RF24Beacon
To install the library, go to /<your_arduino_sketch_folder>/libraries and issue:
git clone https://github.com/v-i-s-h/RF24Beacon.git
Programming arduino
I have given an example along with the library. Once you successfully install the library, you will be able to go to File > Examples > RF24Beacon > beacon in your arduino ide to open the example. It looks like:
// RF24 Beacon #include <SPI.h> #include "RF24Beacon.h" RF24Beacon beacon( 9,10 ); uint8_t customData[] = { 0x01, 0x02, 0x03 }; void setup() { beacon.begin(); beacon.setMAC( 0x01, 0x02, 0x03, 0x04, 0x05, 0xF6 ); uint8_t temp = beacon.setName( "myBeacon" ); beacon.setData( customData, 2 ); } void loop() { // beacon.sendData( customData, sizeof(customData) ); beacon.beep(); delay( 1000 ); }
- In line#7, I configure the connection of nRF module. I should be RF24Beacon beacon( pin_CE, pin_CSN );. If you are using an alternate hardware setup, you might like to modify this.
- In line#14, MAC address for the beacon is set. You can skip this, in that case a random MAC (based on the build date of sketch) will be used as MAC address.
- Line#16 sets the name which will shown while scanning. Here comes the first limitation - you can set only a name of length atmost 14 characters. Not that bad, this will be enough for my applications.
- Line#17 sets the custom data that can be send with the packet. And this is the second limitation - Your beacon name+data should not take more than 14 bytes. Here my name is 8 bytes length. So I can pack 6 bytes of data. Here I'm only putting in 2 bytes.
- Line#24 is the function which transmits a BLE packet. Here, I'm sending a packet every 1 second (line#26).
- Line#26 shows how to send a a packet with custom data. This way I will be able to send some sensor reading also in the packet .
Testing with Raspberry Pi
Now it's time to test the hack. Power up your raspberry Pi. I'm using Pi3 here. Alternatively, any older version of Pi with a bluetooth dongle supporting BLE can also be used.
First we need to install a few packages. Some might be already installed in Pi, but l'm giving the full list.
$ sudo apt-get update $ sudo apt-get install screen bluez bluez-tools bluez-hcidump
Now you can use hcitool to listen to advertised packets:
$ sudo hcitool lescan --duplicates
This will list all the available BLE packets around you. There you will be able to see one with name "myBeacon" (or the name you set in line#16). '--duplicates' flag is added ti continuously list the beacon - otherwise hcitool will list it only once.
Next we'll take a look into the raw BLE packet we are receiving at raspberry pi. What we need to do is to start hcitool for scanning and then use hcidump to display the packet. Since I want to run both the command side by side, I'll be using screen utility. You can find more about screen and how to use it here.
So in first screen, start hcitool with:
$ sudo hcitool lescan --duplicates
and in second screen, start hcidump with
$ sudo hcidump -R -X
This is how my output looks like after running these commands:
Now you will be able to build a cheap ble tags which can be monitored by Pi.
Happy hacking,
vish
Top Comments