Knowledge Base

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

IO Pi Tutorial - MQTT Reading the Ports

Created 24/12/2017 | Reading 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 to read the status of the pins which are all set as inputs. 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 server on the raspberry pi to read the IO Pi pins and send 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.

For this tutorial we will start by creating a new python program file called demo_mqtt_read_server.py. You can use your favourite text editor for writing the program.  You can find a complete example of demo_mqtt_read_server.py in the ABElectronics_Python_Libraries/IOPi/demos/ 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.

iobus1 = IOPi(0x20)
iobus2 = 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 inputs and enable the pullup resistors.

iobus1.set_port_direction(0, 0xFF)
iobus1.set_port_pullups(0, 0xFF)

iobus1.set_port_direction(1, 0xFF)
iobus1.set_port_pullups(1, 0xFF)

iobus2.set_port_direction(0, 0xFF)
iobus2.set_port_pullups(0, 0xFF)

iobus2.set_port_direction(1, 0xFF)
iobus2.set_port_pullups(1, 0xFF)

Next we will add the methods for the MQTT server.

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

# when receiving a mqtt message;

def on_message(client, userdata, msg):
  message = str(msg.payload)
  print(msg.topic+" "+message)

def on_publish(mosq, obj, mid):
    print("mid: " + str(mid))

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.on_connect = on_connect
client.on_message = on_message
client.connect("10.0.0.49", 1883, 60)
client.loop_start()

Now we need to create a loop to read the IO Pi ports and publish the messages to "sensor/iopi/ports" and repeat every 10 seconds.

while True:
  sensor_data = [iobus1.read_port(0),iobus1.read_port(1), iobus2.read_port(0),iobus2.read_port(1)]
  client.publish("sensor/iopi/ports", str(sensor_data))
  time.sleep(10)

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

python3 demo_mqtt_read_server.py

Stage 2 – Create the client on a raspberry pi or other Linux computer to read and process the MQTT messages.

In order to send messages from the Raspberry Pi you need a client computer which will run the same software as the server 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_read_client.py to listen for the MQTT messages from the Raspberry Pi.

The simple script will listen for messages sent to "sensor/iopi/ports" via MQTT and return four bytes of data for the IO Pi ports/busses.

#!/usr/bin/env python3

import paho.mqtt.client as mqtt

# This is the Subscriber

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

def on_message(client, userdata, msg):
  print(msg.payload.decode())
  #client.disconnect()

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

client.on_connect = on_connect
client.on_message = on_message

client.loop_forever()

Run this code using:

python3 demo_mqtt_read_client.py