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.
# 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 = "192.168.0.100"
# declare I/O ports
#==================
#outputs
garoff = 16
garon = 0
gar = garoff #default P4
#inputs
#P1 garst 0=ON/2=OFF
# declare variables
# =================
garst = True
msg = ""
#Functions
#=========
def readbus(adres):
#read bus and check for error
try:
result = bus.read_byte(adres)
except:
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
try:
bus.write_byte(adres,data)
except:
print time.asctime(time.localtime(time.time())) + (" NO WRITE CONTACT!"),hex(adres)
print "=" * 24
return
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
msg=(str(message.payload.decode("utf-8")))
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
client1.subscribe("home")
# ===============================
# 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
client1.publish("home","garst1")
garst = False
else:
if not garst: # publish status only once
client1.publish("home","garst0")
garst = True
# send data to slaves
#====================
writebus(PCF8574,0x0F+P7+P6+P5+gar)
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.
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)