Publish iPhone presence messages to a MQTT broker

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 ‘’


Import the following code and edit the parameters

# Script inspiration form:

# Set Parameters

while [ 1 ]
  # 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)

  # Only send a publication if status is changed compared to last check
  if [[ $previousresult != $result ]]; then
    mosquitto_pub -h $mqttserverip -t $mqtttopic -m $result -r
  sleep $waittimebetweenchecks

Now we need to set execute permissions to our script

sudo chmod a=r+w+x

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/

Now reboot and your good to go

3 thoughts on “Publish iPhone presence messages to a MQTT broker”

  1. 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?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s