[12-06-09] verblendet1337 (1054526843627245569): mqtt_serial.py\n\n```python\n\nimport serial\nimport time\nimport paho.mqtt.client as mqtt\n\n\n#MQTT\nbroker = "172.20.10.4"\nport = 1883\ntopic = "test"\n\n#Arduino\nser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)\n\n\n#Nachricht wird  empfangen\ndef on_message(client, userdata, msg):\n\n    message = msg.payload.decode()\n    print(f"Nachricht: {message} Topic: {msg.topic}")\n\n    #Nachricht Arduino\n    ser.write((message + '\n').encode())\n\n# MQTT Client erstellen\nclient = mqtt.Client()\nclient.on_message = on_message\n\n# Verbindung zum Broker\nclient.connect(broker, port, 60)\nclient.subscribe(topic)\n\n\nprint("Empfangsbereit... ")\n\n# Endlosschleife zum Empfangen\nclient.loop_forever()\n\n```
[12-41-20] verblendet1337 (1054526843627245569): mqtt_sub.py\n\n```python\n\nimport paho.mqtt.client as mqtt\n\nbroker = "172.20.10.4"\nport = 1883\ntopic = "test"\n\n# Callback, wenn eine Nachricht empfangen wird\ndef on_message(client, userdata, msg):\n    print(f"Nachricht: {msg.payload.decode()} Topic: {msg.topic}")\n\n# Client erstellen\nclient = mqtt.Client()\n\n# Callback registrieren\nclient.on_message = on_message\n\n# Mit Broker verbinden\nclient.connect(broker, port, 60)\n\n# Auf Thema abonnieren\nclient.subscribe(topic)\n\nprint("Warte auf Nachrichten... ")\n\n# Endlosschleife zum Empfangen\nclient.loop_forever()\n```
