-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_mqtt_messages.py
48 lines (37 loc) · 1.16 KB
/
send_mqtt_messages.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
import json
import logging
import sys
import paho.mqtt.publish as mqtt_publish
import mqtt_config
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
def publish(topic, **payload):
logger.info('publish topic=%s payload=%s', topic, payload)
mqtt_publish.single(topic,
payload=json.dumps(payload),
qos=1,
retain=True,
hostname=mqtt_config.hostname,
auth=mqtt_config.auth,
port=mqtt_config.port,
client_id='')
def repl(topic):
while True:
try:
message = input('> ')
except EOFError:
break
publish(topic, message=message)
def main(topic='speak'):
# logger.setLevel(logging.INFO)
if not mqtt_config.hostname:
print('At least one of these must be set:',
', '.join(mqtt_config.MQTT_ENV_VARS), file=sys.stderr)
sys.exit(1)
if len(sys.argv) > 1:
message = sys.argv[1]
publish(topic, message=message)
else:
repl(topic)
if __name__ == '__main__':
main()