-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marco Bakera
committed
Sep 21, 2024
1 parent
f123ed3
commit d6f11a4
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# publisher | ||
|
||
import paho.mqtt.client as mqtt | ||
|
||
MQTT_BROKER = "localhost" # "test.mosquitto.org" | ||
TOPIC = "Erdgeschoss/Wohnzimmer/Temp" | ||
|
||
publisher = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) | ||
publisher.connect(MQTT_BROKER) | ||
publisher.loop_start() | ||
|
||
input("Press Enter to publish message") | ||
publisher.publish(topic=TOPIC, payload=22) | ||
|
||
input("Press Enter for next message") | ||
publisher.publish(topic=TOPIC, payload="23 ret", retain=True) | ||
|
||
input("Press Enter to exit") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# subscriber | ||
|
||
import paho.mqtt.client as mqtt | ||
|
||
MQTT_BROKER = "localhost" # "test.mosquitto.org" | ||
TOPIC = "Erdgeschoss/Wohnzimmer/Temp" | ||
|
||
def my_connect_method(client, userdata, flags, rc, properties): | ||
print("Connected. Subscribing to topic", TOPIC) | ||
client.subscribe(TOPIC) | ||
|
||
def my_message_method(client, userdata, msg): | ||
msg_str = msg.payload.decode("UTF8") | ||
print(f"Message received. topic: {msg.topic} payload: {msg_str}") | ||
|
||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) | ||
client.on_connect = my_connect_method | ||
client.on_message = my_message_method | ||
|
||
client.connect(MQTT_BROKER) | ||
|
||
# client.loop_start() | ||
client.loop_forever() |