forked from Betschi/FANET_V2019_02_07
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherdata_winds_4.py
205 lines (172 loc) · 6.7 KB
/
weatherdata_winds_4.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: weatherdata_winds_x.py
#
# Function: Takes from https://winds.mobi live weather informations from the selected weather stations.
# Informations are delivered in JSON format.
# Informations are stored in the MariaDB (SQL)
# More information about the winds.mobi API under https://winds.mobi/doc/#!/stations
#
# Requirements: - Installed MariaDB with installed "FANET" databench
# - Internet connection
#
# Version: 0.2 (Beta)
# Date: 22.12.2018
# Author: Christoph Betschart (christoph[at]betschart.ch)
import time
import json
import requests
import mysql.connector
import datetime
from requests import ConnectionError
# Configuration
max_stations = 8 # Limits the number of weather stations Recommendation: 8... max 16 stations
repetiton_time = 30 # Time for the next read from https://winds.mobi Recommendation: 30...60 seconds
# - Weather stations:
station = [None] * max_stations
station[0] = 'windline-4117' # Landeplatz Höhematte, Interlaken, BE
station[1] = 'windline-4116' # Landeplatz Lehn, Interlaken, BE
station[2] = 'windline-5200' # Startplatz Amisbühl, Beatenberg, BE
station[3] = 'windline-4115' # Hohwald, Beatenberg, BE
station[4] = 'windline-4109' # Niederhorn, Beatenberg, BE
station[5] = 'meteoswiss-INT' # Interlaken
station[6] = None # Reserve
station[7] = None # Reserve
# ANSI colors
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# Creat URL for https request
# DEBUG: url = requests.get('https://winds.mobi/api/2/stations/?limit=5&provider=jdc')
# DEBUG: url = requests.get('https://winds.mobi/api/2/stations/?ids=windline-4117&ids=windline-4116&ids=windline-5200&ids=windline-4115&ids=windline-4109&ids=meteoswiss-INT')
url_text = "https://winds.mobi/api/2/stations/"
station_nr = 0
while station[station_nr]:
if station_nr == 0:
url_text += "?ids="+station[station_nr]
else:
url_text += "&ids="+station[station_nr]
station_nr += 1
time_stamp = [0] * max_stations
_id = [None] * max_stations
name = [None] * max_stations
short = [None] * max_stations
latitude = [None] * max_stations
longitude = [None] * max_stations
alt = [None] * max_stations
temp = [None] * max_stations
hum = [None] * max_stations
pres = [None] * max_stations
w_avg = [None] * max_stations
w_max = [None] * max_stations
w_dir = [None] * max_stations
# Connects with the local MariaDB.
# Install "FANET" databench previously
# Caution: passwd="raspberry" is the standard password of the Rasperry Pi. Chnage it if necessary.
mydb = mysql.connector.connect(
host="localhost",
user="pi",
passwd="raspberry",
database="FANET"
)
while True:
while True:
try:
print bcolors.OKBLUE + "Request data from: ",url_text +bcolors.ENDC
url = requests.get(url_text)
break
except ConnectionError:
print bcolors.FAIL + "Connection error. Retry..." + bcolors.ENDC
url.encoding = 'utf-8'
weather = json.loads(url.text)
# Read out each weather station
for x in range(station_nr):
print "------------------------------------------------------"
if time_stamp[x] == weather[x]['last']['time']:
print "No data changed for Station %d"%(x),weather[x]['short']
elif time_stamp[x] <> weather[x]['last']['time']:
time_stamp[x] = weather[x]['last']['time']
print "ID : ",weather[x]['_id']
print "Name: ",weather[x]['name']
print "Short:",weather[x]['short']
print "Longitute : ",weather[x]['loc']['coordinates'][0]
print "Latitude : ",weather[x]['loc']['coordinates'][1]
print "Altitude : ",weather[x]['alt'],"m"
print "Time : ",weather[x]['last']['time'],"sec = ", datetime.datetime.fromtimestamp(weather[x]['last']['time']).strftime('%Y-%m-%d %H:%M:%S')
# Check, if JSON keys are existing. Not all weather stations provide the full spectrum of data (keys)
if 'temp' in weather[x]['last']:
temp[x] = weather[x]['last']['temp']
print "Temperature : ",temp[x],"C"
else:
temp[x] = None
print "Temperature : * (Key doesn't exist)"
if 'hum' in weather[x]['last']:
hum[x] = weather[x]['last']['hum']
print "Humidity : ",hum[x],"%rh"
else:
hum[x] = None
print "Humidity : * (Key doesn't exist)"
if 'pres' in weather[x]['last']:
pres[x] = weather[x]['last']['pres']['qfe']
print "Pressure : ",pres[x],"hPa"
else:
pres[x] = None
print "Pressure : * (Key doesn't exist)"
if 'w-avg' in weather[x]['last']:
w_avg[x] = weather[x]['last']['w-avg']
print "Wind average : ",w_avg[x],"km/h"
else:
w_avg[x] = None
print "Wind average : * (Key doesn't exist)"
if 'w-max' in weather[x]['last']:
w_max[x] = weather[x]['last']['w-max']
print "Wind peak : ",w_max[x],"km/h"
else:
w_avg[x] = None
print "Wind peak : * (Key doesn't exist)"
if 'w-dir' in weather[x]['last']:
w_dir[x] = weather[x]['last']['w-dir']
print "Wind direction: ",w_dir[x],"Grad"
else:
w_dir[x] = None
print "Wind direction: * (Key doesn't exist)"
mycursor = mydb.cursor()
# Update weather station information in the SQL only if waether station data has changed
sql = "INSERT INTO weather_stations (_id, name, short, longitude, latitude, alt) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE name=%s, short=%s, longitude=%s, latitude=%s, alt=%s"
val = (weather[x]['_id'],
weather[x]['name'],
weather[x]['short'],
weather[x]['loc']['coordinates'][0],
weather[x]['loc']['coordinates'][1],
weather[x]['alt'],
weather[x]['name'], # variables after "... ON DUPLICATE KEY UPDATE ..."
weather[x]['short'],
weather[x]['loc']['coordinates'][0],
weather[x]['loc']['coordinates'][1],
weather[x]['alt']
)
mycursor.execute(sql,val)
mydb.commit()
print("MySQL weather_stations: ",mycursor.rowcount, "record inserted.")
# Insert new weather data into the SQL
sql = "INSERT INTO weather_values (idweather_values, _id_stations, time, temp, hum, pres, w_avg, w_max, w_dir) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = ("0",
weather[x]['_id'],
weather[x]['last']['time'],
temp[x],
hum[x],
pres[x],
w_avg[x],
w_max[x],
w_dir[x]
)
mycursor.execute(sql,val)
mydb.commit()
print("MySQL weather_values: ",mycursor.rowcount, "record inserted.")
time.sleep(repetiton_time)