sub_01.py

#! /usr/bin/python3
# ===================================================================
# MQTT Subscribe
# -------------------------------------------------------------------
# Python Client Documentation
# www.eclipse.org/paho/index.php?page=clients/python/docs/index.php
# ===================================================================

import paho.mqtt.subscribe as subscribe
import time


broker = '192.168.1.77'
topic  = 'sensors/sensor1'


# ---- main

print()
print(f"broker: {broker}")
print(f"topic : {topic}")
print()

print("Subscribe to topic ...")
message = subscribe.simple(topic, hostname=broker)
m = str(message.payload.decode("utf-8"))
print(f"received message = {m}")

print("Sleep ...")
time.sleep(1)

print("Subscribe to topic ...")
message = subscribe.simple(topic, hostname=broker)
m = str(message.payload.decode("utf-8"))
print(f"received message = {m}")