├── mqtt_publish1.py ├── mqtt_publish2.py ├── mqtt_subscribe.py └── readme.md /mqtt_publish1.py: -------------------------------------------------------------------------------- 1 | import paho.mqtt.client as mqtt 2 | from random import randrange, uniform 3 | import time 4 | 5 | mqttBroker = "mqtt.eclipseprojects.io" 6 | client = mqtt.Client("Temperature_Inside") 7 | client.connect(mqttBroker) 8 | 9 | while True: 10 | randNumber = uniform(20.0, 21.0) 11 | client.publish("TEMPERATURE", randNumber) 12 | print("Just published " + str(randNumber) + " to Topic TEMPERATURE") 13 | time.sleep(1) 14 | -------------------------------------------------------------------------------- /mqtt_publish2.py: -------------------------------------------------------------------------------- 1 | import paho.mqtt.client as mqtt 2 | from random import randrange, uniform 3 | import time 4 | 5 | mqttBroker = "mqtt.eclipseprojects.io" 6 | client = mqtt.Client("Temperature_Outside") 7 | client.connect(mqttBroker) 8 | 9 | while True: 10 | randNumber = randrange(10) 11 | client.publish("TEMPERATURE", randNumber) 12 | print("Just published " + str(randNumber) + " to Topic TEMPERATURE") 13 | time.sleep(1) 14 | -------------------------------------------------------------------------------- /mqtt_subscribe.py: -------------------------------------------------------------------------------- 1 | import paho.mqtt.client as mqtt 2 | import time 3 | 4 | def on_message(client, userdata, message): 5 | print("Received message: ", str(message.payload.decode("utf-8"))) 6 | 7 | mqttBroker = "mqtt.eclipseprojects.io" 8 | client = mqtt.Client("Smartphone") 9 | client.connect(mqttBroker) 10 | 11 | client.loop_start() 12 | client.subscribe("TEMPERATURE") 13 | client.on_message = on_message 14 | time.sleep(30) 15 | client.loop_end() 16 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Playing around with Python MQTT client Paho-MQTT and Mosquitto MQTT broker. 2 | You find a detailed explanation of the code and MQTT basics on medium: https://medium.com/@codeanddogs/mqtt-basics-with-python-examples-7c758e605d4 3 | --------------------------------------------------------------------------------