From bf7ac4b0df2b6ec08bfc369999cba2eb8c9ae9a4 Mon Sep 17 00:00:00 2001 From: klaute Date: Tue, 24 Mar 2020 22:24:40 +0100 Subject: [PATCH] script to sync oko weather station to external weather provider like openweather --- oko-weather/oko-weather-sync.py | 97 ++++++++++++++++++++++++++ oko-weather/oko_user_config.py_example | 11 +++ 2 files changed, 108 insertions(+) create mode 100644 oko-weather/oko-weather-sync.py create mode 100644 oko-weather/oko_user_config.py_example diff --git a/oko-weather/oko-weather-sync.py b/oko-weather/oko-weather-sync.py new file mode 100644 index 0000000..9ce27e1 --- /dev/null +++ b/oko-weather/oko-weather-sync.py @@ -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) + diff --git a/oko-weather/oko_user_config.py_example b/oko-weather/oko_user_config.py_example new file mode 100644 index 0000000..976cdb7 --- /dev/null +++ b/oko-weather/oko_user_config.py_example @@ -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" +