forked from lechk82/solarman-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.py
53 lines (45 loc) · 1.31 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
"""
MQTT connect and publish
"""
import logging
import random
from paho.mqtt import client as mqtt_client
logging.basicConfig(level=logging.INFO)
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:
"""
client_id = f'solarmanpv-mqtt-{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):
"""
Publish a message on a MQTT topic
:param client: Connect parameters
:param topic: MQTT topic
:param msg: Message payload
:return:
"""
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
logging.debug("Send %s to %s", msg, topic)
else:
logging.error("Failed to send message to topic %s", topic)
def message(config, topic, msg):
"""
MQTT Connect and send
:param config: Broker configuration
:param topic: MQTT topic
:param msg: Message payload
"""
client = connect_mqtt(config["broker"], config["port"], config["username"], config["password"])
publish(client, topic, msg)