Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

IO Pi Tutorial - MQTT Control

Created 23/12/2017 | Control your IO Pins on the IO Pi using MQTT Message Queue Telemetry Transport

In this tutorial we will use MQTT (Message Queue Telemetry Transport) to communicate with an IO Pi using the pins as outputs to switch on and off remotely. For this you will need your Raspberry Pi and an IO Pi. You will also need a second computer to act as the server/host device.

We will use the AB Electronics python library to talk to the IO Pi, to download the library visit our Python Library and Demos knowledge base article.

You will need to enable i2c on your Raspberry Pi, see our other tutorial on i2c: I2C, SMBus and Raspbian Linux

The AB Electronics python library uses another library called python-smbus, you can install it using apt-get with the following commands.

sudo apt-get update
sudo apt-get install python-smbus

To install the required MQTT software you need to install mosquitto.

sudo apt-get install mosquitto

You also need to install the python library paho-mqtt using pip3.

sudo apt-get install python3-pip

Now you can install paho-mqtt.

sudo pip3 install paho-mqtt

With the libraries installed and the Raspberry Pi is configured to use i2c we can begin building our project.

Stage 1 – Create the client on the raspberry pi to listen and process the MQTT messages.

If you haven’t done so install your IO Pi onto the Raspberry Pi by connecting it onto the GPIO header.  Make sure your Raspberry Pi is turned off when you do this to minimise the risk of damaging the Raspberry Pi or the IO Pi.

The maximum current you can draw from any of the pins on the MCP23017 is 25mA so by using the 200R resistor in series with the LED we limit the current to a safe level.

For this tutorial we will start by creating a new python program file called demo_mqtt_client.py.  You can use your favourite text editor for writing the program.  You can find a complete example of demo_mqtt_client.py in the ABElectronics_Python_Libraries/IOPi/ folder.

At the top of your program you will need to import the required libraries.

#!/usr/bin/env python
from IOPi import IOPi
import time
import paho.mqtt.client as mqtt

The IOPI library is used for all communication with your IO Pi, it gives you control over almost everything that can be done with the MCP23017 controller.

As we are going to be using both of the MCP23017 controllers we will create two instances of the IOPI class.

iobus = IOPi(0x20)
iobusb = IOPi(0x21)

0x20 is the I2C address for the controller chip on bus 1 and 0x21 is the I2C address for the controller chip on bus 2, if you have changed the address selection jumpers on your IO Pi then you will need to change this number to match the new address.

With our new instances of the IOPI class we will be able to access all of the available methods for controlling the IO Pi.  Set all pins as outputs and set to off

iobus.set_port_direction(0, 0x00)
iobus.write_port(0, 0x00)

iobus.set_port_direction(1, 0x00)
iobus.write_port(1, 0x00)

iobusb.set_port_direction(0, 0x00)
iobusb.write_port(0, 0x00)

iobusb.set_port_direction(1, 0x00)
iobusb.write_port(1, 0x00)

Next we will add the methods for the MQTT client.

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))
  client.subscribe("topic/iopi")

Now add the on_message function which listens for a new message and splits the command into the pin and mode. This will turn on or off the selected pin on the IO Pi busses.

def on_message(client, userdata, msg):
  p,s = msg.payload.decode().split(",")
  if (int(p) <= 16):
    print("IO Bus 0 Pin: " + p)
    print("IO Bus 0 mode: " + s)
    iobus.write_pin(int(p), int(s))
  else:
    print("IO Bus 1 Pin: " + p)
    print("IO Bus 1 mode: " + s)
    iobusb.write_pin((int(p) -16), int(s))

Next we add the connection code for MQTT to connect to the broker/server. Change the IP address to match your own broker/servers address.

client = mqtt.Client()
client.connect("10.0.0.49",1883,60)

client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()

The IO Pi is now ready to listen for MQTT messages sent to "topic/iopi"

To run this code and set your Raspberry Pi to respond to MQTT messages you need to run the script using:

python3 demo_mqtt_client.py

Stage 2 – Create the server/broker on the raspberry pi or other Linux computer to send and process the MQTT messages.

In order to send messages to the Raspberry Pi client you need a server computer which will run the same software as the client machine.

We used an installation of Ubuntu for the following code but any MQTT server/broker will perform the same function.

To install the required MQTT software you need to install mosquitto.

sudo apt-get install mosquitto

You also need to install the python library paho-mqtt using pip3.

sudo apt-get install python3-pip

Now you can install paho-mqtt.

sudo pip3 install paho-mqtt

Once all the software has been installed, reboot the system and we will create a new file called demo_mqtt_server.py to send MQTT messages to the Raspberry Pi.

The simple script will use commands to turn pin on or off using client "topic/iopi" With message of pin number,state i.e 5,1 turns pin 5 on the second bus on the IO Pi Plus is accesses using pins 17 to 32.


#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import time

# Create client instance and connect to localhost
client = mqtt.Client()
client.connect("localhost",1883,60)

# Publish message to topic/iopi and set pin 1 on bus 1 to on
client.publish("topic/iopi", "1,1");
time.sleep(2)

# Publish message to topic/iopi and set pin 1 on bus 1 to off
client.publish("topic/iopi", "1,0");
time.sleep(2)
# Publish message to topic/iopi and set pin 1 on bus 2 to on
client.publish("topic/iopi", "17,1");
time.sleep(2)
# Publish message to topic/iopi and set pin 1 on bus 2 to off
client.publish("topic/iopi", "17,0");
client.disconnect();

When you run this code it will turn on pin 1 on bus 1 for two seconds then turn the pin off and repeat using pin 1 on bus 2 (pin 17) and turn off two seconds later.

python3 demo_mqtt_server.py