-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmqtt.py
63 lines (56 loc) · 1.81 KB
/
mqtt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
MQTT connect and publish
"""
import logging
import random
import time
from paho.mqtt import client as mqtt_client
def time_stamp():
"""
Return current time in YYYY-MM-DD hh:mm:ss
:return:
"""
return time.strftime("%Y-%m-%d %H:%M:%S")
def connect_mqtt(broker, port, username, password):
"""
Create an MQTT connection
:param broker: MQTT broker
:param port: MQTT broker port
:param username: MQTT username
:param password: MQTT password
:return:
"""
print(f"{time_stamp()}: 🔥 Connecting mqtt: {broker} on port: {port} as user: {username}")
client_id = f'solarman-{random.randint(0, 1000)}'
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.connect(broker, port)
return client
def publish(client, topic, msg, debug):
"""
Publish a message on a MQTT topic
:param client: Connect parameters
:param topic: MQTT topic
:param msg: Message payload
:return:
"""
if debug:
print(f"{time_stamp()}: 🔥 Publishing message to mqtt: {msg} on topic: {topic}")
result = client.publish(topic, msg)
status = result[0]
if status == 0:
print(f"{time_stamp()}: 🔥 MQTT message published to topic: {topic}")
else:
print(f"{time_stamp()}: 😡 Failed to send mqtt message to topic: {topic}")
def message(config, topic, msg, debug):
"""
MQTT Connect and send
:param config: Broker configuration
:param topic: MQTT topic
:param msg: Message payload
"""
try:
client = connect_mqtt(config["broker"], config["port"], config["username"], config["password"])
publish(client, topic, msg, debug)
except Exception as error: # pylint: disable=broad-except
print(f"{time_stamp()}: 😡 Unable to connect mqtt: {str(error)}")