Skip to content
Snippets Groups Projects
Commit 6eba655d authored by Mark Pascal Sanders's avatar Mark Pascal Sanders
Browse files

Upload New File

parent 99934f3a
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:ffb6a1d8 tags:
### Testing MQTT
%% Cell type:code id:ec02bfa4-c311-493a-a1c8-892d0c4507ef tags:
``` python
# Install packages
!pip install wzl-mqtt --force-reinstall
```
%% Cell type:code id:d04c926c-354a-4386-b873-6be224066eec tags:
``` python
# importing some libraries
import json
import time
# docs: https://wzl-mq-public.pages.git-ce.rwth-aachen.de/iot/mqtt/ download: https://pypi.org/project/wzl-mqtt/
from wzl import mqtt
```
%% Cell type:markdown id:17b4d833 tags:
Add you credentials and see what messages are sent.
Here, MQTT automatically disconnects after 10 s
%% Cell type:code id:d6c37234-0a6a-4cca-a198-2ab6141d5e9d tags:
``` python
# username and password required to connect to the broker
MQTT_USER =
MQTT_PASSWORD =
# address, port and virtual host of the broker to connect to
# MQTT --> from within RWTH IP address ranges
# MQTT_PORT = 8883
# MQTT_BROKER = "mqtt.wzl-mq.rwth-aachen.de"
# define callback which will be executed when a message is received
def print_mqtt_message(topic, message):
print(topic, message)
# address, port and virtual host of the broker to connect to
# via websocket -> from everywhere outside RWTH IP address ranges
MQTT_BROKER = "iot.wzl-mq.rwth-aachen.de/mqtt-ws/"
MQTT_VHOST = "develop"
client = mqtt.MQTTSubscriber()
client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True, websocket=True)
# register the callback and subscribe topic
client.set_callback("PRINT", print_mqtt_message)
client.subscribe("#") # <- "#" is a wildcard to subscribe to EVERYTHING.
# start waiting loop to prevent program from exiting
for i in range(10):
try:
time.sleep(1)
except KeyboardInterrupt:
break
client.disconnect()
```
%% Cell type:markdown id:bf404b2c tags:
Try to get some messages into the dashboard!
- The root topic must be "training".
- The last topic must be "Var-message"
- The message must be a JSON string with a "value" field filled with a string.
%% Cell type:code id:3d73bab3-72a2-4e04-82bc-1f10758be78b tags:
``` python
# initialize publisher and connect to the broker
publisher = mqtt.MQTTPublisher()
# this should work everywhere
publisher.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True, websocket=True)
# this should work from within RWTH ip address ranges
# publisher.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True)
for i in range(10):
# publisher.publish(topic, message)
time.sleep(1)
publisher.disconnect()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment