script to sync oko weather station to external weather provider like openweather

This commit is contained in:
klaute 2020-03-24 22:24:40 +01:00
parent 35e1234798
commit bf7ac4b0df
2 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,97 @@
import logging
import time
import datetime
import urllib.parse
import http.client
from influxdb import InfluxDBClient
from oko_user_config import *
oko_influxdb_query = "select last(temperature), humidity, light, windspeed, pressure from weather where device='"
def read_data_from_influxdb():
# temp, hum, wind, pres, date
ret = [ 0.0, 0.0, 0.0, 0.0, "" ]
client = InfluxDBClient(oko_server, oko_server_port, oko_user, oko_password, oko_dbname)
query = oko_influxdb_query + oko_weatherstation_name + "';"
print(query)
qres = client.query(query)
print(qres)
tdata = qres.raw['series'][0]['values'][0]
print(tdata)
ret[0] = tdata[1] # temp
ret[1] = tdata[2] # hum
ret[2] = tdata[4] * 1000 # wind km/h to m/s
ret[3] = tdata[5] # pres
ret[4] = tdata[0] # date
print(ret)
return ret
def send_data_to_openweather(temp, hum, wind, pres, date):
# http://api.openweathermap.org/data/3.0/stations?appid=
http.client.HTTPSConnection.debuglevel = 1
conn = http.client.HTTPSConnection("api.openweathermap.com")
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
url = "/data/3.0/stations?appid=" + openweather_api_key
data = '[{"station_id":"' + openweather_station_id + '",' + \
'"dt":' + str(date) + '",' + \
'"temperature":"' + str(temp) + '",' + \
'"wind_speed":"' + str(wind) + '",' + \
'"pressure":"' + str(pres) + '",' + \
'"humidity":"' + str(hum) + '"' + \
'}]'
header = { "Content-Type" : "application/json" }
print(url)
print(data)
print(header)
#conn.request("POST", url, urllib.parse.urlencode(data), header)
conn.request("POST", url, data, header)
res = conn.getresponse()
print(res.read())
def date_to_str(date):
date = date.replace("T", " ")[0:19]
ret = int(time.mktime(datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S").timetuple()))
return ret
if __name__ == "__main__":
data = [ 0.0, 0.0, 0.0, 0.0, 0 ]
data = read_data_from_influxdb()
temp = data[0]
hum = data[1]
wind = data[2]
pres = data[3]
date = data[4]
tdate = date_to_str(date)
send_data_to_openweather(temp, hum, wind, pres, tdate)

View File

@ -0,0 +1,11 @@
oko_server = "influxdb host"
oko_server_port = "8082"
oko_weatherstation_name = "myStation"
oko_user = "username"
oko_password = "password"
oko_dbname = "weatherstation"
openweather_api_key = "12345"
openweather_station_id = "01234"