forked from render-examples/flask-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpub.py
More file actions
39 lines (27 loc) · 1.18 KB
/
Copy pathpub.py
File metadata and controls
39 lines (27 loc) · 1.18 KB
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
#A simple testing script for an MQTT sub to test the publisher ig idk
import paho.mqtt.client as mqtt
import sys, os
client = mqtt.Client()
print("cmdargs : " + str(sys.argv))
print("progname = " + os.path.basename(__file__)) #Get cmd line info
if (len(sys.argv) != 3):
print("ERROR : Usage : ./pub <topic> <payload>") ##ensure topic and payload provided
exit()
# mqtthost = "broker.mqttdashboard.com" #Broker URL
# mqtthost = "mqtt.eclipseprojects.io"
mqtthost = "test.mosquitto.org"
if (client.connect(mqtthost, 1883, 30) != 0):
print("Could not connect to " + mqtthost) #Setup connection to broker
exit()
else:
print("Connection successful at " + mqtthost)
mytopic = "sosa/test"
mytopic = sys.argv[1] #Read topic name from cmdline
print("Topic : " + mytopic)
mypayload = "default pub message"
mypayload = sys.argv[2] #Read payload from cmdline
print("Payload = " + mypayload)
myqos = 1
print("QOS = " + str(myqos)) #QOS = at least once ( 1 )
info = client.publish(mytopic, mypayload, myqos) #Publish message
client.disconnect() #Disconnect from broker