ich kenne mich mit MQTT und Python leider noch nicht all zu gut aus und habe deswegen eine Frage. Aktuell programmiere ich mit einem Dexter Industries und einem RPi3b ein autonomes Fahrzeug.
Ich bekomme per MQTT unterschiedliche Topics und kann je nach dem, unterschiedliche Motoren und Sensoren ansteuern. Ich würde nur gerne, dass das Transportsystem am Ende der Durchführung einer Aufgabe eine eigene Nachricht Published. Ich weiß leider nicht wie das geht und vielleicht kann mir jemand Hilfe bezüglich meines Codes geben, wie ich am besten eine Nachtricht publishen kann (siehe Ende des Codes)
Code: Alles auswählen
from __future__ import print_function
from __future__ import division
import time
import brickpi3
import paho.mqtt.client as mqtt
BP.brickpi3.BrickPi3()
BP.set_sensor_type(BP.Port_1, BP.SENSOR_TYPE.EV3_COLOR_REFLECTED)
MQTT_Server = "192.168.0.21"
MQTT_PATH = "/RoboticLabor/Lagerplaetze/#"
def on_connect(client, userdata, flags, rc):
print("Connected")
client.subscribe(MQTT_PATH)
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
Case = 0
Reihe = 1
Start = 0
if str(msg.topic) == "/RobotikLabor/Lagerplaetze/Lagerplatz1" and str(msg.payload) == "false":
print("Fahre zu Lagerplatz 1")
Lagerspalte = 1
Hoehe = 1
Start = 1
while Start == 1 and Case == 0:
try:
BP.set_sensor_type(BP.Port_1, BP.SENSOR_TYPE.EV3_COLOR_COLOR_COMPONENTS)
time.sleep(0.02)
Komponenten = BP.get_sensor(BP.PORT_1)
Motorspeed = 80
if Komponenten[0] > 80 and Komponenten[1] < 40 and Komponenten[2] < 40:
BP.set_motor_dps(BP.PORT_B + BP.PORT_C, 0)
else:
if Komponenten[0] < 40:
BP:set_motor_dps(BP.PORT_B, Motorspeed/2)
BP:set_motor_dps(BP.PORT_C, -Motorspeed)
elif Komponenten[0] > 40 and Komponenten[0] < 150:
BP:set_motor_dps(BP.PORT_B, -Motorspeed)
BP:set_motor_dps(BP.PORT_C, -Motorspeed)
elif Komponenten[0] > 150:
BP:set_motor_dps(BP.PORT_C, Motorspeed/2)
BP:set_motor_dps(BP.PORT_B, -Motorspeed)
except KeyboardInterrupt:
BP.reset_all()
# Hier soll dann eine Nachricht von dem Broker 192.168.0.21 mit dem Topic /RobotikLabor/Transportsysteme/TS1 versendet werden!!!
Flo