working project, maybe

This commit is contained in:
2019-08-22 19:24:35 +02:00
parent 8689dd8c63
commit a454a4aac5
152 changed files with 43419 additions and 316 deletions

View File

@@ -11,59 +11,6 @@
TODO: Rainrate going bonkers, check that shite.
influxDB SCHEMA:
DB weather
measure wind
----------------
value | speed or direction or windgust
---------------------------------------
field tag
measure temphumi
----------------
temperature | humidity | external, internal | pressure
---------------------------------------------------------
field field tag field
measure rain
----------------
rain | rate / total / intensity | restart if zero, was it 65535 => before?
---------------------------------------------
field tag field(int)
DB status
ISS measure
----------------
voltage | solar or capacitor | state / lqi / | battery or future_shit |
----------------------------------------------------------------
field tag field tag
RasPI system
----------------
usage | disk, mem, cpu, eth, wifi %
------------------------------------
field | tag
#
TABLE vantage_vue_iss
----------------------
t_stamp | voltage | type | lqi | rssi | batt_low
------------------------------------------------------------------
INT | INT | VARCHAR(20) | TINYINT | TINYINT | BOOL
CREATE TABLE raspi(
t_stamp INT,
count INT,
type VARCHAR(20),
nic VARCHAR(20),
host VARCHAR(50));
--------------------------------------------------------------------------------
Import libraries
--------------------------------------------------------------------------------
@@ -73,8 +20,6 @@ import requests
import sys
import os
import re
import textwrap
import argparse
import time
import datetime
import serial
@@ -83,8 +28,8 @@ import influxdb
# optionally, future modules, locally available, I hate dependencies
from pprint import pprint
_SCRIPT_PATH = os.path.dirname(sys.argv[0])
sys.path.append(_SCRIPT_PATH + "/home/pi/test/lib")
_SCRIPT_PATH = os.path.dirname(os.path.abspath(sys.argv[0]))
sys.path.append(_SCRIPT_PATH + "/lib")
#print(_SCRIPT_PATH + "/lib")
#import ventilLogger
@@ -101,15 +46,16 @@ _LOG_DIR = _SCRIPT_PATH + '/log/'
_LOG_FILE_ROOT = re.sub(u'./', '', sys.argv[0])
_LOG_FILE = _LOG_DIR + _LOG_FILE_ROOT + u'.log'
_DEBUG_FILE = _LOG_DIR + _LOG_FILE_ROOT + u'.dbg'
_DAVIS_SERIAL_DEVICE = '/dev/ttyUSBdavis'
# finite loop implementation, tout for 43200 cycles
tout = 0
_ABS_ZERO = 273.15
#_ABS_ZERO = 273.15
_HEIGHT = 455
temp = {}
wind = {}
humidity = 0
pressure_adjusted = 0
#pressure_adjusted = 0
supercap = 0
solarvolt = 0
rainstate = 0
@@ -127,7 +73,7 @@ status_db = 'status'
'''
--------------------------------------------------------------------------------
Set up logging - disabled, need to enable this iin future
Set up logging - disabled, need to enable this in future
--------------------------------------------------------------------------------
'''
@@ -137,59 +83,7 @@ status_db = 'status'
Setup arguments and Options - not edited, sample shite
--------------------------------------------------------------------------------
'''
desc = u'''\
DESCRIPTION:
Vantage Vue wireless data transfer decoder, V2
consult http://wp.spoton.cz/2017/11/24/davis-vantague-vue-arduino-and-a-raspberry-pi-3/
for wtf is going on
'''
epi = u'''\
ERROR CODES:
?
EXAMPLES:
?
'''
formatter = argparse.RawDescriptionHelpFormatter
arg_parser = argparse.ArgumentParser(description = desc,
formatter_class = formatter,
epilog = textwrap.dedent(epi))
arg_parser.add_argument('-d', '--details',
help = 'help',
action='store_true')
arg_parser.add_argument('-v', '--verbose',
help = 'help',
action='store_true')
arg_parser.add_argument('-p', '--section',
dest = 'section',
default = ['last', 'count', 'diff'],
choices = ['last', 'count', 'diff'],
nargs = '+',
type = str,
help = 'help')
arg_parser.add_argument('-s', '--snapusage',
help = 'help',
action='store_true')
args = arg_parser.parse_args()
if args.details:
_details = True
else:
_details = False
if args.verbose:
_more_details = True
else:
_more_details = False
if args.snapusage:
_SNAP_USAGE = True
else:
_SNAP_USAGE = False
try:
_sections = args.sections
except:
_sections = ['last', 'count', 'diff']
'''
--------------------------------------------------------------------------------
Generic, standalone functions
@@ -204,31 +98,6 @@ influx_status_client = influxdb.client.InfluxDBClient(
influx_host, influx_port, influx_user, influx_pwd, status_db
)
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
def create_project(conn, project):
"""
Create a new project into the projects table
:param conn:
:param project:
:return: project id
"""
sql = ''' INSERT INTO wind(name,begin_date,end_date)
VALUES(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, project)
return cur.lastrowid
'''
--------------------------------------------------------------------------------
@@ -242,13 +111,6 @@ class davisDecoder(object):
self.height = _HEIGHT
self.temp_dict = {}
def load_external_data(self):
# Data external to the ISS
self.pressure = float(davis_data['P'])
self.inside_temp = round((float(davis_data['Ti'])\
+ float(davis_data['Thtu'])) / 2, 2)
self.inside_hum = davis_data['Hhtu']
def zero_fill(self, data):
binary_data = format(int(data), '08b')
msb = binary_data[0:4]
@@ -285,12 +147,6 @@ class davisDecoder(object):
def decode_humidity(self, hum):
pass
def adjust_pressure(self, temp):
sh = 0.0065 * self.height
base = 1 - (sh) / (temp + sh + _ABS_ZERO)
result = round(self.pressure * pow(base, -5.257), 2)
return result
def supercap_decode(self, byte2, byte3):
cap = (byte2 << 2) + (byte3 >> 6)
result = float(cap / 100.00)
@@ -306,7 +162,7 @@ class davisDecoder(object):
return result
def rainrate_decode(self, byte2, byte3):
# if byte3(b2 here) is 0xFF, or 255, there is not rain
# if byte3(b2 here) is 0xFF, or 255, there is no rain
print("b2:{} b3:{} = result:{}".format(byte2, byte3, byte2 + (byte3 >> 4 << 8)))
if byte2 == 255:
rainstate = 0
@@ -370,14 +226,6 @@ class DBwriter(object):
"measurement": "wind",
"fields": { "value": base_value_dict['direction'] },
"tags": { "type": "direction" }
},
{
"measurement": "temphumi",
"fields": {
"temperature": base_value_dict['temperature'],
"humidity": base_value_dict['humidity']
},
"tags": { "type": "internal" }
}]
return base_connector
@@ -387,29 +235,12 @@ class DBwriter(object):
--------------------------------------------------------------------------------
'''
if '__main__':
'''
2 = Supercap voltage (Vue only)
3 = ?
4 = UV Index
5 = Rain rate
6 = Solar radiation
7 = Solar Cell output (Vue only)
8 = Temperature
9 = Wind gust
a = Humidity
e = Rain
{'nxt': 64, 'P0': 1020.43, 'lqi': 6, 'b2': 3, 'P': 969.29, 'h': 144, 'Ti': 24.49,
'cnt': 1, 'Hhtu': 28.68, 'b4': 129, 'b5': 247, 'b6': 36, 'b7': 255, 'b0': 1, 'b1': 10,
'hop': 0, 'b3': 225, 'Thtu': 24.28, 'b8': 255, 'b9': 182, 'rssi': 45}
'''
# TODO, make it work for any USB to serial port
davis_decoder = davisDecoder()
davis_writer = DBwriter()
try:
with serial.Serial('/dev/ttyUSBdavis', 9600) as davis:
with serial.Serial(_DAVIS_SERIAL_DEVICE, 9600) as davis:
# Now, let it run a couple times, end and restart via systemd
while tout < 400:
line = davis.readline()
@@ -437,13 +268,14 @@ if '__main__':
"winddir": raw_winddir})
# Get data external to the ISS, from local PCB / internal
# sensors and create base values for influx writing
davis_decoder.load_external_data()
#davis_decoder.load_external_data()
influx_weather_write = davis_writer.base_construct(
{ "speed": float(wind['speed']),
"direction": float(wind['direction']),
"temperature": float(davis_decoder.inside_temp),
"humidity": float(davis_decoder.inside_hum)}
"direction": float(wind['direction'])}
)
# "temperature": float(davis_decoder.inside_temp),
# "humidity": float(davis_decoder.inside_hum)}
#)
# Wind gusts calculation
if davis_packet_id == '0x9':
@@ -461,19 +293,19 @@ if '__main__':
raw_temp = (davis_data['b2'] << 8) + davis_data['b3']
temp_dict = davis_decoder.decode_temp(raw_temp)
temp = float(temp_dict['celsius'])
pressure_adjusted = davis_decoder.adjust_pressure(temp)
influx_weather_write = davis_writer.construct(
influx_weather_write,
"temphumi",
{"pressure": float(pressure_adjusted)},
{"type" : "adjusted"}
)
influx_weather_write = davis_writer.construct(
influx_weather_write,
"temphumi",
{"pressure": float(davis_decoder.pressure)},
{"type" : "raw"}
)
#pressure_adjusted = davis_decoder.adjust_pressure(temp)
#influx_weather_write = davis_writer.construct(
# influx_weather_write,
# "temphumi",
# {"pressure": float(pressure_adjusted)},
# {"type" : "adjusted"}
#)
#influx_weather_write = davis_writer.construct(
# influx_weather_write,
# "temphumi",
# {"pressure": float(davis_decoder.pressure)},
# {"type" : "raw"}
#)
influx_weather_write = davis_writer.construct(
influx_weather_write,
@@ -521,7 +353,6 @@ if '__main__':
# 0xe -> Rain bucket tips -> https://www.carluccio.de/
if davis_packet_id == '0xe':
raw_rain = (davis_data['b2']) + (davis_data['b3'] >> 7 << 8)
#raw_rain = davis_data['b2']
rain = davis_decoder.rain_decode(raw_rain)
influx_weather_write = davis_writer.construct(
influx_weather_write,
@@ -561,22 +392,17 @@ if '__main__':
temp,
wind,
humidity)
out2 = "Padj: {}, Praw {}, Tins: {}, Humins: {}".format(
pressure_adjusted,
davis_decoder.pressure,
davis_decoder.inside_temp,
davis_decoder.inside_hum)
out3 = "RainState: {}, Rrate {}, Rain Total: {}, Cap:{}, Volt: {}".format(
out2 = "RainState: {}, Rrate {}, Rain Total: {}, Cap:{}, Volt: {}".format(
rainstate,
rainrate,
rain,
supercap,
solarvolt)
out4 = {'RAW': davis_data}
with open('/home/pi/davis.rawdata', 'a+') as fh:
now = datetime.datetime.strftime(datetime.datetime.now(), "%s")
fh.write(now + ";" + str(out4) + "\n")
#print("\n{} \n{} \n{} \n{}\n{}\n".format(out_id, out1, out2, out3, out4))
out3 = {'RAW': davis_data}
#with open('/home/pi/davis.rawdata', 'a+') as fh:
# now = datetime.datetime.strftime(datetime.datetime.now(), "%s")
# fh.write(now + ";" + str(out4) + "\n")
print("\n{} \n{} \n{} \n{}\n".format(out_id, out1, out2, out3))
# Write the whole blob into Influx DB
influx_weather_client.write_points(influx_weather_write)