Skip to content
Snippets Groups Projects
Commit 39680034 authored by Matthias Stefan Bodenbenner's avatar Matthias Stefan Bodenbenner
Browse files

2.5.4 - fixed usage of a randomized client_id, so that the same credentials...

2.5.4 - fixed usage of a randomized client_id, so that the same credentials can be used for multiple clients in parallel
parent 28fdb268
No related branches found
No related tags found
No related merge requests found
Pipeline #255642 passed
# WZL-MQTT
Current stable version: 2.5.3
Current stable version: 2.5.4
## Documentation
......@@ -79,6 +79,9 @@ while True:
## Changelog
**2.5.4** - 2023-04-18
- fixed usage of a randomized client_id, so that the same credentials can be used for multiple clients in parallel
**2.5.3** - 2023-04-15
- if the port is specified as string it is also correctly processed now
- improved user feedback in case of successful connection to the broker
......
# WZL-MQTT
Current stable version: 2.5.3
Current stable version: 2.5.4
## Documentation
......@@ -90,9 +90,13 @@ the European Union’s Horizon 2020 research and innovation programme and the EM
## Changelog
**2.5.4** - 2023-04-18
- fixed usage of a randomized client_id, so that the same credentials can be used for multiple clients in parallel
**2.5.3** - 2023-04-15
- if the port is specified as string it is also correctly processed now
- improved logging output in case of successful connection to the broker
- improved user feedback in case of successful connection to the broker
- improved user feedback if the combination of port, ssl and websocket is invalid
**2.5.2** - 2023-03-29
- client handles slash in at the end of prefix and the beginning of topic to avoid multiple consecutive slashes correctly now
......
......@@ -4,7 +4,7 @@ with open("PyPI-README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setup(name='wzl-mqtt',
version='2.5.3',
version='2.5.4',
url='https://git-ce.rwth-aachen.de/wzl-mq-public/iot/mqtt/',
project_urls={
"Bug Tracker": "https://git-ce.rwth-aachen.de/wzl-mq-public/iot/mqtt/-/issues",
......@@ -21,6 +21,6 @@ setup(name='wzl-mqtt',
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
install_requires=['paho-mqtt==1.5.1'],
install_requires=['paho-mqtt~=1.5.1'],
python_requires='>=3.6',
zip_safe=False)
......@@ -43,15 +43,16 @@ class MQTTClient:
self._logger = logger.get(
'MQTTClient') if logger is not None else Logger().get('MQTTClient')
if username in MQTTClient.instances:
self.name = username if username is not None else str(uuid.uuid4())
if self.name in MQTTClient.instances:
self._logger.error(
"MQTT Client {} already exists!".format(username))
raise IndexError("MQTT Client {} already exists!".format(username))
"MQTT Client {} already exists!".format(self.name))
raise IndexError("MQTT Client {} already exists!".format(self.name))
MQTTClient.instances[username] = self
self.name = username if username is not None else uuid.uuid4()
MQTTClient.instances[self.name] = self
self.prefix = prefix
self._client = mqtt.Client(username, *args, **kwargs)
self._client = mqtt.Client(self.name, *args, **kwargs)
self._client.on_connect = self._on_connect
self._client.on_disconnect = self._on_disconnect
......
import MQTT
import json
from datetime import datetime
import time
import random
MQTT_USER = "test"
MQTT_PASSWORD = "test"
MQTT_BROKER = "localhost"#"134.130.225.37"#
MQTT_PORT = 1883
MQTT_VHOST = "/"
client = MQTT.Client(MQTT_USER)
client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+ MQTT_USER, MQTT_PASSWORD)
pos = [0,0,0]
i=0
while True:
i+=1
client.publish(
topic="quatsch"+"/VAR-position",
message=json.dumps({
"value": pos,
"timestamp": datetime.utcnow().isoformat()+"Z",
"covariance": None,
"nonce": {},
"hash": None,
"unit": "P1"
}),
qos=0,
)
print(i)
pos = [max(0,min(10,value + random.uniform(-0.1,0.1))) for value in pos]
print(pos)
time.sleep(1)
\ No newline at end of file
import asyncio
import datetime
# For sample data
import json
import logging
import time
import uuid
# import matplotlib.pyplot as plt
import numpy
from src import mqtt
### Ask for settings and individual credentials ###
MQTT_PUBLISHER_USER = ""
MQTT_PUBLISHER_PASSWORD = ""
MQTT_RECEIVER_USER = ""
MQTT_RECEIVER_PASSWORD = ""
### to connect to the central MQTT-Broker of MQ-MS use the following settings:
MQTT_BROKER = "localhost" # wzl-mbroker01.wzl.rwth-aachen.de"
MQTT_PORT = 1883
MQTT_VHOST = "" # "metrology"
### Ask for settings and individual credentials ###
topic = "#" # set topic to subscribe according to MQTT syntax!
qos = 0 # set QoS according to MQTT specifications!
counter = 0
starts = []
delay = []
def print_mqtt_message(topic, message):
global starts, delay
stop = time.perf_counter()
counter = json.loads(message.decode("utf-8"))['value']
for count, start in starts:
if count == counter:
delay += [stop - start]
break
# print("{}\r\n### {} ###\r\n{}\r\n".format(datetime.datetime.utcnow().isoformat() + "Z", topic, message.decode("utf-8")))
if __name__=="__main__":
console_log = logging.StreamHandler()
console_log.setFormatter(mqtt.formatter)
mqtt.logger.addHandler(console_log)
mqtt.logger.setLevel(logging.INFO)
pclient = mqtt.MQTTPublisher(MQTT_PUBLISHER_USER)
pclient.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+ MQTT_PUBLISHER_USER, MQTT_PUBLISHER_PASSWORD)
client = mqtt.MQTTSubscriber(MQTT_RECEIVER_USER)
client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_RECEIVER_USER, MQTT_RECEIVER_PASSWORD)
client.set_callback("PRINT", print_mqtt_message)
client.subscribe(topic, qos)
loop = asyncio.get_event_loop()
while counter < 1e2:
try:
message = json.dumps({"value" : counter, "timestamp" : datetime.datetime.utcnow().isoformat() + "Z", "covariance": [[2,0,0], [0,2,0], [0,0,0]], "nonce" : str(uuid.uuid4()), "hash" : None, "unit" : "MTR"})
starts += [(counter, time.perf_counter())]
pclient.publish(MQTT_PUBLISHER_USER + "/channel-001",message.encode("utf-8"), 0)
time.sleep(0.1)
counter += 1
except KeyboardInterrupt:
break
delay = numpy.array(delay)
print("Nr. messages: {}".format(delay.size))
print("Average delay: {} ms".format(delay.mean() * 1e3))
print("Std. Dev. delay: {} ms".format(delay.std() * 1e3))
# n, bins, patches = plt.hist(delay, 50, density=True, facecolor='g', alpha=0.75)
# plt.xlabel('Delay')
# plt.ylabel('Count')
# plt.text(0.01, 50, r'$\mu={},\ \sigma={}$'.format(delay.mean() * 1e3, delay.std() * 1e3))
# plt.show()
import src.mqtt as mqtt
import json
from datetime import datetime
import random
import time
MQTT_USER = "test"
MQTT_PASSWORD = "test"
MQTT_BROKER = "localhost"#"134.130.225.37"#
MQTT_PORT = 1883
MQTT_VHOST = "/"
client = mqtt.client.MQTTPublisher(MQTT_USER)
client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+ MQTT_USER, MQTT_PASSWORD)
test=0
i=0
while True:
i+=1
test += random.random()-0.5
client.publish(
topic="quatsch"+"/OBJ-Environment/OBJ-0/VAR-TEST",
message=json.dumps({
"value": [float(test)],
"timestamp": datetime.utcnow().isoformat()+"Z",
"covariance": None,
"nonce": {},
"hash": None,
"unit": "P1"
}),
qos=0,
)
print(i)
time.sleep(1)
\ No newline at end of file
......@@ -8,11 +8,11 @@ from src import mqtt
### Ask for settings and individual credentials ###
MQTT_USER = "bdn-aafdecf0-a14c-4cb5-bb08-3844399e0a25"
MQTT_PASSWORD = "azesR4Q8~M7UBKh<7S~d\"NN-|)i9:Le["
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_BROKER = "mqtt.wzl-mq.rwth-aachen.de"
MQTT_PORT = "0"
MQTT_PORT = 8883
MQTT_VHOST = "metrology"
### to connect to the central MQTT-Broker of MQ-MS use the following settings:
......@@ -24,7 +24,7 @@ MQTT_VHOST = "metrology"
if __name__ == "__main__":
client = mqtt.MQTTPublisher(prefix="bdn-212d8419-c75c-471f-8ea5-f3a5c19ac42e")
client = mqtt.MQTTPublisher()
client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT, ssl=True)
while True:
......
......@@ -2,7 +2,7 @@ import time
from src import mqtt
logger = mqtt.root_logger.get('Receiver')
# logger = mqtt.root_logger.get('Receiver')
### Ask for settings and individual credentials ###
......@@ -13,22 +13,17 @@ MQTT_BROKER = "mqtt.wzl-mq.rwth-aachen.de"
MQTT_PORT = 8883
MQTT_VHOST = "metrology"
## to connect to the central MQTT-Broker of MQ-MS use the following settings:
# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de"
# MQTT_PORT = 1883
# MQTT_VHOST = "metrology"
### Ask for settings and individual credentials ###
topic = "#" # set topic to subscribe according to MQTT syntax!
qos = 0 # set QoS according to MQTT specifications!
def print_mqtt_message(topic, message):
logger.info("### {} ###\r\n{}\r\n".format(topic, message.decode("utf-8")))
print("### {} ###\r\n{}\r\n".format(topic, message.decode("utf-8")))
if __name__=="__main__":
client = mqtt.MQTTSubscriber(prefix="bdn-212d8419-c75c-471f-8ea5-f3a5c19ac42e")
client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_USER, vhost=MQTT_VHOST, MQTT_PASSWORD)
client = mqtt.MQTTSubscriber()
client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True, port=MQTT_PORT)
client.set_callback("PRINT", print_mqtt_message)
# client.subscribe(topic, qos)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment