From 7197b258342583bbab3a7fc93806f20110176515 Mon Sep 17 00:00:00 2001 From: wwakabobik Date: Wed, 3 Feb 2021 15:37:50 +0300 Subject: [PATCH] Massive update: - wind_data added - power data added - some refactoring --- home_server/app.py | 74 +++++++-- home_server/db/power_data.py | 3 - home_server/db/queries.py | 101 +++++++++++++ home_server/db/schema.sql | 46 +++++- home_server/db/weather_station.py | 76 ---------- home_server/db/wind_station.py | 3 - .../pages/weather_station/compare_page.py | 6 +- .../pages/weather_station/dashboard.py | 141 +++++++++++------- .../pages/weather_station/send_data.py | 2 +- .../pages/weather_station/single_data_page.py | 8 +- .../pages/weather_station/single_page.py | 22 ++- home_server/pages/weather_station/tools.py | 39 +++-- home_server/templates/index.html | 12 +- .../weather_station/compare_page.html | 11 +- .../weather_station/single_data_page.html | 4 +- .../weather_station/single_power_page.html | 35 +++++ ...gle_page.html => single_weather_page.html} | 15 +- .../weather_station/single_wind_page.html | 43 ++++++ ...{dashboard.html => weather_dashboard.html} | 2 +- iot/arduino/wind_meter/wind_meter.ino | 22 ++- 20 files changed, 454 insertions(+), 211 deletions(-) delete mode 100644 home_server/db/power_data.py create mode 100644 home_server/db/queries.py delete mode 100644 home_server/db/weather_station.py delete mode 100644 home_server/db/wind_station.py create mode 100644 home_server/templates/weather_station/single_power_page.html rename home_server/templates/weather_station/{single_page.html => single_weather_page.html} (63%) create mode 100644 home_server/templates/weather_station/single_wind_page.html rename home_server/templates/weather_station/{dashboard.html => weather_dashboard.html} (99%) diff --git a/home_server/app.py b/home_server/app.py index 818f5bc..1b09aeb 100755 --- a/home_server/app.py +++ b/home_server/app.py @@ -1,14 +1,15 @@ #!/usr/bin/env python3.7 from datetime import datetime +from time import time from flask import Flask, jsonify, request, abort from db.db import init_app -from db.weather_station import store_weather_data +from db.queries import store_weather_data from pages.index import index_page from pages.weather_station.dashboard import dashboard_page -from pages.weather_station.single_page import single_page +from pages.weather_station.single_page import single_weather_page, single_wind_page, single_power_page from pages.weather_station.single_data_page import single_data_page from pages.weather_station.compare_page import compare_page from pages.weather_station.send_data import send_data @@ -18,7 +19,7 @@ @app.route('/api/v1/add_weather_data', methods=['POST']) -def store_in_db(): +def store_weather_data(): if not request.json: abort(400) timestamp = str(datetime.now()) @@ -28,6 +29,30 @@ def store_in_db(): return jsonify({'data': db_data}), 201 +@app.route('/api/v1/add_power_data', methods=['POST']) +def store_power_data(): + if not request.json: + abort(400) + timestamp = str(datetime.now()) + unix_timestamp = int(time()) + data = request.json.get('data', "")[:2] + db_data = f'"{timestamp}", {unix_timestamp}, {data}' + store_weather_data(db_data) + return jsonify({'data': db_data}), 201 + + +@app.route('/api/v1/add_wind_data', methods=['POST']) +def store_wind_data(): + if not request.json: + abort(400) + timestamp = str(datetime.now()) + unix_timestamp = int(time()) + data = request.json.get('data', "")[:2] + db_data = f'"{timestamp}", {unix_timestamp}, {data}' + store_weather_data(db_data) + return jsonify({'data': db_data}), 201 + + @app.route('/send_data') def send_weather_data(): return send_data() @@ -44,25 +69,54 @@ def dashboard(): return dashboard_page() -@app.route('/single_page') -def single(): +@app.route('/single_weather_page') +def single_weather_page_via_url(): + period = request.args.get('period') + param = request.args.get('param') + return single_weather_page(param=param, period=period) + + +@app.route('/single_wind_page') +def single_wind_page_via_url(): + period = request.args.get('period') + param = request.args.get('param') + return single_weather_page(param=param, period=period) + + +@app.route('/single_power_page') +def single_power_page_via_url(): period = request.args.get('period') param = request.args.get('param') - return single_page(param=param, period=period) + return single_weather_page(param=param, period=period) + + +@app.route('/single_weather_page', methods=['POST']) +def single_weather_page_via_post(): + period = request.form['period'] + param = request.form['param'] + return single_weather_page(param=param, period=period) + + +@app.route('/single_wind_page', methods=['POST']) +def single_wind_page_via_post(): + period = request.form['period'] + param = request.form['param'] + return single_wind_page(param=param, period=period) -@app.route('/single_page', methods=['POST']) -def single_via_post(): +@app.route('/single_power_page', methods=['POST']) +def single_power_page_via_post(): period = request.form['period'] param = request.form['param'] - return single_page(param=param, period=period) + return single_power_page(param=param, period=period) @app.route('/single_data_page') def single_data(): + table = request.args.get('table') period = request.args.get('period') param = request.args.get('param') - return single_data_page(param=param, period=period) + return single_data_page(table=table, param=param, period=period) @app.route('/compare_page', methods=['POST']) diff --git a/home_server/db/power_data.py b/home_server/db/power_data.py deleted file mode 100644 index 0c4d38b..0000000 --- a/home_server/db/power_data.py +++ /dev/null @@ -1,3 +0,0 @@ -from db.db import get_db, close_db - -data_update_period = 5 \ No newline at end of file diff --git a/home_server/db/queries.py b/home_server/db/queries.py new file mode 100644 index 0000000..b3db286 --- /dev/null +++ b/home_server/db/queries.py @@ -0,0 +1,101 @@ +from db.db import get_db, close_db + +data_update_period = 5 + + +def store_weather_data(data): + connection = get_db() + sql = f'''INSERT INTO weather_data(ts,meas_type,temperature,humidity,pressure,dew_point) VALUES({data});''' + cur = connection.cursor() + cur.execute(sql) + connection.commit() + close_db() + return cur.lastrowid + + +def store_wind_data(data): + connection = get_db() + sql = f'''INSERT INTO wind_data(ts,unix_ts, + avg_rps,max_rps,min_rps, + avg_ms,max_ms,min_ms, + avg_kmh,max_kmh,min_knh, + avg_knots,max_knots,min_knots, + heading,heading_abbr) VALUES({data});''' + cur = connection.cursor() + cur.execute(sql) + connection.commit() + close_db() + return cur.lastrowid + + +def store_power_data(data): + connection = get_db() + sql = f'''INSERT INTO wind_data(ts,unix_ts,avg_voltage,avg_current,avg_power,avg_consumption) VALUES({data});''' + cur = connection.cursor() + cur.execute(sql) + connection.commit() + close_db() + return cur.lastrowid + + +def get_one_measurement(table, param, offset, meas_type): + connection = get_db() + criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else '' + sql = f''' SELECT {param} FROM {table} {criteria} ORDER BY RowId DESC LIMIT 2 OFFSET {offset}; ''' + cur = connection.cursor() + cur.execute(sql) + row = cur.fetchone() + if row: + retval = dict(zip(row.keys(), row))[param] + else: + retval = 0 + return retval + + +def get_last_measurement_pack(table, offset, meas_type=None): + connection = get_db() + criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else '' + sql = f''' SELECT * FROM {table} {criteria} ORDER BY RowId DESC LIMIT 2 OFFSET {offset}; ''' + cur = connection.cursor() + cur.execute(sql) + row = cur.fetchone() + if row: + retval = dict(zip(row.keys(), row)) + else: + retval = 0 + return retval + + +def get_one_last_average_measurement(table, param, period, meas_type=None): + connection = get_db() + criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else '' + number = str(2 + (period / data_update_period)) + sql = f''' avg({param}) from (SELECT {param} FROM {table} {criteria} ORDER BY RowId DESC LIMIT {number}); ''' + cur = connection.cursor() + cur.execute(sql) + row = cur.fetchone() + if cur.rowcount > 0: + retval = dict(zip(row.keys(), row))[param] + else: + retval = 0 + return retval + + +def get_last_series_measurement(table, param, period, meas_type=None): + connection = get_db() + criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else '' + number = int(period / data_update_period) + sql = f''' SELECT ts, {param} FROM {table} {criteria} ORDER BY RowId DESC LIMIT {number}; ''' + cur = connection.cursor() + cur.execute(sql) + + columns = [column[0] for column in cur.description] + results = [] + for row in cur.fetchall(): + results.append(dict(zip(columns, row))) + x = [] + y = [] + for result in results: + x.append(result['ts']) + y.append(result[param]) + return x, y diff --git a/home_server/db/schema.sql b/home_server/db/schema.sql index 01b6404..4a9ace5 100644 --- a/home_server/db/schema.sql +++ b/home_server/db/schema.sql @@ -1,11 +1,43 @@ DROP TABLE IF EXISTS weather_data; +DROP TABLE IF EXISTS wind_data; +DROP TABLE IF EXISTS power_data; CREATE TABLE weather_data ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - ts TIMESTAMP NOT NULL, - meas_type INTEGER NOT NULL, - temperature DOUBLE, - humidity DOUBLE, - pressure DOUBLE, - dew_point DOUBLE + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts TIMESTAMP NOT NULL, + meas_type INTEGER NOT NULL, + temperature DOUBLE, + humidity DOUBLE, + pressure DOUBLE, + dew_point DOUBLE +); + +CREATE TABLE wind_data ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts TIMESTAMP NOT NULL, + unix_tx INTEGER NOT NULL, + avg_rps DOUBLE, + max_rps DOUBLE, + min_rps DOUBLE, + avg_ms DOUBLE, + max_ms DOUBLE, + min_ms DOUBLE, + avg_kmh DOUBLE, + max_kmh DOUBLE, + min_knh DOUBLE, + avg_knots DOUBLE, + max_knots DOUBLE, + min_knots DOUBLE, + heading DOUBLE, + heading_abbr VARCHAR +); + +CREATE TABLE power_data ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts TIMESTAMP NOT NULL, + unix_tx INTEGER NOT NULL, + avg_voltage DOUBLE, + avg_current DOUBLE, + avg_power DOUBLE, + avg_consumption DOUBLE ); \ No newline at end of file diff --git a/home_server/db/weather_station.py b/home_server/db/weather_station.py deleted file mode 100644 index 008c219..0000000 --- a/home_server/db/weather_station.py +++ /dev/null @@ -1,76 +0,0 @@ -from db.db import get_db, close_db - -data_update_period = 5 - - -def store_weather_data(data): - connection = get_db() - sql = f'''INSERT INTO weather_data(ts,meas_type,temperature,humidity,pressure,dew_point) VALUES({data});''' - cur = connection.cursor() - cur.execute(sql) - connection.commit() - close_db() - return cur.lastrowid - - -def get_one_measurement(param, offset, meas_type): - connection = get_db() - criteria = f'WHERE meas_type = \'{meas_type}\'' - sql = f''' SELECT {param} FROM weather_data {criteria} ORDER BY RowId DESC LIMIT 2 OFFSET {offset}; ''' - cur = connection.cursor() - cur.execute(sql) - row = cur.fetchone() - if row: - retval = dict(zip(row.keys(), row))[param] - else: - retval = 0 - return retval - - -def get_last_measurement_pack(offset, meas_type): - connection = get_db() - criteria = f'WHERE meas_type = \'{meas_type}\'' - sql = f''' SELECT * FROM weather_data {criteria} ORDER BY RowId DESC LIMIT 2 OFFSET {offset}; ''' - cur = connection.cursor() - cur.execute(sql) - row = cur.fetchone() - if row: - retval = dict(zip(row.keys(), row)) - else: - retval = 0 - return retval - - -def get_one_last_average_measurement(param, period, meas_type): - connection = get_db() - criteria = f'WHERE meas_type = {meas_type}' - number = str(2 + (period / data_update_period)) - sql = f''' avg({param}) from (SELECT param FROM weather_data {criteria} ORDER BY RowId DESC LIMIT {number}); ''' - cur = connection.cursor() - cur.execute(sql) - row = cur.fetchone() - if cur.rowcount > 0: - retval = dict(zip(row.keys(), row))[param] - else: - retval = 0 - return retval - - -def get_last_series_measurement(param, period, meas_type): - connection = get_db() - criteria = f'WHERE meas_type = {meas_type}' - number = int(period / data_update_period) - sql = f''' SELECT ts, {param} FROM weather_data {criteria} ORDER BY RowId DESC LIMIT {number}; ''' - cur = connection.cursor() - cur.execute(sql) - - columns = [column[0] for column in cur.description] - results = [] - for row in cur.fetchall(): - results.append(dict(zip(columns, row))) - x = [] - y = [] - for result in results: - x.append(result['ts']) - y.append(result[param]) - return x, y diff --git a/home_server/db/wind_station.py b/home_server/db/wind_station.py deleted file mode 100644 index 24e88d4..0000000 --- a/home_server/db/wind_station.py +++ /dev/null @@ -1,3 +0,0 @@ -from db.db import get_db, close_db - -data_update_period = 5 diff --git a/home_server/pages/weather_station/compare_page.py b/home_server/pages/weather_station/compare_page.py index 9585e52..a53fbd6 100644 --- a/home_server/pages/weather_station/compare_page.py +++ b/home_server/pages/weather_station/compare_page.py @@ -3,15 +3,15 @@ from pages.weather_station.tools import generate_scatter -def compare_page(param1, param2, period): +def compare_page(table1, table2, param1, param2, period): if param1 is None: param1 = "temperature_in" if param2 is None: param2 = "temperature_in" if period is None: period = "day" - offline_fig1 = generate_scatter(param=param1, period=period, width=600, height=600) - offline_fig2 = generate_scatter(param=param2, period=period, width=600, height=600) + offline_fig1 = generate_scatter(table1=table1, param=param1, period=period, width=600, height=600) + offline_fig2 = generate_scatter(table2=table2, param=param2, period=period, width=600, height=600) return render_template("weather_station/compare_page.html", param1=param1, param2=param2, diff --git a/home_server/pages/weather_station/dashboard.py b/home_server/pages/weather_station/dashboard.py index b0658c4..3972f9a 100644 --- a/home_server/pages/weather_station/dashboard.py +++ b/home_server/pages/weather_station/dashboard.py @@ -1,10 +1,11 @@ from flask import render_template from wunderground_pws import WUndergroundAPI, units -from db.weather_station import get_one_measurement, data_update_period, get_last_measurement_pack +from db.queries import get_one_measurement, data_update_period, get_last_measurement_pack from secure_data import wu_api_key, wu_reference_station_id +# To obtain reference values wu = WUndergroundAPI( api_key=wu_api_key, default_station_id=wu_reference_station_id, @@ -19,16 +20,21 @@ def deg_to_heading(degrees=0): def dashboard_page_fast(): - current_data_in = get_last_measurement_pack('0', '0') - current_data_out = get_last_measurement_pack('0', '1') - previous_data_in = get_last_measurement_pack('0', '0') - previous_data_out = get_last_measurement_pack('0', '1') - last_day_data_in = get_last_measurement_pack((float(60 * 24) / data_update_period) - 1, '0') - last_day_data_out = get_last_measurement_pack((float(60 * 24) / data_update_period) - 1, '1') - last_year_data_in = get_last_measurement_pack((float(60 * 24 * 365) / data_update_period) - 1, '0') - last_year_data_out = get_last_measurement_pack((float(60 * 24 * 365) / data_update_period) - 1, '1') + current_data_in = get_last_measurement_pack('weather_data', '0', '0') + current_data_out = get_last_measurement_pack('weather_data', '0', '1') + previous_data_in = get_last_measurement_pack('weather_data', '1', '0') + previous_data_out = get_last_measurement_pack('weather_data', '1', '1') + last_day_data_in = get_last_measurement_pack('weather_data', (float(60 * 24) / data_update_period) - 1, '0') + last_day_data_out = get_last_measurement_pack('weather_data', (float(60 * 24) / data_update_period) - 1, '1') + last_year_data_in = get_last_measurement_pack('weather_data', (float(60 * 24 * 365) / data_update_period) - 1, '0') + last_year_data_out = get_last_measurement_pack('weather_data', (float(60 * 24 * 365) / data_update_period) - 1, '1') + current_wind_data = get_last_measurement_pack('wind_data', '0') + previous_wind_data = get_last_measurement_pack('weather_data', '1') + last_day_wind_data = get_last_measurement_pack('weather_data', (float(60 * 24) / data_update_period) - 1) + last_year_wind_data = get_last_measurement_pack('weather_data', (float(60 * 24 * 365) / data_update_period) - 1) + wu_current = wu.current() - return render_template("weather_station/dashboard.html", + return render_template("weather_station/weather_dashboard.html", temperature_in=current_data_in['temperature'], temperature_out=current_data_out['temperature'], humidity_in=current_data_in['humidity'], @@ -57,63 +63,84 @@ def dashboard_page_fast(): last_year_pressure=last_year_data_in['pressure'], last_year_dew_point_in=last_year_data_in['dew_point'], last_year_dew_point_out=last_year_data_out['dew_point'], - wu_temp=wu.current()['observations'][0]['metric_si']['temp'], - wu_humidity=wu.current()['observations'][0]['humidity'], - wu_pressure=int(int(wu.current()['observations'][0]['metric_si']['pressure'])/1.33), - wu_dew_point=wu.current()['observations'][0]['metric_si']['dewpt'], - wu_wind_speed=wu.current()['observations'][0]['metric_si']['windSpeed'], - wu_wind_gust=wu.current()['observations'][0]['metric_si']['windGust'], - wu_wind_direction=wu.current()['observations'][0]['winddir'], - wu_wind_heading=deg_to_heading(int(wu.current()['observations'][0]['winddir'])) + current_wind_speed=current_wind_data['avg_kmh'], + current_wind_gust=current_wind_data['max_kmh'], + previous_wind_speed=previous_wind_data['avg_kmh'], + prevous_wind_gust=previous_wind_data['max_kmh'], + last_day_wind_speed=last_day_wind_data['avg_kmh'], + last_day_wind_gust=last_day_wind_data['max_kmh'], + last_year_wind_speed=last_year_wind_data['avg_kmh'], + last_year_wind_gust=last_year_wind_data['max_kmh'], + wind_heading=current_wind_data['heading'], + wind_heading_abbr=deg_to_heading(current_wind_data['heading']), + wu_temp=wu_current['observations'][0]['metric_si']['temp'], + wu_humidity=wu_current['observations'][0]['humidity'], + wu_pressure=int(int(wu_current['observations'][0]['metric_si']['pressure'])/1.33), + wu_dew_point=wu_current['observations'][0]['metric_si']['dewpt'], + wu_wind_speed=wu_current['observations'][0]['metric_si']['windSpeed'], + wu_wind_gust=wu_current['observations'][0]['metric_si']['windGust'], + wu_wind_direction=wu_current['observations'][0]['winddir'], + wu_wind_heading=deg_to_heading(int(wu_current['observations'][0]['winddir'])) ) def dashboard_page(): # current data - temperature_in = get_one_measurement('temperature', '0', '0') - temperature_out = get_one_measurement('temperature', '0', '1') - humidity_in = get_one_measurement('humidity', '0', '0') - humidity_out = get_one_measurement('humidity', '0', '1') - pressure = get_one_measurement('pressure', '0', '0') - dew_point_in = get_one_measurement('dew_point', '0', '0') - dew_point_out = get_one_measurement('dew_point', '0', '1') + temperature_in = get_one_measurement('weather_data', 'temperature', '0', '0') + temperature_out = get_one_measurement('weather_data', 'temperature', '0', '1') + humidity_in = get_one_measurement('weather_data', 'humidity', '0', '0') + humidity_out = get_one_measurement('weather_data', 'humidity', '0', '1') + pressure = get_one_measurement('weather_data', 'pressure', '0', '0') + dew_point_in = get_one_measurement('weather_data', 'dew_point', '0', '0') + dew_point_out = get_one_measurement('weather_data', 'dew_point', '0', '1') + wind_speed = get_one_measurement('wind_data', 'avg_speed', '0') + wind_gust = get_one_measurement('wind_data', 'avg_gust', '0') # previous data - prev_temperature_in = get_one_measurement('temperature', '1', '0') - prev_temperature_out = get_one_measurement('temperature', '1', '1') - prev_humidity_in = get_one_measurement('humidity', '1', '0') - prev_humidity_out = get_one_measurement('humidity', '1', '1') - prev_pressure = get_one_measurement('pressure', '1', '0') + prev_temperature_in = get_one_measurement('weather_data', 'temperature', '1', '0') + prev_temperature_out = get_one_measurement('weather_data', 'temperature', '1', '1') + prev_humidity_in = get_one_measurement('weather_data', 'humidity', '1', '0') + prev_humidity_out = get_one_measurement('weather_data', 'humidity', '1', '1') + prev_pressure = get_one_measurement('weather_data', 'pressure', '1', '0') prev_dew_point_in = get_one_measurement('dew_point', '1', '0') prev_dew_point_out = get_one_measurement('dew_point', '1', '1') + prev_wind_speed = get_one_measurement('wind_data', 'avg_speed', '1') + prev_wind_gust = get_one_measurement('wind_data', 'avg_gust', '1') # last day data period = (float(60 * 24) / data_update_period) - 1 - last_day_temperature_in = get_one_measurement('temperature', period, '0') - last_day_temperature_out = get_one_measurement('temperature', period, '1') - last_day_humidity_in = get_one_measurement('humidity', period, '0') - last_day_humidity_out = get_one_measurement('humidity', period, '1') - last_day_pressure = get_one_measurement('pressure', period, '0') - last_day_dew_point_in = get_one_measurement('dew_point', period, '0') - last_day_dew_point_out = get_one_measurement('dew_point', period, '1') + last_day_temperature_in = get_one_measurement('weather_data', 'temperature', period, '0') + last_day_temperature_out = get_one_measurement('weather_data', 'temperature', period, '1') + last_day_humidity_in = get_one_measurement('weather_data', 'humidity', period, '0') + last_day_humidity_out = get_one_measurement('weather_data', 'humidity', period, '1') + last_day_pressure = get_one_measurement('weather_data', 'pressure', period, '0') + last_day_dew_point_in = get_one_measurement('weather_data', 'dew_point', period, '0') + last_day_dew_point_out = get_one_measurement('weather_data', 'dew_point', period, '1') + last_day_wind_speed = get_one_measurement('wind_data', 'avg_speed', period) + last_day_wind_gust = get_one_measurement('wind_data', 'avg_gust', period) # last year data period = (float(60 * 24 * 365) / data_update_period) - 1 - last_year_temperature_in = get_one_measurement('temperature', period, '0') - last_year_temperature_out = get_one_measurement('temperature', period, '1') - last_year_humidity_in = get_one_measurement('humidity', period, '0') - last_year_humidity_out = get_one_measurement('humidity', period, '1') - last_year_pressure = get_one_measurement('pressure', period, '0') - last_year_dew_point_in = get_one_measurement('dew_point', period, '0') - last_year_dew_point_out = get_one_measurement('dew_point', period, '1') + last_year_temperature_in = get_one_measurement('weather_data', 'temperature', period, '0') + last_year_temperature_out = get_one_measurement('weather_data', 'temperature', period, '1') + last_year_humidity_in = get_one_measurement('weather_data', 'humidity', period, '0') + last_year_humidity_out = get_one_measurement('weather_data', 'humidity', period, '1') + last_year_pressure = get_one_measurement('weather_data', 'pressure', period, '0') + last_year_dew_point_in = get_one_measurement('weather_data', 'dew_point', period, '0') + last_year_dew_point_out = get_one_measurement('weather_data', 'dew_point', period, '1') + last_year_wind_speed = get_one_measurement('wind_data', 'avg_speed', period) + last_year_wind_gust = get_one_measurement('wind_data', 'avg_gust', period) + # Wind heading + wind_heading = get_one_measurement('wind_data', 'heading', '0') # WU forecast data - wu_temp = wu.current()['observations'][0]['metric_si']['temp'] - wu_humidity = wu.current()['observations'][0]['humidity'] - wu_pressure = int(int(wu.current()['observations'][0]['metric_si']['pressure'])/1.33) - wu_dew_point = wu.current()['observations'][0]['metric_si']['dewpt'] - wu_wind_speed = wu.current()['observations'][0]['metric_si']['windSpeed'] - wu_wind_gust = wu.current()['observations'][0]['metric_si']['windGust'] - wu_direction = wu.current()['observations'][0]['winddir'] + wu_current = wu.current() + wu_temp = wu_current['observations'][0]['metric_si']['temp'] + wu_humidity = wu_current['observations'][0]['humidity'] + wu_pressure = int(int(wu_current['observations'][0]['metric_si']['pressure'])/1.33) + wu_dew_point = wu_current['observations'][0]['metric_si']['dewpt'] + wu_wind_speed = wu_current['observations'][0]['metric_si']['windSpeed'] + wu_wind_gust = wu_current['observations'][0]['metric_si']['windGust'] + wu_direction = wu_current['observations'][0]['winddir'] wu_heading = deg_to_heading(int(wu_direction)) - return render_template("weather_station/dashboard.html", + return render_template("weather_station/weather_dashboard.html", temperature_in=temperature_in, temperature_out=temperature_out, humidity_in=humidity_in, @@ -142,6 +169,16 @@ def dashboard_page(): last_year_pressure=last_year_pressure, last_year_dew_point_in=last_year_dew_point_in, last_year_dew_point_out=last_year_dew_point_out, + current_wind_speed=wind_speed, + current_wind_gust=wind_gust, + previous_wind_speed=prev_wind_speed, + prevous_wind_gust=prev_wind_gust, + last_day_wind_speed=last_day_wind_speed, + last_day_wind_gust=last_day_wind_gust, + last_year_wind_speed=last_year_wind_speed, + last_year_wind_gust=last_year_wind_gust, + wind_heading=wind_heading, + wind_heading_abbr=deg_to_heading(wind_heading), wu_temp=wu_temp, wu_humidity=wu_humidity, wu_pressure=wu_pressure, diff --git a/home_server/pages/weather_station/send_data.py b/home_server/pages/weather_station/send_data.py index 6f07363..b4d7b28 100644 --- a/home_server/pages/weather_station/send_data.py +++ b/home_server/pages/weather_station/send_data.py @@ -1,7 +1,7 @@ import requests from openweather_pws import Station -from db.weather_station import get_last_measurement_pack +from db.queries import get_last_measurement_pack from secure_data import wu_station_id, wu_station_pwd, pwsw_station_id, pwsw_api_key, ow_station_id, ow_api_key from pages.weather_station.tools import celsius_to_fahrenheit, fahrenheit_to_celsius from pages.weather_station.tools import mmhg_to_baromin, heat_index, humidex diff --git a/home_server/pages/weather_station/single_data_page.py b/home_server/pages/weather_station/single_data_page.py index a5f2c10..2b59102 100644 --- a/home_server/pages/weather_station/single_data_page.py +++ b/home_server/pages/weather_station/single_data_page.py @@ -1,13 +1,13 @@ from flask import render_template -from db.weather_station import get_last_series_measurement +from db.queries import get_last_series_measurement from pages.weather_station.tools import convert_param, convert_period -def single_data_page(period, param): - fparam, meas_type = convert_param(param) +def single_data_page(table, period, param): + fparam, meas_type = convert_param(param, table) num_period = convert_period(period) - x, y = get_last_series_measurement(period=num_period, param=fparam, meas_type=meas_type) + x, y = get_last_series_measurement(table=table, period=num_period, param=fparam, meas_type=meas_type) data = '' for i in range(len(x)): data = f'{data}' diff --git a/home_server/pages/weather_station/single_page.py b/home_server/pages/weather_station/single_page.py index ed29580..335af89 100644 --- a/home_server/pages/weather_station/single_page.py +++ b/home_server/pages/weather_station/single_page.py @@ -3,9 +3,25 @@ from pages.weather_station.tools import generate_scatter -def single_page(param, period): - offline_fig = generate_scatter(param=param, period=period) - return render_template("weather_station/single_page.html", +def single_weather_page(param, period): + offline_fig = generate_scatter(table='weather_data', param=param, period=period) + return render_template("weather_station/single_weather_page.html", + param=param, + period=period, + offline_fig=offline_fig) + + +def single_wind_page(param, period): + offline_fig = generate_scatter(table='wind_data', param=param, period=period) + return render_template("weather_station/single_wind_page.html", + param=param, + period=period, + offline_fig=offline_fig) + + +def single_power_page(param, period): + offline_fig = generate_scatter(table='power_data', param=param, period=period) + return render_template("weather_station/single_power_page.html", param=param, period=period, offline_fig=offline_fig) diff --git a/home_server/pages/weather_station/tools.py b/home_server/pages/weather_station/tools.py index 8970eee..0d376cc 100644 --- a/home_server/pages/weather_station/tools.py +++ b/home_server/pages/weather_station/tools.py @@ -2,10 +2,17 @@ from plotly import graph_objects, offline -from db.weather_station import data_update_period, get_last_series_measurement +from db.queries import data_update_period, get_last_series_measurement -units = {'temperature': '°C', 'humidity': '%', 'pressure': 'mm Hg', 'dew_point': '°C'} +units = {'temperature': '°C', 'humidity': '%', 'pressure': 'mm Hg', 'dew_point': '°C', + 'avg_voltage': 'V', 'avg_current': 'A', 'avg_power': 'W', 'avg_consumption': 'W/h', + 'avg_rps': 'rps', 'max_rps': 'rps', 'min_rps': 'rps', + 'avg_ms' : 'm/s', 'max_ms': 'm/s', 'min_ms': 'm/s', + 'avg_kmh': 'km/h', 'max_kmh': 'km/h', 'min_kmh': 'km/h', + 'avg_knots': 'knots', 'max_knots': 'knots', 'min_knots': 'knots', + 'heading': '°', 'precip': 'mm', 'uv': 'mW/cm^2' + } def convert_period(period): @@ -24,23 +31,27 @@ def convert_period(period): return num_period -def convert_param(param): - if "_in" in param: - meas_type = 0 - fparam = param[:-3] - elif "_out" in param: - meas_type = 1 - fparam = param[:-4] +def convert_param(param, table): + if table == 'weather_data': + if "_in" in param: + meas_type = 0 + fparam = param[:-3] + elif "_out" in param: + meas_type = 1 + fparam = param[:-4] + else: + meas_type = 0 + fparam = param else: - meas_type=0 - fparam=param + fparam = param + meas_type = None return fparam, meas_type -def generate_scatter(param, period, height=None, width=None): - fparam, meas_type = convert_param(param) +def generate_scatter(table, param, period, height=None, width=None): + fparam, meas_type = convert_param(param, table) num_period = convert_period(period) - x, y = get_last_series_measurement(period=num_period, param=fparam, meas_type=meas_type) + x, y = get_last_series_measurement(table=table, period=num_period, param=fparam, meas_type=meas_type) line_chart1 = graph_objects.Scatter(x=x, y=y) lay1 = graph_objects.Layout( title=f'{param}', diff --git a/home_server/templates/index.html b/home_server/templates/index.html index ad27c3a..63189ef 100644 --- a/home_server/templates/index.html +++ b/home_server/templates/index.html @@ -4,8 +4,12 @@ H.O.M.E -

Home Online Monitoring Environment

-
- Weather dashboard +
+

Home Online Monitoring Environment

+
+ Weather dashboard + Wind station dashboard + Power data dashboard +
- \ No newline at end of file + diff --git a/home_server/templates/weather_station/compare_page.html b/home_server/templates/weather_station/compare_page.html index ca04f35..07588f8 100644 --- a/home_server/templates/weather_station/compare_page.html +++ b/home_server/templates/weather_station/compare_page.html @@ -37,19 +37,10 @@

{{period}} data for {{param1}}/{{param2}}

- - - - - - - - -
- \ No newline at end of file + diff --git a/home_server/templates/weather_station/single_data_page.html b/home_server/templates/weather_station/single_data_page.html index e612cb2..200a3e5 100644 --- a/home_server/templates/weather_station/single_data_page.html +++ b/home_server/templates/weather_station/single_data_page.html @@ -8,7 +8,7 @@

{{param}} data for {{period}}

{{ data|safe }}
- Go back to single page + Go Back
- \ No newline at end of file + diff --git a/home_server/templates/weather_station/single_power_page.html b/home_server/templates/weather_station/single_power_page.html new file mode 100644 index 0000000..c2affc8 --- /dev/null +++ b/home_server/templates/weather_station/single_power_page.html @@ -0,0 +1,35 @@ + + + H.O.M.E. - {{param}} for {{period}} + + +

{{period}} data for {{param}}

+ {{ offline_fig|safe }} +
+
+ Param: + + Period: + + +
+ +
+ + +
+ Data in table view +
+ + diff --git a/home_server/templates/weather_station/single_page.html b/home_server/templates/weather_station/single_weather_page.html similarity index 63% rename from home_server/templates/weather_station/single_page.html rename to home_server/templates/weather_station/single_weather_page.html index 350901f..289071b 100644 --- a/home_server/templates/weather_station/single_page.html +++ b/home_server/templates/weather_station/single_weather_page.html @@ -6,7 +6,7 @@

{{period}} data for {{param}}

{{ offline_fig|safe }}
-
+ Param:
@@ -41,7 +32,7 @@

{{period}} data for {{param}}


- Data in table view + Data in table view
- \ No newline at end of file + diff --git a/home_server/templates/weather_station/single_wind_page.html b/home_server/templates/weather_station/single_wind_page.html new file mode 100644 index 0000000..3793732 --- /dev/null +++ b/home_server/templates/weather_station/single_wind_page.html @@ -0,0 +1,43 @@ + + + H.O.M.E. - {{param}} for {{period}} + + +

{{period}} data for {{param}}

+ {{ offline_fig|safe }} +
+
+ Param: + + Period: + + +
+ +
+ + +
+ Data in table view +
+ + diff --git a/home_server/templates/weather_station/dashboard.html b/home_server/templates/weather_station/weather_dashboard.html similarity index 99% rename from home_server/templates/weather_station/dashboard.html rename to home_server/templates/weather_station/weather_dashboard.html index c51cf60..06cc292 100644 --- a/home_server/templates/weather_station/dashboard.html +++ b/home_server/templates/weather_station/weather_dashboard.html @@ -242,4 +242,4 @@

Wind

{x[i]}{y[i]}
- \ No newline at end of file + diff --git a/iot/arduino/wind_meter/wind_meter.ino b/iot/arduino/wind_meter/wind_meter.ino index ac8fa22..4e0a966 100644 --- a/iot/arduino/wind_meter/wind_meter.ino +++ b/iot/arduino/wind_meter/wind_meter.ino @@ -548,13 +548,23 @@ String get_all_data_as_csv() // get all data and format it as CSV wd = get_rps_data_per_period(CYCLE_MILLIS*MEAS_PERIODS, MEAS_PERIODS); heading = get_heading(); - return_string = DEVICE_ID + delimiter +String(millis()) + delimiter + String(wd.average_rps) + delimiter + String(wd.max_rps) + delimiter + String(wd.min_rps); - return_string += delimiter + String(rps_to_ms(wd.average_rps)) + delimiter + String(rps_to_ms(wd.max_rps)) + delimiter + String(rps_to_ms(wd.min_rps)); - return_string += delimiter + String(ms_to_kmh(rps_to_ms(wd.average_rps))) + delimiter + String(ms_to_kmh(rps_to_ms(wd.max_rps))) + delimiter + String(ms_to_kmh(rps_to_ms(wd.min_rps))); - return_string += delimiter + String(ms_to_knots(rps_to_ms(wd.average_rps))) + delimiter + String(ms_to_knots(rps_to_ms(wd.max_rps))) + delimiter + String(ms_to_knots(rps_to_ms(wd.min_rps))); - return_string += delimiter + String(heading) + delimiter + heading_to_abbr(heading); + return_string = DEVICE_ID; + return_string += delimiter + String(wd.average_rps); + return_string += delimiter + String(wd.max_rps); + return_string += delimiter + String(wd.min_rps); + return_string += delimiter + String(rps_to_ms(wd.average_rps)); + return_string += delimiter + String(rps_to_ms(wd.max_rps)); + return_string += delimiter + String(rps_to_ms(wd.min_rps)); + return_string += delimiter + String(ms_to_kmh(rps_to_ms(wd.average_rps))); + return_string += delimiter + String(ms_to_kmh(rps_to_ms(wd.max_rps))); + return_string += delimiter + String(ms_to_kmh(rps_to_ms(wd.min_rps))); + return_string += delimiter + String(ms_to_knots(rps_to_ms(wd.average_rps))); + return_string += delimiter + String(ms_to_knots(rps_to_ms(wd.max_rps))); + return_string += delimiter + String(ms_to_knots(rps_to_ms(wd.min_rps))); + return_string += delimiter + String(heading); + return_string += delimiter + heading_to_abbr(heading); #ifdef RTC_CLOCK - return_string += get_time_stamp(); + return_string += delimeter + get_time_stamp(); #endif return return_string;