
Table of Contents
1. Introduction
2. The Basics of MQTT
3. Set Up the Mosquitto MQTT Server
4. Testing the Mosquitto MQTT Server Installation
5. Create an App on iOS using Swift
6. Testing the iOS MQTT app using Simulator
7. Create a program on Raspberry Pi using Python
8. Testing the communication between iOS and Raspberry Pi using MQTT
9. What's Next?
Introduction
Let's say that you have an LED connected to a GPIO pin and you want to turn it on and off. One way to solve this problem is to use a button. You connect a button to another GPIO pin. From here, you would create a program that detects changes in the button's state and determine whether to turn the LED on or off. This gets the job done!
However, what if you were far away from that button? It would be a hassle to walk back to the Raspberry Pi and press the button to turn the LED on or off. Wouldn't it be nice if you could just turn the LED on or off remotely from a portable device like an iPhone or iPad? If so, you're in the right place!
In this guide, I will talk about how you can establish a connection between an iOS device and a Raspberry Pi so you can do stuff like controlling GPIO pins remotely. Since this guide will go in depth, you will need to get your hands dirty which means that you will be required to code. Don't worry, the code will be provided but the main goal is for you to understand how to implement such task manually instead of relying on someone else to do it. Raspberry Pi is of course a great platform to learn about programming and to stimulate ideas.
Let's begin!
The Basics of MQTT
MQTT is a popular lightweight messaging protocol used by Internet of Things (IoT) devices. Since this is a lightweight protocol, very little power is used. The idea is simple. The MQTT architecture is made up of clients and an MQTT server (sometimes referred to as MQTT broker). The clients are the devices connected to the MQTT server.

For this guide, an iOS device and a Raspberry Pi will be our clients. These clients will connect to the MQTT server. The question is, who is the MQTT server?

This might sound weird, but the Raspberry Pi is the MQTT server. In other words, the Raspberry Pi is a client and is the MQTT server at the same time. It will communicate with itself.

But this makes sense. To be clear, the MQTT server is simply a program that will run in the background on the Raspberry Pi. The MQTT server will receive the messages sent by the clients and send them to other clients connected to the MQTT server. A client that sends messages is known as a publisher. A client that receives messages is known as a subscriber. Each message sent by a publisher contains a topic. A subscriber that is subscribed (or signed up) to that topic will receive the message. For this example, let's assume that the publisher is the iOS device and the subscriber is the Raspberry Pi. The message created by the publisher has the topic of "resetGPIO" and the Raspberry Pi is subscribed to the topic "resetGPIO".

As you can see from the top image, the iOS device is the publisher. It sends a message with a topic of "resetGPIO". This message is received by the MQTT server. The MQTT server searches for the subscriber that is subscribed to the topic of the message. Since the Raspberry Pi is subscribed to the topic "resetGPIO", the MQTT server sends the message to the Raspberry Pi itself and the message is successfully delivered. Now, what happens if the publisher sends a message with a topic that no subscriber is subscribed to?

The message simply gets thrown out since there is no destination for it.
Since we're on the discussion on sending messages from a publisher to a subscriber, can a client be a publisher and a subscriber at the same time? The answer to that is yes! Let's assume that an iOS device publishes to the topic "resetGPIO" and is subscribed to the topic "sensorData". The Raspberry Pi publishes to the topic "sensorData" and is subscribed to the topic "resetGPIO".

From the top image, the iOS device sends a message with the topic of "resetGPIO" and is received by the MQTT server. Since the Raspberry Pi itself is subscribed to the topic "resetGPIO", the MQTT server sends the message to itself and the message is successfully delivered. The Raspberry Pi sends a message with the topic of "sensorData" and since the MQTT server is running on the Raspberry Pi itself, it sends the message to itself. Since the iOS device is subscribed to the topic "sensorData", the MQTT server sends the message to the iOS device and the message is successfully delivered.
Does it sound confusing? If so, that's fine since we're now going to see what is happening behind the scenes. This should clear things up. For this example, we have two clients that will communicate with each other. The two clients are an iOS device and a Raspberry Pi. The iOS device will run an application that implements the MQTT protocol while the Raspberry Pi will run a program that implement the MQTT protocol.

The iOS application will contain code that implements the publisher and/or subscriber model. The same goes for the Raspberry Pi. The Raspberry Pi program implements the publisher and/or subscriber model. All that is left is the MQTT server program. It makes sense for the Raspberry Pi to run the MQTT server program as well since it is really easy to get it up and running on the Raspberry Pi platform.

The Raspberry Pi is now running two programs, the MQTT server and a program that implements the publisher and/or subscriber model. For the MQTT protocol to work, each client registers itself with the MQTT server program. The client will provide its name, network address, and subscribed topics, if any. When a publisher (client that sends messages) sends a message, the message goes to the Raspberry Pi where the MQTT server will intercept the message. The MQTT server will determine the topic of the message received and search for the registered subscribers (clients subscribed to a specific topic) subscribed to that topic. If the Raspberry Pi itself is subscribed to that topic, then the MQTT server will send the message to the Raspberry Pi itself but now the message will now be received by the Raspberry Pi program instead. This explains why the Raspberry Pi sends messages to itself. It is either to send the message to the MQTT server program or for the Raspberry Pi program to receive the message sent by the MQTT server.
The last thing that you might be wondering is, what exactly does a message contain? Very simple, a topic and a message! But from a coding perspective, the topic and message are simply strings like "1234567890" or "online" or "true" or "tv is on".
That's it for the basics of MQTT! If you are still interested in using MQTT and want to lean how implement it, continue to read on!
Set Up the Mosquitto MQTT Server
We will be installing the Mosquitto MQTT server on the Raspberry Pi. There are two choices. You can either build the latest version of Mosquitto MQTT server from Eclipse Mosquitto or download an older stable release from the Mosquitto Debian repository. Regardless of your choice, both will work for this guide.
OPTION 1: Stable Release from Mosquitto Debian Repository
First, you must import the Mosquitto repository package signing key. Run these commands in Terminal:
Code: Select all
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
Code: Select all
cd /etc/apt/sources.list.d/
Raspbian Wheezy
Code: Select all
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
Code: Select all
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
Code: Select all
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
Code: Select all
sudo apt-get update
Code: Select all
sudo apt-get install mosquitto
1. Change the working directory to your home folder by entering the command below:
Code: Select all
cd
Code: Select all
wget http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u7_armhf.deb
Code: Select all
sudo dpkg -i libssl1.0.0_1.0.1t-1+deb8u7_armhf.deb
Code: Select all
wget http://ftp.us.debian.org/debian/pool/main/libw/libwebsockets/libwebsockets3_1.2.2-1_armhf.deb
Code: Select all
sudo dpkg -i libwebsockets3_1.2.2-1_armhf.deb
Code: Select all
sudo apt-get install mosquitto
That's it! Mosquitto MQTT server should now be installed! Now read "Testing the Mosquitto MQTT Server Installation" section below to test the installation.
OPTION2: Latest Release from Eclipse Mosquitto
We will compile the Mosquitto MQTT server source code from Eclipse Mosquitto. Before you continue, make sure that the Raspberry Pi has the "build-essential" package installed. This pacakge is necessary to compile programs from source. Most newer releases of Raspbian and Raspbian Lite already include "build-essential" package. If you are not sure, simply run this command in Terminal:
Code: Select all
sudo apt-get install build-essential
Now, change the working directory to your Home folder by running this command in Terminal:
Code: Select all
cd
Code: Select all
wget https://mosquitto.org/files/source/mosquitto-1.6.2.tar.gz
An archive file named "mosquitto-1.6.2.tar.gz" will be found in your home folder. Extract the contents of this archive file by running this command:
Code: Select all
tar xavf mosquitto-1.6.2.tar.gz
Code: Select all
cd mosquitto-1.6.2
Code: Select all
sudo apt-get install cmake libssl-dev libwebsockets-dev uuid-dev libc-ares-dev
Code: Select all
cmake -DWITH_WEBSOCKETS=ON .
Code: Select all
make -j4
Code: Select all
sudo make install
Code: Select all
sudo /sbin/ldconfig
Testing the Mosquitto MQTT Server Installation
Now that Mosquitto MQTT server has been installed on the Raspberry Pi, let's test it!
To run Mosquitto, run the command below:
Code: Select all
mosquitto -v

The "-v" flag simply means verbose mode, where Mosquitto MQTT server will print out any activity that is currently happening. If you see the screen above, it means that Mosquitto is working correctly!
However, if you get "-bash mosquitto: command not found" instead, it means that your user account is not authorized to execute commands from /usr/local/sbin. The fix is simple. You need to add "usr/local/sbin" to PATH. In Terminal, change the working directory to your home folder. Then run the command:
Code: Select all
nano .bashrc
Code: Select all
export PATH=$PATH:/usr/local/sbin

Once you have made those changes, save the file and reboot your Raspberry Pi. The "mosquitto" command should now be working!
At anytime, to terminate the Mosquitto MQTT server, press CTRL+C on the Terminal window. Now that the Mosquitto MQTT server is installed and running correctly, all that is left is to create an application for iOS and to create a program for Raspberry Pi that implements the MQTT protocol. Then you will see what the MQTT protocol can really do! If you are up for the challenge, let's continue!