Skip to content

Instantly share code, notes, and snippets.

@Ellsworth
Created January 25, 2023 03:27
Show Gist options
  • Save Ellsworth/bda70f2cbd4b288d106411d444253ace to your computer and use it in GitHub Desktop.
Save Ellsworth/bda70f2cbd4b288d106411d444253ace to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from influxdb import InfluxDBClient
# Setup InfluxDB client
client = InfluxDBClient("hostname_here", "8086", "user", "pass", "database_name")
import argparse, os
from vedirect import Vedirect
'''
* CS - Operation State
* ERR - Error Codes
* VPV - Solar Voltage (mV)
* PPV - Panel Wattage (W)
* V - Battery Voltage (mV)
* I - Battery Current (mA)
* HSDS - Day Sequence (0-364)
* H23 - Max Power Yesterday (W)
* H21 - Max Power Today (W)
* H19 - Total Yield (kWh)
* H20 - Yield Today (kWh)
* H22 - Yield Yesterday (KWh)
'''
def updateDashboard(packet):
#print(packet)
json_body = [
{
"measurement": "SOLAR",
"tags": {
"location": "testing",
},
"fields": {
"OP_STATE": packet["CS"],
"ERROR_CODE": packet["ERR"],
"SOLAR_VOLTAGE": float(packet["VPV"]) / 1000,
"PANEL_WATTAGE": int(packet["PPV"]),
"BATTERY_VOLTAGE": float(packet["V"]) / 1000,
"BATTERY_CURRENT": float(packet["I"]) / 1000,
"kWh_TODAY": float(packet["H20"]) * 10,
"kWh_YESTERDAY": float(packet["H22"]) * 10,
}
}
]
print(json_body)
client.write_points(json_body)
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()
ve = Vedirect(args.port, args.timeout)
#print(ve.read_data_callback(print_data_callback))
while True:
for _ in range(15):
ve.read_data_single()
updateDashboard(ve.read_data_single())
#print(ve.read_data_callback(updateDashboard))
print('Sending update...')
updateDashboard(ve.read_data_single())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment