-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscriber.py
34 lines (27 loc) · 1.19 KB
/
subscriber.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
import paho.mqtt.client as paho
import sqlite3
from sqlitehandler import write_to_db
#from mongohandler import write_to_db
def on_message(mosq, obj, msg):
print ("%-20s %d %s" % (msg.topic, msg.qos, msg.payload))
mosq.publish('pong', 'ack', 0)
def on_publish(mosq, obj, mid):
pass
if __name__ == '__main__':
broker = "127.0.0.1"
port = 1883
client = paho.Client()
client.on_message = on_message
client.on_publish = on_publish
# Not working with certificates yet!
#client.tls_set('root.ca', certfile='c1.crt', keyfile='c1.key')
client.connect("127.0.0.1", 1883)
client.subscribe("dummysubscription/#", 0)
while client.loop() == 0:
pass
#### ####
# #
# HINT: When writing to db replace the "/" in the topic names with for example "_", #
# as sqlite doesn't support "/" in table's names #
# #
#### ####