3 import paho.mqtt.client as paho
5 from ConfigParser import ConfigParser
7 from pprint import pprint
9 def on_message(mosq, obj, msg):
14 c.execute('CALL meteo.submit_mqtt(%s,%s,NULL)', (topic,payload))
18 print "Failed to submit data"
25 select topic from mqtt_topics
35 global client,database
37 conffile = sys.argv[1]
39 config = ConfigParser()
40 config.add_section('mqtt')
41 # set defaults for anonymous auth
42 config.set('mqtt', 'username', '')
43 config.set('mqtt', 'password', '')
44 config.set('mqtt', 'port', '1883')
47 mqtt_server = config.get('mqtt', 'server')
48 mqtt_port = config.getint('mqtt', 'port')
49 mqtt_username = config.get('mqtt', 'username')
50 mqtt_password = config.get('mqtt', 'password')
52 mysql_server = config.get('mysql', 'server')
53 mysql_username = config.get('mysql','username')
54 mysql_password = config.get('mysql','password')
55 mysql_db = config.get('mysql','db')
57 client = paho.Client('weather')
58 client.username_pw_set(mqtt_username, mqtt_password)
59 client.on_message=on_message
60 client.connect(mqtt_server, port=mqtt_port)
62 database = MySQLdb.connect(host=mysql_server,user=mysql_username,passwd=mysql_password,db=mysql_db,use_unicode=True,connect_timeout=10)
63 database.set_character_set('utf8')
65 c.execute('SET NAMES utf8;')
68 for topic in Topics():
69 topics.append((topic[0].encode('UTF-8'),1))
71 client.subscribe(topics)