Using MQTT to send the IP address from a Raspberry PI

Docker is all the rage nowadays. I am privileged to use it daily, though not on a RasPI. Recently, I was about to give a demo illustrating the span of devices that it supports. The clever Docker Pirates have even made a Raspberry PI kernel complete with a Docker container, so I installed it on a spare SD card. Only to discover that the kernel doesn’t support the new and shiny 7″ touchscreen, leaving my RasPi headless in every respect. (Update: It does now. Support became available the day after… But I hope this post is relevant if you want to run the RasPI headless)

The instructions on the pirate website suggest to use nmap (the port scanner) to find the IP. Even used carefully, it is not  a very popular tool in a corporate IT environment. I could have used it, but risked setting off an alarm somewhere.

I gambled on being able to fix the display in the remaining hours, but…

The journalist was arriving in five minutes, and I had no idea how to SSH to the box so that I could demonstrate the one-line «docker run» command and get a web server running. The helpful guys at corporate IT located the IP-address after I asked for help. But there should be an easier way!

The Interview and demo went fine. But I am no stranger to this situation: I have given a ton of IoT-related demos with Raspberry Pi the last couple of years, and I always had to carry with me a small HDMI display and keyboard/mouse just to obtain the IP address at a new site.

My shiny new touch-screen. With the docker kernal it was ptich black

My shiny new RasPI touchscreen. With the docker kernel it was pitch black!

I could make a script that sends the IP address as an email, no? Well. I don’t like to leave my username and password on the PI. Neither do I want to have the mailbox spammed with IP addresses.

..what to do?

MQTT to the rescue!  MQTT is a IoT-friendly protocol with a lightweight footprint. The clue here is to use a public MQTT broker – in my case mqttdashboard.com – so that both the RasPi and my mac could reach it.

I found a rather decent solution by San Bergman where he solves the trickery of getting the IP address for an interface and mails it. So I changed it to use MQTT:

1. Install Mosquitto on your Raspi and PC/Mac

Mosquitto is an obvious choice here, with excellent community support.

On the PI it is pretty easy:

sudo apt-get install mosquitto mosquitto-clients 

To get clients for Mac and PC, please refer to the instructions on the mosquitto website

2. Make a script that posts the IP address to MQTT

First, test and select  a public MQTT broker that you can reach from both the PI and your laptop.

Create a folder /home/pi/bin and edit the file mqttmyip.sh:

#! /bin/bash

# mqttmyip.sh
# Posts the IP address of an interface to an MQTT queue
#
# Use: mqttmyip [interfacename]
# Author: Simen Sommerfeldt, based on work by San Bergmans (www.sbprojects.net)

# Configuration variables
RPINAME="RaspberryPi"

#MQTT broker. If this isn´t up check https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
MQTTHOST="iot.eclipse.org"

# Change this to become something unique, so that you get your own topic path
MQTTPREFIX="yourname"

# Get interface name from parameters
if [ $# -eq 0 ]
then
 IFC="eth0"
else
 IFC="$1"
fi

ifconfig $IFC &> /dev/null
if [ $? -ne 0 ]
then
 exit 1
fi

# Get current private IP address for the selected interface
PRIVATE=$(ifconfig $IFC | grep "inet addr:" | awk '{ print $2 }')
PRIVATE=${PRIVATE:5}
# Exit if IP address is empty
if [ -z $PRIVATE ]
then
 exit 0
fi

/usr/bin/mosquitto_pub -h $MQTTHOST -t "$MQTTPREFIX/$RPINAME/$IFC/ip" -m "$PRIVATE"

Remember to make it executable:

chmod +x mqttmyip.sh

3. Make it run when the RasPI boots

The /etc/rc.local script allows you to insert commands that run after a reboot. I added three lines at the end, so that it reports the address of the interfaces that I can think of. Remember to use «sudo» when you edit it..

/home/pi/bin/mqttmyip.sh wlan0 &
/home/pi/bin/mqttmyip.sh wlan1 &
/home/pi/bin/mqttmyip.sh eth0 &
exit 0

4. Reboot, and get the IP

Having installed the Mosquitto client, do the following from your PC/mac before you power the RasPI:

 mosquitto_sub -v -h <<MQTT BROKER OF CHOICE>> -t "yourname/#"

NB! Of course you have to replace the topic with whatever you chose in the script

In my case, it looked like this:

Skjermbilde 2015-10-10 kl. 21.10.14

Voila! I no more need to drag a keyboard and mouse around for demos. This small hack really illustrates the versatility of the MQTT protocol

2 tanker om “Using MQTT to send the IP address from a Raspberry PI

    • I hope you don don’t find me dismissive when I say that I doubt if this is suitable use-case for Docker. It’s a fire-once script that runs on startup, not a process component that benefits from lightweight virtualization. Besides, most PI’s don’t have Docker. Only those with the Kernel from Docker Pirates.

Leave a reply to alexus Avbryt svar