-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.py
60 lines (48 loc) · 1.76 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
54
55
56
57
58
59
60
import paho.mqtt.publish as pub
from socket import *
from my_libs import *
setup_logging()
log = logging.getLogger("<MQTT>")
####################################################
# For publishing sensor data to iot.eclipse.org
####################################################
def publish(topic, message):
try:
msgs = [{'topic': "paho/test/iotBUET/"+topic, 'payload': message},
("paho/test/multiple", "multiple 2", 0, False)]
pub.multiple(msgs, hostname="iot.eclipse.org")
return True
except gaierror:
log.error('[MQTT] Publish ERROR.')
eprint ("[MQTT] Publish ERROR." )
return False
def publish_packet(message):
try:
msgs = [{'topic': "paho/test/iotBUET/bulk/", 'payload': message},
("paho/test/multiple", "multiple 2", 0, False)]
pub.multiple(msgs, hostname="iot.eclipse.org")
return True
except gaierror:
log.error('[MQTT] Publish ERROR.')
eprint ("[MQTT] Publish ERROR." )
return False
def publish_packet_raw(message):
try:
msgs = [{'topic': "paho/test/iotBUET/bulk_raw/", 'payload': message},
("paho/test/multiple", "multiple 2", 0, False)]
pub.multiple(msgs, hostname="iot.eclipse.org")
return True
except gaierror:
log.error('[MQTT] Publish ERROR.')
eprint ("[MQTT] Publish ERROR." )
return False
def test_connection():
try:
msgs = [{'topic': "paho/test/iotTEST/" },
("paho/test/multiple", "multiple 2", 0, False)]
pub.multiple(msgs, hostname="iot.eclipse.org")
return True
except gaierror:
log.error('[MQTT] Connection Test ERROR.')
eprint ("[MQTT] Connection Test ERROR." )
return False