diff --git a/app.py b/app.py new file mode 100644 index 0000000..29e1180 --- /dev/null +++ b/app.py @@ -0,0 +1,66 @@ +from flask import Flask, jsonify, render_template +from pysolarmanv5 import PySolarmanV5 +import config + +app = Flask(__name__) + +def get_inverter_data(): + """ + Connects to the inverter and reads voltage registers. + Returns a dictionary with the data. + """ + try: + inverter = PySolarmanV5( + config.INVERTER_IP, + config.INVERTER_SERIAL, + port=config.INVERTER_PORT, + mb_slave_id=config.INVERTER_SLAVE_ID, + verbose=False + ) + + # Registers to read: + # 150: Grid Voltage (Input Voltage) - 0.1V + # 154: Output Voltage - 0.1V + # 157: Load Voltage - 0.1V + + # Reading individually for simplicity, though block read is more efficient if contiguous + # But these are slightly apart (150, 154, 157). + # We can read a block from 150 to 157 (8 registers) and parse. + + # Let's read block starting at 150, length 8. + # 150 -> Index 0 + # 154 -> Index 4 + # 157 -> Index 7 + + raw_data = inverter.read_holding_registers(register_addr=150, quantity=8) + + input_voltage = raw_data[0] * 0.1 + output_voltage = raw_data[4] * 0.1 + load_voltage = raw_data[7] * 0.1 + + inverter.disconnect() + + return { + "input_voltage": round(input_voltage, 1), + "output_voltage": round(output_voltage, 1), + "load_voltage": round(load_voltage, 1), + "status": "success" + } + + except Exception as e: + return { + "status": "error", + "message": str(e) + } + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/api/data') +def api_data(): + data = get_inverter_data() + return jsonify(data) + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') diff --git a/config.py b/config.py new file mode 100644 index 0000000..d693b40 --- /dev/null +++ b/config.py @@ -0,0 +1,6 @@ +# Inverter Configuration +# Replace with your actual Inverter IP and Serial Number +INVERTER_IP = "192.168.1.X" +INVERTER_SERIAL = 1234567890 +INVERTER_PORT = 8899 +INVERTER_SLAVE_ID = 1 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0907901 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +pysolarmanv5 diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0dcbe2c --- /dev/null +++ b/templates/index.html @@ -0,0 +1,203 @@ + + +
+ + +