Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MQTT Jupyter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
WZL-IQS-Public
Internet of Things
MQTT Jupyter
Commits
6eba655d
Commit
6eba655d
authored
10 months ago
by
Mark Pascal Sanders
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
99934f3a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
MQTT.ipynb
+149
-0
149 additions, 0 deletions
MQTT.ipynb
with
149 additions
and
0 deletions
MQTT.ipynb
0 → 100644
+
149
−
0
View file @
6eba655d
{
"cells": [
{
"cell_type": "markdown",
"id": "ffb6a1d8",
"metadata": {},
"source": [
"### Testing MQTT"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec02bfa4-c311-493a-a1c8-892d0c4507ef",
"metadata": {},
"outputs": [],
"source": [
"# Install packages\n",
"!pip install wzl-mqtt --force-reinstall"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d04c926c-354a-4386-b873-6be224066eec",
"metadata": {},
"outputs": [],
"source": [
"# importing some libraries\n",
"\n",
"import json\n",
"import time\n",
"\n",
"# docs: https://wzl-mq-public.pages.git-ce.rwth-aachen.de/iot/mqtt/ download: https://pypi.org/project/wzl-mqtt/\n",
"from wzl import mqtt"
]
},
{
"cell_type": "markdown",
"id": "17b4d833",
"metadata": {},
"source": [
"Add you credentials and see what messages are sent.\n",
"\n",
"Here, MQTT automatically disconnects after 10 s"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6c37234-0a6a-4cca-a198-2ab6141d5e9d",
"metadata": {},
"outputs": [],
"source": [
"# username and password required to connect to the broker\n",
"MQTT_USER = \n",
"MQTT_PASSWORD = \n",
"\n",
"# address, port and virtual host of the broker to connect to\n",
"# MQTT --> from within RWTH IP address ranges\n",
"# MQTT_PORT = 8883\n",
"# MQTT_BROKER = \"mqtt.wzl-mq.rwth-aachen.de\"\n",
"\n",
"\n",
"# define callback which will be executed when a message is received\n",
"def print_mqtt_message(topic, message):\n",
" print(topic, message)\n",
"\n",
"\n",
"# address, port and virtual host of the broker to connect to\n",
"# via websocket -> from everywhere outside RWTH IP address ranges\n",
"MQTT_BROKER = \"iot.wzl-mq.rwth-aachen.de/mqtt-ws/\"\n",
"MQTT_VHOST = \"develop\"\n",
"\n",
"client = mqtt.MQTTSubscriber()\n",
"client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True, websocket=True)\n",
"\n",
"# register the callback and subscribe topic\n",
"client.set_callback(\"PRINT\", print_mqtt_message)\n",
"client.subscribe(\"#\") # <- \"#\" is a wildcard to subscribe to EVERYTHING. \n",
"\n",
"# start waiting loop to prevent program from exiting\n",
"for i in range(10):\n",
" try:\n",
" time.sleep(1)\n",
" except KeyboardInterrupt:\n",
" break\n",
"client.disconnect()"
]
},
{
"cell_type": "markdown",
"id": "bf404b2c",
"metadata": {},
"source": [
"Try to get some messages into the dashboard!\n",
"\n",
"- The root topic must be \"training\". \n",
"- The last topic must be \"Var-message\"\n",
"- The message must be a JSON string with a \"value\" field filled with a string."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d73bab3-72a2-4e04-82bc-1f10758be78b",
"metadata": {},
"outputs": [],
"source": [
"# initialize publisher and connect to the broker\n",
"publisher = mqtt.MQTTPublisher()\n",
"\n",
"# this should work everywhere\n",
"publisher.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True, websocket=True)\n",
"\n",
"# this should work from within RWTH ip address ranges\n",
"# publisher.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True)\n",
"\n",
"\n",
"for i in range(10):\n",
" # publisher.publish(topic, message)\n",
" time.sleep(1)\n",
"\n",
"publisher.disconnect()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% 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
()
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment