Browse Source

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

klaute 7 months ago
parent
commit
bf7ac4b0df
2 changed files with 108 additions and 0 deletions
  1. 97
    0
      oko-weather/oko-weather-sync.py
  2. 11
    0
      oko-weather/oko_user_config.py_example

+ 97
- 0
oko-weather/oko-weather-sync.py View File

@@ -0,0 +1,97 @@
1
+
2
+import logging
3
+
4
+import time
5
+
6
+import datetime
7
+
8
+import urllib.parse
9
+
10
+import http.client
11
+
12
+from influxdb import InfluxDBClient
13
+
14
+from oko_user_config import *
15
+
16
+oko_influxdb_query = "select last(temperature), humidity, light, windspeed, pressure from weather where device='"
17
+
18
+def read_data_from_influxdb():
19
+    # temp, hum, wind, pres, date
20
+    ret = [ 0.0, 0.0, 0.0, 0.0, "" ]
21
+
22
+    client = InfluxDBClient(oko_server, oko_server_port, oko_user, oko_password, oko_dbname)
23
+
24
+    query = oko_influxdb_query + oko_weatherstation_name + "';"
25
+    print(query)
26
+
27
+    qres = client.query(query)
28
+    print(qres)
29
+
30
+    tdata = qres.raw['series'][0]['values'][0]
31
+
32
+    print(tdata)
33
+
34
+    ret[0] = tdata[1] # temp
35
+    ret[1] = tdata[2] # hum
36
+    ret[2] = tdata[4] * 1000 # wind km/h to m/s
37
+    ret[3] = tdata[5] # pres
38
+    ret[4] = tdata[0] # date
39
+
40
+    print(ret)
41
+    return ret
42
+
43
+def send_data_to_openweather(temp, hum, wind, pres, date):
44
+
45
+    # http://api.openweathermap.org/data/3.0/stations?appid=
46
+    http.client.HTTPSConnection.debuglevel = 1
47
+    conn = http.client.HTTPSConnection("api.openweathermap.com")
48
+
49
+    logging.basicConfig()
50
+    logging.getLogger().setLevel(logging.DEBUG)
51
+    requests_log = logging.getLogger("requests.packages.urllib3")
52
+    requests_log.setLevel(logging.DEBUG)
53
+    requests_log.propagate = True
54
+
55
+    url = "/data/3.0/stations?appid=" + openweather_api_key
56
+
57
+    data = '[{"station_id":"' + openweather_station_id + '",' + \
58
+             '"dt":' + str(date) + '",' + \
59
+             '"temperature":"' + str(temp) + '",' + \
60
+             '"wind_speed":"' + str(wind) + '",' + \
61
+             '"pressure":"' + str(pres) + '",' + \
62
+             '"humidity":"' + str(hum) + '"' + \
63
+           '}]'
64
+
65
+    header = { "Content-Type" : "application/json" }
66
+
67
+    print(url)
68
+    print(data)
69
+    print(header)
70
+
71
+    #conn.request("POST", url, urllib.parse.urlencode(data), header)
72
+    conn.request("POST", url, data, header)
73
+
74
+    res = conn.getresponse()
75
+    print(res.read())
76
+
77
+def date_to_str(date):
78
+    date = date.replace("T", " ")[0:19]
79
+    ret = int(time.mktime(datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S").timetuple()))
80
+    return ret
81
+
82
+if __name__ == "__main__":
83
+
84
+    data = [ 0.0, 0.0, 0.0, 0.0, 0 ]
85
+
86
+    data = read_data_from_influxdb()
87
+
88
+    temp = data[0]
89
+    hum = data[1]
90
+    wind = data[2]
91
+    pres = data[3]
92
+    date = data[4]
93
+
94
+    tdate = date_to_str(date)
95
+
96
+    send_data_to_openweather(temp, hum, wind, pres, tdate)
97
+

+ 11
- 0
oko-weather/oko_user_config.py_example View File

@@ -0,0 +1,11 @@
1
+
2
+oko_server = "influxdb host"
3
+oko_server_port  = "8082"
4
+oko_weatherstation_name = "myStation"
5
+oko_user = "username"
6
+oko_password = "password"
7
+oko_dbname = "weatherstation"
8
+
9
+openweather_api_key = "12345"
10
+openweather_station_id = "01234"
11
+

Loading…
Cancel
Save