Using a BLE remote for home automation with RaspberryPi.
- Connect to a given MAC address of a BLE device (with a button)
- Monitor for disconnect or errors and try to connect again
- Listen to clicks being made by the device
- RaspberryPi (tested on pi-3 with 'Raspbian Stretch Lite' install)
- BLE remote โ This script was design to work with a specific BLE device and will probably need modifications to work with other devices.
Here is the result of hcitool leinfo
for the BLE remote I've used:
pi@raspberrypi:~ $ sudo hcitool leinfo <MAC-ADDRESS>
Requesting information ...
Handle: 64 (0x0040)
LMP Version: 4.0 (0x6) LMP Subversion: 0x4103
Manufacturer: Telink Semiconductor Co. Ltd (529)
Features: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
You can check your's after installing 'bluez'
In Terminal execute the following commands:
Replace any occurrence of bluez-5.50
with the newest version you find in bluez.org
sudo wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.50.tar.xz
dpkg --get-selections | grep -v deinstall | grep bluez
tar xvf bluez-5.50.tar.xz
sudo apt-get install libglib2.0-dev libdbus-1-dev libusb-dev libudev-dev libical-dev systemd libreadline-dev
cd bluez-5.50
sudo ./configure --enable-library
sudo make -j8 && sudo make install
sudo cp attrib/gatttool /usr/local/bin/
Find the MAC address by:
- First, running
hcitool lescan
- Then, turn on the device and watch for the last one added.
sudo hciconfig hci0 down; sudo hciconfig hci0 up
sudo hcitool lescan
Exit with
ctrl c
sudo apt-get install git-core
git clone https://github.com/gilhaz/BLE-remote.git
sudo BLE-remote/install.sh
Fill the < $POST_SERVER > and < $DEVICES > as listed in the file
sudo nano BLE-remote/ble_config.conf
In Terminal execute the following commands:
bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
sudo systemctl enable nodered.service
sudo systemctl start nodered.service
npm install node-red-dashboard
Node-Red flow to set HTTP response to POST from BLE-remote script (for use in automation)
- Go to your Node-Red GUI (e.g. localhost:1880)
- Import > Clipboard
- Past the following flow
- Click 'Import'
- Click 'Deploy'
Make sure to copy all the text in the box
[{"id":"d8fb803d.87e658","type":"tab","label":"BLE-remote","disabled":false,"info":""},{"id":"aac8f684.b0a22","type":"http in","z":"d8fb803d.87e658","name":"","url":"/BLE-remote","method":"post","upload":false,"swaggerDoc":"","x":90,"y":160,"wires":[["93c4ad11.7d4358","660362d6.eae364","393ae75d.74d858"]]},{"id":"f872ad39.b958f","type":"debug","z":"d8fb803d.87e658","name":"Clicks","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":140,"wires":[]},{"id":"93c4ad11.7d4358","type":"http response","z":"d8fb803d.87e658","name":"http responce","statusCode":"","headers":{},"x":340,"y":100,"wires":[]},{"id":"238d1384.d32314","type":"change","z":"d8fb803d.87e658","name":"\"Device2\".clicks","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.clicks","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":180,"wires":[["7f1ac3fe.096ccc"]]},{"id":"dd6282b0.10742","type":"change","z":"d8fb803d.87e658","name":"\"Device1\".clicks","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.clicks","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":140,"wires":[["f872ad39.b958f"]]},{"id":"7f1ac3fe.096ccc","type":"debug","z":"d8fb803d.87e658","name":"Clicks","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":180,"wires":[]},{"id":"660362d6.eae364","type":"switch","z":"d8fb803d.87e658","name":"MAC list","property":"payload.mac_address","propertyType":"msg","rules":[{"t":"eq","v":"11:11:11:11:11:11","vt":"str"},{"t":"eq","v":"22:22:22:22:22:22","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":280,"y":160,"wires":[["dd6282b0.10742"],["238d1384.d32314"],["9185a6f7.b36e4"]]},{"id":"393ae75d.74d858","type":"debug","z":"d8fb803d.87e658","name":"full JSON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":320,"y":60,"wires":[]},{"id":"9185a6f7.b36e4","type":"debug","z":"d8fb803d.87e658","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":430,"y":220,"wires":[]}]
In Node-Red:
Change the 'MAC list' node, to list the MAC address you set in 'ble_config.conf'
Click 'Deploy'
in Terminal execute the following command:
sudo systemctl start smartthings-mqtt-bridge
You can watch live status of the service by running
journalctl -f -u BLE-remote
in Terminal execute the following command:
sudo systemctl enable smartthings-mqtt-bridge
Now you can:
Run the service
Push the BLE button
and Watch Node-Red 'Debag' window for Clicks
Merge the result with your automation (node-red dashboard is a good place to start..)