Cameteo/python/mqtt2sql.py

93 lines
2.4 KiB
Python
Raw Normal View History

2017-07-26 15:51:58 +02:00
#!/usr/bin/python3
# Pérenisation de données arrivant sur un MQTT
# vers une base de données MariaDb/MySQL
# Auteur : Arofarn
# v0.1
#################
# Configuration #
#################
#MQTT Broker
mqtt_host = "aro-yuno"
mqtt_port = 1883
mqtt_client_id = "mqtt2sql"
#mqtt_auth = { 'username' : 'arofarn', 'password' : 'xxx' }
qos=1
topic = "huzzah0/#"
#MySQL database
sql_host = "localhost"
#sql_port =
sql_base = "iot_test"
sql_user = "arofarn"
#sql_pass = "password"
########
# MAIN #
########
import paho.mqtt.client as mqtt
from datetime import datetime
import json
import pymysql.cursors
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(topic)
def on_message(client, userdata, msg):
top=msg.topic[len(topic)-1:].strip()
subtopics = top.split("/")
payload = msg.payload.decode()
val = json.loads(payload)
print("%s : %s %s (%s)" % (val['type'], val['value'], val['unit'], subtopics[0]))
with connection.cursor() as cursor:
sql = "INSERT INTO `data` (`valdate`, `value`, `type_id`, `sensor_id`) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (datetime.strptime(val['date'], "%d/%m/%Y %H:%M:%S"), float(val['value']), val['type'], subtopics[0]))
connection.commit()
def on_message_date(client, userdata, msg):
payload = msg.payload.decode()
try:
d = datetime.strptime(payload, "%d/%m/%Y %H:%M:%S")
print("Date : %s" % d)
except:
print("Date mal formatée : %s" % payload)
def on_disconnect(client, userdata, msg):
if msg != 0:
print("Déconnexion imprévu : %s" % msg)
connection.close()
connection = pymysql.connect(host=sql_host,
user=sql_user,
#password=sql_pass,
db=sql_base,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
########
# Main #
########
#Connect to MQTT broker and loop...
mqtt_client = mqtt.Client(mqtt_client_id, clean_session=False)
#mqtt_client.username_pw_set(mqtt_auth['username'], mqtt_auth['password'])
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.on_disconnect = on_disconnect
mqtt_client.message_callback_add("huzzah0/NTP/date", on_message_date)
mqtt_client.connect(mqtt_host, mqtt_port, 60)
mqtt_client.loop_forever()