This guide should help you to setup an iBeacon scanner using a Python script as external service. I have used the following "stuff":
- A RPi3B+
- SD-card, complete fresh Raspbian Buster with desktop and recommended software
- a Python script written to handle the actual scanning
- latest Node-RED
- the RPi3B+ built-in BLE device
First of all, see to that you start with a fresh Buster setup as above. Other combinations including older versions of raspbian, other rpi models is possible to use but you might run into a situation where more dependencies needs to be resolved. Nothing impossible but,,,more time consuming
Check also that Bluetooth is turned on !!!
Assuming you have reached the state that the newly installed Buster image is properly updated and configured according to the initial startup, we can go ahead right away (I simply named my host "gattscanner"). Open up a cmd prompt, LXTerminal, and assuming we are now in /home/pi:
Check the installed version of Python3 (I have 3.7.3, thats fine)
pi@gattscanner:~ $ python3 --version
Python 3.7.3
Check the installed version of Bluez:
pi@gattscanner:~ $ bluetoothctl -v
bluetoothctl: 5.50
Check the installed version of PIP3:
pi@gattscanner:~ $ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
So far it looks good and we are ready to install a number of needed libraries
sudo apt-get install libbluetooth-dev bluez bluez-hcidump libboost-python-dev libboost-thread-dev libglib2.0-dev libboost-all-dev
Now is time to download and install gattlib
pip3 download gattlib
tar xvzf ./gattlib-0.20150805.tar.gz
cd gattlib-0.20150805/
Since the gattlib setup is prepared to be installed under Python 3.4, we need to correct this to the version we are using
sed -ie 's/boost_python-py34/boost_python-py37/' setup.py
We can finally install gattlib (don't forget the dot at the end of the command):
pi@gattscanner:~/gattlib-0.20150805 $ sudo pip3 install .
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Processing /home/pi/gattlib-0.20150805
Building wheels for collected packages: gattlib
Running setup.py bdist_wheel for gattlib ... done
Stored in directory: /root/.cache/pip/wheels/2b/2d/d2/d62bdc868bb470d7b7e2fa672805652c9e57ff271d2ba6a8ea
Successfully built gattlib
Installing collected packages: gattlib
Successfully installed gattlib-0.20150805
Next we change back to /home/pi
cd ..
Install the following Python modules:
sudo pip3 install setproctitle
sudo pip3 install paho-mqtt
sudo apt install -y mosquitto
Copy this Python script to /home/pi (rename/change extension from .txt to .py)
gatt_ibeacon_scan.txt (2.1 KB)
Install/Update Node-RED
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
Configure Node-RED service to start automatically at boot
sudo systemctl enable nodered.service
sudo systemctl start nodered.service
Import this flow and deploy
[{"id":"f1e1879c.db20c8","type":"tab","label":"BLE Gatt Scanner","disabled":false,"info":""},{"id":"15ee910e.06391f","type":"mqtt in","z":"f1e1879c.db20c8","name":"","topic":"found","qos":"0","datatype":"auto","broker":"18e7c688.018489","x":150,"y":360,"wires":[["de6816a8.875538","f0f36562.6daab8","b83fe5a5.6c36b8"]]},{"id":"9fb41c04.1bebd","type":"debug","z":"f1e1879c.db20c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":420,"wires":[]},{"id":"de6816a8.875538","type":"switch","z":"f1e1879c.db20c8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"name: MiniBeacon_10754, address: D9:77:B9:4C:B8:5B","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":360,"wires":[["3c60c68c.05deba","9fb41c04.1bebd"]]},{"id":"f0f36562.6daab8","type":"debug","z":"f1e1879c.db20c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":350,"y":420,"wires":[]},{"id":"8df2f747.4534f8","type":"exec","z":"f1e1879c.db20c8","command":"export DISPLAY=:0 && lxterminal -e sudo python3 /home/pi/gatt_ibeacon_scan.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1020,"y":100,"wires":[[],[],[]]},{"id":"d8a34f4b.6d244","type":"trigger","z":"f1e1879c.db20c8","op1":"","op2":"1","op1type":"nul","op2type":"str","duration":"10","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":570,"y":100,"wires":[["8df2f747.4534f8"]]},{"id":"9e332f97.2746d","type":"inject","z":"f1e1879c.db20c8","name":"Init","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"","x":330,"y":100,"wires":[["d8a34f4b.6d244","bbd896e2.aee998","dce570b.a37ce9"]]},{"id":"47b75403.06780c","type":"comment","z":"f1e1879c.db20c8","name":"Starting BLE scanning service","info":"","x":220,"y":60,"wires":[]},{"id":"8d5ac6aa.304908","type":"mqtt out","z":"f1e1879c.db20c8","name":"","topic":"commands","qos":"0","retain":"","broker":"18e7c688.018489","x":800,"y":160,"wires":[]},{"id":"bbd896e2.aee998","type":"change","z":"f1e1879c.db20c8","name":"abort","rules":[{"t":"set","p":"payload","pt":"msg","to":"abort","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":160,"wires":[["8d5ac6aa.304908"]]},{"id":"334e200c.65883","type":"inject","z":"f1e1879c.db20c8","name":"abort","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":330,"y":220,"wires":[["bbd896e2.aee998","dce570b.a37ce9"]]},{"id":"dce570b.a37ce9","type":"delay","z":"f1e1879c.db20c8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":560,"y":220,"wires":[["6763bcb4.604624"]]},{"id":"6763bcb4.604624","type":"exec","z":"f1e1879c.db20c8","command":"sudo pkill -f gatt_ble_scanner","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":860,"y":220,"wires":[[],[],[]]},{"id":"b83fe5a5.6c36b8","type":"trigger","z":"f1e1879c.db20c8","op1":"","op2":"true","op1type":"nul","op2type":"bool","duration":"30","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":350,"y":300,"wires":[["dce570b.a37ce9","bbd896e2.aee998","d8a34f4b.6d244"]]},{"id":"3c60c68c.05deba","type":"trigger","z":"f1e1879c.db20c8","op1":"MiniBeacon_10754 enter GATT 239","op2":"MiniBeacon_10754 leave GATT 239","op1type":"str","op2type":"str","duration":"90","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":570,"y":360,"wires":[["2547b4a5.f9292c"]]},{"id":"2547b4a5.f9292c","type":"debug","z":"f1e1879c.db20c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":800,"y":360,"wires":[]},{"id":"18e7c688.018489","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
In a few seconds you should see the scanner running in a window on the desktop. Use the debug nodes to view incoming scan results
The script will scan your environment looking for iBeacons. The result is published to the MQTT broker. If you want to terminate the script, use the "abort" button in Node-RED.
Setup filtering, watching the iBeacons you are interested in. The events are there, use them for any cool purposes