Add custom_50x/solar_monitor.py

This commit is contained in:
smallsolar 2024-12-03 19:45:53 +00:00
parent 84d34f8b29
commit b886d2cfc9
1 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,68 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# https://github.com/karioja/vedirect
# Command: python3 solar_monitor.py --port /dev/ttyUSB0
import argparse
from vedirect import Vedirect
from datetime import datetime
# DB 1 is used as DB 0 is used by mastodon
import redis
r = redis.Redis(host='localhost', port=6379, db=1, decode_responses=True)
import logging
logging.basicConfig(encoding='utf-8', level=logging.INFO)
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.start()
def print_data_callback(packet):
r.set('batt_v', packet['V'])
r.set('main_current', packet['I'])
r.set('panel_voltage',packet['VPV'])
r.set('panel_power',packet['PPV'])
r.set('load_current',packet['IL'])
r.set('yield_today',packet['H20'])
r.set('max_power_today',packet['H21'])
r.set('yield_yesterday',packet['H22'])
r.set('max_power_yesterday',packet['H23'])
def write_html():
batt_v_V = int(r.get('batt_v')) / 1000
main_current = r.get('main_current')
panel_voltage = int(r.get('panel_voltage')) / 1000
panel_power = r.get('panel_power')
load_current = int(r.get('load_current')) / 1000
yield_today = int(r.get('yield_today')) / 100
max_power_today = r.get('max_power_today')
load_power = load_current * batt_v_V
Func = open("/usr/share/nginx/html/custom_50x.html","w")
# Adding input data to the HTML file
Func.write("<html>\n<head>\n<title> \nsolarcene.community \
</title>\n</head> <body><h1>solarcene.community is offline</h1>\
\n<p>solarcene.community is a solar powered webserver, if you are seeing this page it means that it is currently sleeping (as the battery is low), once the sun comes up it will be back online so please check back tomorrow.</p>\n \
\n<table border=\"1\"> <tr><td>Battery Voltage</td><td>{}V</td></tr> <tr><td>Panel Voltage</td><td>{}V</td></tr> <tr><td>Panel Power</td><td>{}W</td></tr> <tr><td>Load Power</td><td>{:.2f}W</td></tr> </table>\n \
\n <p><i>Last update: {} UTC</i></p></body></html>".format(batt_v_V, panel_voltage, panel_power, load_power, datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') ))
# Saving the data into the HTML file
Func.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process VE.Direct protocol')
parser.add_argument('--port', help='Serial port')
parser.add_argument('--timeout', help='Serial port read timeout', type=int, default='60')
args = parser.parse_args()
scheduler.add_job(write_html, 'interval', minutes=10)
ve = Vedirect(args.port, args.timeout)
logging.info('Starting...')
ve.read_data_callback(print_data_callback)
logging.info('Exit')