Knowledge Base

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

Knowledge Base

IO Pi Plus Tutorial - MQTT Reading the Ports

Reading the GPIO pins on the IO Pi Plus using MQTT Message Queue Telemetry Transport

In this tutorial, we will use MQTT (Message Queue Telemetry Transport) to communicate with an IO Pi Plus to read the status of the pins, which are all set as inputs. You will need your Raspberry Pi and an IO Pi Plus for this. You will also need a second computer as the server/host device.

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

You must 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 python3-smbus; you can install it using apt-get with the following commands.

sudo apt-get update
sudo apt-get install python3-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 configured to use i2c, we can begin building our project.

Stage 1 – Create the server on the Raspberry Pi to read the IO Pi Plus pins and send the MQTT messages.

If you haven't done so, install your IO Pi Plus onto the Raspberry Pi by connecting it to 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 Plus.

We will start this tutorial by creating a new Python program file called demo_mqtt_read_server.py. You can use your favourite text editor to write 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.

We will use both MCP23017 controllers and 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 can access all 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/server's 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()

We must create a loop to read the IO Pi Plus ports, 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 the following:

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.

You need a client computer running the same software as the server machine to send messages from the Raspberry Pi.

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 script will listen to messages sent to "sensor/iopi/ports" via MQTT and return four bytes of data for the IO Pi ports/buses.

#!/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 the following:

python3 demo_mqtt_read_client.py

Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit
Created 24/12/2017 | Last Updated: 11/11/2023

Related Expansion Boards

Order a IO Pi Plus Photo of IO Pi Plus
IO Pi Plus

32 channel MCP23017 digital I/O expander for the Raspberry Pi

£14.39 Ex VAT: £11.99
Add to Basket

Related Articles

IO Pi Plus Tutorial 1 - The Blinking LED
Blinking an LED with the Raspberry Pi
IO Pi Plus Tutorial 2 - Push the Button
Reading a button input using a Raspberry Pi and IO Pi
IO Pi Plus Tutorial 3 - Introducing Interrupts
Using Interrupts with the IO Pi Plus
IO Pi Plus Tutorial 4 - More Interrupts
Using the IO Pi interrupts with the Raspberry PI GPIO interrupts
IO Pi Plus Tutorial - MQTT Reading the Ports
Reading the GPIO pins on the IO Pi Plus using MQTT Message Queue Telemetry Transport
IO Pi Plus with Raspberry Pi Pico
Using the IO Pi with Raspberry Pi Pico and MicroPython
IO Pi Plus Tutorial - MQTT Control
Control the GPIO Pins on the IO Pi Plus using the MQTT Message Queue Telemetry Transport
Driving Relays or higher loads with the IO Pi Plus
Using the IO Pi Plus or Zero with relays or opto-isolators
Relay Board for the IO Pi Plus 2.1
Relay board project for the IO Pi Plus
16 Channel Opto-Isolated Input Board
For use with the IO Pi Plus and IO Pi Zero