generated from cepdnaclk/eYY-3yp-project-template
-
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
Showing
1 changed file
with
34 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,34 @@ | ||
import paho.mqtt.client as mqtt | ||
|
||
# Define the MQTT broker address and port | ||
broker_address = "localhost" # Change to the IP address of your broker if remote | ||
broker_port = 1883 | ||
topic = "pebo/task" # Define the topic to subscribe to | ||
|
||
# Define the callback for connection | ||
def on_connect(client, userdata, flags, rc): | ||
print(f"Connected with result code {rc}") | ||
client.subscribe(topic) | ||
|
||
# Define the callback for message reception | ||
def on_message(client, userdata, msg): | ||
print(f"Message received on topic {msg.topic}: {msg.payload.decode()}") | ||
|
||
# Create the client instance | ||
client = mqtt.Client() | ||
client.on_connect = on_connect | ||
client.on_message = on_message | ||
|
||
# Connect to the broker | ||
client.connect(broker_address, broker_port, 60) | ||
|
||
# Start the loop to listen for messages | ||
client.loop_start() | ||
|
||
# Keep the script running to receive messages | ||
import time | ||
time.sleep(10) | ||
|
||
# Stop the loop and disconnect the client | ||
client.loop_stop() | ||
client.disconnect() |