3 import paho.mqtt.client as paho
5 from ConfigParser import ConfigParser
7 from pprint import pprint
9 from dateutil.parser import parser
12 def on_message(mosq, obj, msg):
14 payload=json.loads(msg.payload)
15 timestamp=tparser.parse(payload['Time'])
16 for sensor_type in payload:
17 if sensor_type != 'Time':
18 sensor_data=payload[sensor_type]
19 for param in sensor_data:
21 value=sensor_data[param]
23 c.execute('CALL meteo.submit_mqtt(%s,%s,%s,%s,NULL)', (topic,sensor_type,param,value))
25 print topic,sensor_type,param,value
27 print "Failed to submit data"
34 select topic from mqtt_topics
44 global client,database
46 conffile = sys.argv[1]
48 config = ConfigParser()
49 config.add_section('mqtt')
50 # set defaults for anonymous auth
51 config.set('mqtt', 'username', '')
52 config.set('mqtt', 'password', '')
53 config.set('mqtt', 'port', '1883')
56 mqtt_server = config.get('mqtt', 'server')
57 mqtt_port = config.getint('mqtt', 'port')
58 mqtt_username = config.get('mqtt', 'username')
59 mqtt_password = config.get('mqtt', 'password')
61 mysql_server = config.get('mysql', 'server')
62 mysql_username = config.get('mysql','username')
63 mysql_password = config.get('mysql','password')
64 mysql_db = config.get('mysql','db')
66 client = paho.Client('weather')
67 client.username_pw_set(mqtt_username, mqtt_password)
68 client.on_message=on_message
69 client.connect(mqtt_server, port=mqtt_port)
71 database = MySQLdb.connect(host=mysql_server,user=mysql_username,passwd=mysql_password,db=mysql_db,use_unicode=True,connect_timeout=10)
72 database.set_character_set('utf8')
74 c.execute('SET NAMES utf8;')
77 for topic in Topics():
78 topics.append((topic[0].encode('UTF-8'),1))
80 client.subscribe(topics)