Ich habe jetzt mal ein wenig mit deinem Input rumprobiert __blackjack__ aber bin leider nicht viel weiter gekommen. Ich habe jetzt nochmal eine andere Veranschaulichung von meinem aktuellen Problem:
Code
import time # import the time library for the sleep function
import paho.mqtt.client as mqtt
import brickpi3 # import the BrickPi3 drivers
MQTT_Server = "192.168.0.20"
MQTT_PATH = "/RobotikLabor/Lagerplaetze/#"
BP = brickpi3.BrickPi3()
def on_connect(client, userdata, flags, rc):
print("Connected with Raspberry Homestation")
client.subscribe(MQTT_PATH)
def on_message(client, userdata, msg):
if str(msg.topic) == '/RobotikLabor/Lagerplaetze/Lagerplatz1' and str(msg.payload) == 'false':
print(msg.topic+" "+str(msg.payload))
print('Fahre zu Lagerplatz 1.')
start = 1
lagerspalte = 1
hoehe = 1.5
case = 1
elif str(msg.topic) == '/RobotikLabor/Lagerplaetze/Lagerplatz1' and str(msg.payload) == 'true':
print(msg.topic+" "+str(msg.payload))
print('Kein Bauteil vorhanden')
start = 0
lagerspalte = 0
hoehe = 0
case = 0
def main():
#BP = brickpi3.BrickPi3()
try:
if start == 1:
print("Start = %i, Lagerspalte = %i, Hoehe = %i, case = %i " % (start, lagerspalte, hoehe, case))
else:
print("Start = %i, Lagerspalte = %i, Hoehe = %i, case = %i " % (start, lagerspalte, hoehe, case))
client =mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_Server, 1883, 60)
client.loop_forever()
except KeyboardInterrupt:
pass
finally:
BP.reset_all()
if __name__ == "__main__":
main()
Display More
Ich würde gerne anhand der eingehenden Nachrichten die vier Werte der Variablen setzen und dann in die main Funktion mit übergeben. Wie stelle ich das am besten an? Ich komme dort leider nicht weiter. Der aktuelle Error ist, dass "global name 'start' ist not defined".
Wäre super wenn mir hier noch jemand helfen könnte.