pub_02.py

#! /usr/bin/python3
# ===================================================================
# MQTT Publish
# -------------------------------------------------------------------
# Python Client Documentation
# //www.eclipse.org/paho/index.php?page=clients/python/docs/index.php
# -------------------------------------------------------------------
# Simple Python MQTT Publish and Subscribe Example Script
# http://www.steves-internet-guide.com/python-mqtt-publish-subscribe/
# -------------------------------------------------------------------
# Python MQTT Client Connections– Working with Connections
# http://www.steves-internet-guide.com/client-connections-python-mqtt/
# ===================================================================

import paho.mqtt.client as paho
import time

broker = '192.168.1.77'
topic  = 'sensors/sensor2'

# ---- main

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

print("Create client ...")
client = paho.Client("paho-client")

print("Connect to broker ...")
client.connect(broker)

print("Sending status ...")
client.publish(topic, 'abc')

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

print("Sending status ...")
client.publish(topic, 'xyz')