-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-mqtt.py
122 lines (111 loc) · 4.34 KB
/
send-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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3
#
# Function to send a MQTT message. To be called from Domoticz rule
#
# Blochly Usage: Start Script: <optional path to script>/send-mqtt.sh
# with parameter(s): "<topic>" "<value>" "Y/N datetime stamp"
# - all arguements are optional and then defaults will apply
# - provide null "" if subsequent arguements
#
# Arguements:
# 1. topic - can prefix with "+" to append the topic to the default topic
# Examples: "MyTopic/SubTopicA" => "MyTopic/SubTopicA"
# "+SubTopicB" => "MyTopic/SubTopicA/SubTopicB"
# "" or omitted => "DefaultTopic/DefaultSubTopic"
# 2. value - can prefix with "+" to append the value to the default value
# Examples: "0" => "0"
# "+my extra value" => "DefaultValue my extra value"
# "" => "DefaultValue"
# 3. datetimestamp - set to Y if the Value is to be appended with a date and time stamp
# Format is according to the DATETIMEFORMAT setting
# Examples when Y: and DATETIMEFORMAT = " at %Y-%m-%d %H:%M:%S"
# "My value text" => "My value text at 2020-03-10 13:12:45"
#
#
# Future possible enhancements to include error handling, logging, QOS, retain arguement and SSL
# e.g. publish(topic, payload=None, qos=0, retain=False)
# For more debugging code see https://www.programcreek.com/python/example/106505/paho.mqtt.client.Client
#
# Version 2.1
# Added date & time stamp option
# Configutation setting are now in a send-mqtt-config.yml file
# Version 2.2
# +option applies to Value
# .upper() applied to datetimestamp
# AEC 2020-03-14
# License: GNU General Public License v3.0
import paho.mqtt.client as paho # pip3 install paho-mqtt
import sys
import os
from datetime import datetime
import yaml # apt-get install python-yaml
# get configuration settings
with open(os.path.join(sys.path[0], "send-mqtt.yml"), 'r') as configfile:
config = yaml.safe_load(configfile)
HOST = config['HOST']
PORT = config['PORT']
CLIENT_ID = config['CLIENT_ID']
TOPIC = config['TOPIC']
VALUE = config['VALUE']
QOS = config['QOS']
RETAIN = config['RETAIN']
SSL_CERT = config['SSL_CERT']
SSL_TLS = config['SSL_TLS']
DATETIMESTAMP = config['DATETIMESTAMP']
DATETIMEFORMAT = config['DATETIMEFORMAT']
def on_publish(client,userdata,result): # create function for callback
pass
class sendmqtt:
def __init__(self):
pass
def send(self, arguements):
# message text is passed as the 1st arguement
if len(arguements) >= 2:
topic = arguements[1]
if topic != "": # not null
if topic[0:1] == "+":
topic = TOPIC + '\\' + topic[1:]
else:
topic = arguements[1]
else:
topic = TOPIC
else:
topic = TOPIC
# value is passed as the 2nd arguement
if len(arguements) >= 3:
value = arguements[2]
if value != "": # not null
if value[0:1] == "+":
value = VALUE + ' ' + value[1:]
else:
value = arguements[2]
else:
value = VALUE
else:
value = VALUE
# datetimestamp is an optional 3rd user arguement
if len(arguements) >= 4:
if arguements[3].upper() == "Y":
datetimestamp = "Y"
else:
datetimestamp = "N"
else:
datetimestamp = DATETIMESTAMP.upper()
# apply datetimestamp option, either default or override option
if datetimestamp == "Y":
now = datetime.now()
value = value + now.strftime(DATETIMEFORMAT)
# create client object
client1 = paho.Client(CLIENT_ID)
# assign function to callback
client1.on_publish = on_publish
# establish connection
client1.connect(HOST,PORT)
# publish
retcode = client1.publish(topic,value)
print("Sent Topic: "+ topic + ", Value: " + value + ". Return was:" + str(retcode))
# create an instance of sendmqtt
sendobj = sendmqtt()
# now send the message
sendobj.send(sys.argv)
quit()