-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmqtt.py
More file actions
59 lines (47 loc) · 1.63 KB
/
mqtt.py
File metadata and controls
59 lines (47 loc) · 1.63 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import paho.mqtt.client as mqtt
import os, urlparse
import serial
#ser = serial.Serial('com15',9600)
# Define event callbacks
def on_connect(client, userdata, flags, rc):
print("rc: " + str(rc))
def on_message(client, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
#ser.write(str(msg.payload))
def on_publish(client, obj, mid):
print("mid: " + str(mid))
def on_subscribe(client, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(client, obj, level, string):
print(string)
try:
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
#mqttc.on_log = on_log
# Parse CLOUDMQTT_URL (or fallback to localhost)
#url_str = os.environ.get('CLOUDMQTT_URL', 'mqtt://localhost:1883')
#url = urlparse.urlparse(url_str)
#topic = url.path[1:] or 'test'
# Connect
mqttc.username_pw_set("username", "password")
mqttc.connect('server', port, 60)
# Start subscribe, with QoS level 0
a = mqttc.subscribe("/frommothership", 0)
b = mqttc.subscribe("lat",0);
b = mqttc.subscribe("project",0);
# Publish a message
#mqttc.publish("/frommothership","addresses" )
# Continue the network loop, exit when an error occurs
rc = 0
while True :
#mqttc.publish("/frommothership", "my message")
rc = mqttc.loop()
print("rc: " + str(rc))
except:
exit
#ser.close()