In two previous posts I explained how we can add iPhone presence detection to our Raspberry Pi 3 and how we can install and use an MQTT client to publish messages.
Now it’s time to combine these to and publish our iPhone presence to our MQTT broker.
In this post I will layout the steps needed to do this
Create a bash script ‘detect_bluetooth_presence.sh’
nano detect_bluetooth_presence.sh
Import the following code and edit the parameters
#!/bin/bash # Script inspiration form: https://www.domoticz.com/forum/viewtopic.php?t=12570 # Set Parameters mqttserverip='192.168.XXX.XXX' bluetoothmac='XX:XX:XX:XX:XX:XX' mqtttopic='home/sensors/presence/iphone' messagefound='On' messagenotfound='Off' waittimebetweenchecks=10 while [ 1 ] do # Actual pinging... result=$(l2ping -c1 -s32 -t1 "$bluetoothmac" > /dev/null && echo $messagefound || echo $messagenotfound) if [[ $result == $messagenotfound ]]; then # Try once more to connect result=$(l2ping -c1 -s32 -t1 "$bluetoothmac" > /dev/null && echo $messagefound || echo $messagenotfound) fi # Only send a publication if status is changed compared to last check if [[ $previousresult != $result ]]; then previousresult=$result mosquitto_pub -h $mqttserverip -t $mqtttopic -m $result -r fi sleep $waittimebetweenchecks done
Now we need to set execute permissions to our script
sudo chmod a=r+w+x detect_bluetooth_presence.sh
Auto run on startup
To run this script on startup we edit our rc.local file
sudo nano /etc/rc.local
Add the line:
sudo /home/pi/detect_bluetooth_presence.sh&
Now reboot and your good to go
Are you able to detect when you come home and/or leave the house with this? Or is the bluetooth not sufficiently strong for this?
LikeLike
hi, im currently using it to track my iphone to open my front door, it works good
LikeLike
hi, the script worked perfectly, but I want to be able to track 2 o more bluetooth devices, can you share an example with 2 iphones?
LikeLike