Browse Source

Raspy MQTT Tests added

master
waldluis 1 year ago
parent
commit
e39b2a2753
2 changed files with 34 additions and 0 deletions
  1. 13
    0
      software/roboter/raspy/mqtt_publish1.py
  2. 21
    0
      software/roboter/raspy/mqtt_subscribe.py

+ 13
- 0
software/roboter/raspy/mqtt_publish1.py View File

import paho.mqtt.client as mqtt
import random
import time

mqttBroker = "mqtt.eclipseprojects.io"
client = mqtt.Client("Temperature")
client.connect(mqttBroker)

while True:
randNumber = random.randint(0, 20)
client.publish("TEMPERATURE", randNumber)
print("Published " + str(randNumber) + " to topic TEMPERATURE")
time.sleep(2)

+ 21
- 0
software/roboter/raspy/mqtt_subscribe.py View File

import paho.mqtt.client as mqtt


def on_message_temp(client, userdata, message):
print("Received temp message: ", str(message.payload.decode("utf-8")))

def on_message_hum(client, userdata, message):
print("Received hum message: ", str(message.payload.decode("utf-8")))

mqttBroker = "mqtt.eclipseprojects.io"
client = mqtt.Client("Smartphone_temp")

client.message_callback_add("TEMPERATURE", on_message_temp)
client.message_callback_add("HUMIDITY", on_message_hum)

client.connect(mqttBroker)
client.subscribe("TEMPERATURE")
client.subscribe("HUMIDITY")

client.loop_forever()

Loading…
Cancel
Save