From 6eba655d683c1bb4a9d7d494b6274be6db940052 Mon Sep 17 00:00:00 2001
From: Mark Pascal Sanders <mark.sanders@rwth-aachen.de>
Date: Thu, 29 Aug 2024 18:13:22 +0200
Subject: [PATCH] Upload New File

---
 MQTT.ipynb | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 149 insertions(+)
 create mode 100644 MQTT.ipynb

diff --git a/MQTT.ipynb b/MQTT.ipynb
new file mode 100644
index 0000000..02a4231
--- /dev/null
+++ b/MQTT.ipynb
@@ -0,0 +1,149 @@
+{
+ "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
+}
-- 
GitLab