-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_weather.py
43 lines (36 loc) · 1.2 KB
/
mqtt_weather.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
35
36
37
38
39
40
41
42
#!/usr/bin/python3
import sqlite3
from sqlite3 import Error
import datetime
import time
import paho.mqtt.client as mqttClient
import json
Connected = False
broker_address= "10.0.0.11"
port = 1883
client = mqttClient.Client("weatherdata")
#client.connect(broker_address,port=port)
database = r"/var/lib/weewx/weewx.sdb"
try:
conn = sqlite3.connect(database)
except Error as e:
pass
#querystring = "SELECT dateTime, barometer,inTemp,extraTemp2,outTemp,pm2_5,pm10_0 from archive where dateTime>1644172560"
querystring = "SELECT dateTime, barometer,inTemp,extraTemp2,outTemp,pm2_5,pm10_0 from archive where dateTime>?"
lasttime = 1645235000
while True:
cur = conn.cursor()
column_name=[col[0] for col in cur]
cur.execute(querystring,(str(lasttime),))
try:
row = [ dict(line) for line in [zip([ column[0] for column in cur.description], row) for row in cur.fetchall()] ]
print ("------")
payload = json.dumps(row[len(row)-1])
print (payload)
client.connect(broker_address,port=port,keepalive=30)
client.publish("weather/data",payload)
lasttime = (row[len(row)-1]["dateTime"])
print (lasttime)
except:
print ("error")
time.sleep(120)