Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Python-MQTT Library
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
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
Show more breadcrumbs
WZL-IQS-Public
Internet of Things
Python-MQTT Library
Commits
6360fd5d
Commit
6360fd5d
authored
2 years ago
by
Matthias Stefan Bodenbenner
Browse files
Options
Downloads
Patches
Plain Diff
updated sample scripts to newest library version
parent
5b713523
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
sample/publish.py
+5
-8
5 additions, 8 deletions
sample/publish.py
sample/receive.py
+5
-12
5 additions, 12 deletions
sample/receive.py
src/mqtt/__init__.py
+5
-6
5 additions, 6 deletions
src/mqtt/__init__.py
src/mqtt/logger.py
+6
-3
6 additions, 3 deletions
src/mqtt/logger.py
with
21 additions
and
29 deletions
sample/publish.py
+
5
−
8
View file @
6360fd5d
...
@@ -10,24 +10,21 @@ MQTT_USER = ""
...
@@ -10,24 +10,21 @@ MQTT_USER = ""
MQTT_PASSWORD
=
""
MQTT_PASSWORD
=
""
MQTT_BROKER
=
"
127.0.0.1
"
MQTT_BROKER
=
"
127.0.0.1
"
MQTT_PORT
=
1883
MQTT_VHOST
=
"
/
"
MQTT_VHOST
=
"
/
"
WEBSOCKET
=
False
SSL
=
True
## To connect to the central MQTT-Broker of MQ-MS use the settings below.
topic
=
"
test
"
# set topic to publish according to MQTT syntax!
## Ask Mark Sanders (sdr) for personal credentials.
# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de"
# MQTT_PORT = 1883
# MQTT_VHOST = "metrology"
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
client
=
mqtt
.
MQTTPublisher
()
client
=
mqtt
.
MQTTPublisher
()
client
.
connect
(
MQTT_BROKER
,
MQTT_
PORT
,
MQTT_
VHOST
+
"
:
"
+
MQTT_USER
,
MQTT_PASSWORD
)
client
.
connect
(
MQTT_BROKER
,
MQTT_
USER
,
MQTT_
PASSWORD
,
vhost
=
MQTT_VHOST
,
ssl
=
SSL
,
websocket
=
WEBSOCKET
)
while
True
:
while
True
:
try
:
try
:
message
=
json
.
dumps
({
"
value
"
:
[
random
.
uniform
(
0
,
5
)
for
i
in
range
(
3
)],
"
timestamp
"
:
datetime
.
datetime
.
utcnow
().
isoformat
()
+
"
Z
"
,
message
=
json
.
dumps
({
"
value
"
:
[
random
.
uniform
(
0
,
5
)
for
i
in
range
(
3
)],
"
timestamp
"
:
datetime
.
datetime
.
utcnow
().
isoformat
()
+
"
Z
"
,
"
covariance
"
:
[[
2
,
0
,
0
],
[
0
,
2
,
0
],
[
0
,
0
,
0
]],
"
nonce
"
:
str
(
uuid
.
uuid4
()),
"
hash
"
:
None
,
"
unit
"
:
"
MTR
"
})
"
covariance
"
:
[[
2
,
0
,
0
],
[
0
,
2
,
0
],
[
0
,
0
,
0
]],
"
nonce
"
:
str
(
uuid
.
uuid4
()),
"
hash
"
:
None
,
"
unit
"
:
"
MTR
"
})
client
.
publish
(
"
channel-001
"
,
message
.
encode
(
"
utf-8
"
),
0
)
client
.
publish
(
topic
,
message
.
encode
(
"
utf-8
"
),
0
)
time
.
sleep
(
1
)
time
.
sleep
(
1
)
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
break
break
This diff is collapsed.
Click to expand it.
sample/receive.py
+
5
−
12
View file @
6360fd5d
...
@@ -2,32 +2,25 @@ import time
...
@@ -2,32 +2,25 @@ import time
from
wzl
import
mqtt
from
wzl
import
mqtt
logger
=
mqtt
.
root_logger
.
get
(
'
Receiver
'
)
MQTT_USER
=
""
MQTT_USER
=
""
MQTT_PASSWORD
=
""
MQTT_PASSWORD
=
""
MQTT_BROKER
=
"
127.0.0.1
"
MQTT_BROKER
=
"
127.0.0.1
"
MQTT_PORT
=
1883
MQTT_VHOST
=
"
/
"
MQTT_VHOST
=
"
/
"
WEBSOCKET
=
False
SSL
=
True
## To connect to the central MQTT-Broker of MQ-MS use the settings below.
topic
=
"
test
"
# set topic to subscribe according to MQTT syntax!
## Ask Mark Sanders (sdr) for personal credentials.
# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de"
# MQTT_PORT = 1883
# MQTT_VHOST = "metrology"
topic
=
"
#
"
# set topic to subscribe according to MQTT syntax!
qos
=
0
# set QoS according to MQTT specifications!
qos
=
0
# set QoS according to MQTT specifications!
def
print_mqtt_message
(
topic
,
message
):
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__
"
:
if
__name__
==
"
__main__
"
:
client
=
mqtt
.
MQTTSubscriber
()
client
=
mqtt
.
MQTTSubscriber
()
client
.
connect
(
MQTT_BROKER
,
MQTT_
PORT
,
MQTT_
VHOST
+
"
:
"
+
MQTT_USER
,
MQTT_PASSWORD
)
client
.
connect
(
MQTT_BROKER
,
MQTT_
USER
,
MQTT_
PASSWORD
,
vhost
=
MQTT_VHOST
,
ssl
=
SSL
,
websocket
=
WEBSOCKET
)
client
.
set_callback
(
"
PRINT
"
,
print_mqtt_message
)
client
.
set_callback
(
"
PRINT
"
,
print_mqtt_message
)
client
.
subscribe
(
topic
,
qos
)
client
.
subscribe
(
topic
,
qos
)
...
...
This diff is collapsed.
Click to expand it.
src/mqtt/__init__.py
+
5
−
6
View file @
6360fd5d
from
.exceptions
import
MQTTException
from
.exceptions
import
ConnectionError
from
.exceptions
import
SubscriptionError
from
.exceptions
import
CallbackError
from
.exceptions
import
PublishError
from
.client
import
MQTTPublisher
from
.client
import
MQTTPublisher
from
.client
import
MQTTSubscriber
from
.client
import
MQTTSubscriber
from
.exceptions
import
CallbackError
from
.exceptions
import
ConnectionError
from
.exceptions
import
MQTTException
from
.exceptions
import
PublishError
from
.exceptions
import
SubscriptionError
from
.logger
import
Logger
from
.logger
import
Logger
This diff is collapsed.
Click to expand it.
src/mqtt/logger.py
+
6
−
3
View file @
6360fd5d
...
@@ -11,8 +11,8 @@ class Logger(object):
...
@@ -11,8 +11,8 @@ class Logger(object):
"""
Creates a basic logger for console/terminal and optional file output.
"""
Creates a basic logger for console/terminal and optional file output.
Args:
Args:
filename: Name of the file the output should be logged to, file format can be om
m
ited.
filename: Name of the file the output should be logged to, file format can be omi
t
ted.
Current timestamp and file format is automatically appen
e
d to the given filename.
Current timestamp and file format is automatically append to the given filename.
If no, filename is given, the output is written to console/terminal only.
If no, filename is given, the output is written to console/terminal only.
path: Optional path to the logging file. If omitted, file is created at the current working directory.
path: Optional path to the logging file. If omitted, file is created at the current working directory.
level: Specifies which information should be logged as specified by the python logging module.
level: Specifies which information should be logged as specified by the python logging module.
...
@@ -63,7 +63,7 @@ class Logger(object):
...
@@ -63,7 +63,7 @@ class Logger(object):
Returns: A basic python logger with the given name.
Returns: A basic python logger with the given name.
"""
"""
return
logging
.
getLogger
(
name
)
return
self
.
root_logger
.
getChild
(
name
)
def
set_logging_level
(
self
,
level
:
int
,
target
:
str
=
''
)
->
None
:
def
set_logging_level
(
self
,
level
:
int
,
target
:
str
=
''
)
->
None
:
"""
Sets the logging level.
"""
Sets the logging level.
...
@@ -84,3 +84,6 @@ class Logger(object):
...
@@ -84,3 +84,6 @@ class Logger(object):
self
.
console_handler
.
setLevel
(
level
)
self
.
console_handler
.
setLevel
(
level
)
if
self
.
_filename
is
not
None
:
if
self
.
_filename
is
not
None
:
self
.
file_handler
.
setLevel
(
level
)
self
.
file_handler
.
setLevel
(
level
)
self
.
root_logger
.
setLevel
(
level
)
self
.
root_logger
.
addHandler
(
self
.
file_handler
)
self
.
root_logger
.
addHandler
(
self
.
console_handler
)
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