From 2a8d0e3792bd5012b65e14a1f53ee2d146464529 Mon Sep 17 00:00:00 2001 From: Billy Pi Date: Fri, 23 Feb 2018 19:49:31 +0000 Subject: [PATCH] first commit --- sql/sqlwriter.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 sql/sqlwriter.py diff --git a/sql/sqlwriter.py b/sql/sqlwriter.py new file mode 100644 index 0000000..87ff06e --- /dev/null +++ b/sql/sqlwriter.py @@ -0,0 +1,55 @@ +from time import gmtime, strftime +import paho.mqtt.client as mqtt +import sqlite3 + +temperature_topic = "temperature" +humidity_topic = "humidity" +dbFile = "esp8266.db" + +dataTuple = [-1,-1] + +# The callback for when the client receives a CONNACK response from the server. +def on_connect(client, userdata, flags, rc): + print("Connected with result code "+str(rc)) + + # Subscribing in on_connect() means that if we lose the connection and + # reconnect then subscriptions will be renewed. + client.subscribe(temperature_topic) + client.subscribe(humidity_topic) + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + theTime = strftime("%Y-%m-%d %H:%M:%S", gmtime()) + + result = (theTime + "\t" + str(msg.payload)) + print(msg.topic + ":\t" + result) + if (msg.topic == temperature_topic): + dataTuple[0] = str(msg.payload) + if (msg.topic == humidity_topic): + dataTuple[1] = str(msg.payload) + #return + if (dataTuple[0] != -1 and dataTuple[1] != -1): + writeToDb(theTime, dataTuple[0], dataTuple[1]) + return + +def writeToDb(theTime, temperature, humidity): + conn = sqlite3.connect(dbFile) + c = conn.cursor() + print "Writing to db..." + c.execute("INSERT INTO climate VALUES (?,?,?)", (theTime, temperature, humidity)) + conn.commit() + + global dataTuple + dataTuple = [-1, -1] + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message + +client.connect("raspberrypi", 1883, 60) + +# Blocking call that processes network traffic, dispatches callbacks and +# handles reconnecting. +# Other loop*() functions are available that give a threaded interface and a +# manual interface. +client.loop_forever()