Help, Controlling the chip pcf8574 (Port expander) via I2C from Raspberry Pi from openHAB

Dear Rik,
It meant to be “mqtt<==> python” part and those the “python code” method (like in C++). I wrote “method” because I associate function of python as access method to the exact pin from specific pcf8574 module.

At the first time, I thought that mqtt is a universal platform for communication with pcf8574 for example, but… I guess, where is the “communication part come from”. From where, the mqtt knows which module address and which pin I want to set/reset (?).
Now is more clear. When you will post here your python code, you will help to many people looking for nice solution for RaspberyPi GPIO pins extension.

I found a lot off information on this website;
The code structure I use (python) as follows;

# example of the followed structure...
import smbus # This is required to include i2c module.
import time  # This is required to include time module.
import paho.mqtt.client as mqtt # This is required to broadcast and receive the broker.

PCF8574 = 0x20

bus = smbus.SMBus(1)
brokeraddress = ""

# declare I/O ports
garoff = 16
garon = 0
gar = garoff #default P4

#P1 garst 0=ON/2=OFF

# declare variables
# =================
garst = True
msg = ""

def readbus(adres):
    #read bus and check for error
        result = bus.read_byte(adres)
        print time.asctime(time.localtime(time.time())) + (" NO READ CONTACT! "),hex(adres)
        print "=" * 24
        result = 0
    return result

def writebus(adres,data):
    #write bus and check for error
        print time.asctime(time.localtime(time.time())) + (" NO WRITE CONTACT!"),hex(adres)
        print "=" * 24

def on_connect(client, userdata, flags, rc):
    print "Connected flags"+str(flags)+"result code "+str(rc)+"client1_id  "+str(client)

def on_message(client1, userdata, message):
    global gar
    if msg=="gar1":
        gar = garon
    elif msg=="gar0":
        gar = garoff

# init MQTT server
# ================
client1 = mqtt.Client()             #create new instance
client1.on_connect=on_connect       #attach function to callback
client1.on_message=on_message       #attach function to callback
client1.connect(brokeraddress)      #connect to broker
client1.loop_start()                #start the loop

# ===============================
# from here the program
# ===============================
print "Starting program."
print "Press CTRL+C to break."
print "...."
while True:
    # PCF8574
    # =======
    slave = readbus(PCF8574)
    # status P1
    if slave & 2:
        if garst: # publish status only once
            garst = False
        if not garst: # publish status only once
            garst = True

    # send data to slaves

1 Like


newbie to OH and not yet presented to Python, this code you’ve uploaded where should I put it? It’s apart from OH, right? not a rule or something like that.


Did you managed to solve this? I have the same problem and no other solution to interact with the PCF8574


Good question Rodrigo_Delgado.
Rik Eric DG - can U help to understand what to do with your code to work with OH2?

I spend 5 days on trying implement the pcf8574control binding but do not work on my OH2 and crashes the system.

I had to come back on this topic, I found a great working solution.
Using the pcf8574 as a input. actually 6 of them so I have 48 digital inputs using 1 interrupt pin
all connected to the Rpi on whish openhab is running. and Node Red.

if any one is interested i’ll post the code the flows etc.
(one thing remaining and that is configuring my openhab, and work out how the mqtt convention (Homie) works)