feat: Implement a Deye inverter monitoring web application with Flask, pysolarmanv5, and a basic HTML dashboard.
This commit is contained in:
66
app.py
Normal file
66
app.py
Normal file
@@ -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')
|
||||
6
config.py
Normal file
6
config.py
Normal file
@@ -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
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
flask
|
||||
pysolarmanv5
|
||||
203
templates/index.html
Normal file
203
templates/index.html
Normal file
@@ -0,0 +1,203 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Deye Inverter Monitor</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg-color: #1a1a1a;
|
||||
--card-bg: #2d2d2d;
|
||||
--text-color: #e0e0e0;
|
||||
--accent-color: #00d2ff;
|
||||
--secondary-accent: #3a7bd5;
|
||||
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: var(--bg-color);
|
||||
color: var(--text-color);
|
||||
font-family: var(--font-family);
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-bottom: 40px;
|
||||
font-weight: 300;
|
||||
letter-spacing: 2px;
|
||||
text-transform: uppercase;
|
||||
background: linear-gradient(45deg, var(--accent-color), var(--secondary-accent));
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
}
|
||||
|
||||
.dashboard {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
||||
gap: 20px;
|
||||
width: 100%;
|
||||
max-width: 1000px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background-color: var(--card-bg);
|
||||
border-radius: 15px;
|
||||
padding: 25px;
|
||||
text-align: center;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
|
||||
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.card:hover {
|
||||
transform: translateY(-5px);
|
||||
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.card::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 4px;
|
||||
background: linear-gradient(90deg, var(--accent-color), var(--secondary-accent));
|
||||
}
|
||||
|
||||
.card h2 {
|
||||
margin: 0 0 15px 0;
|
||||
font-size: 1.2rem;
|
||||
color: #aaa;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.value {
|
||||
font-size: 2.5rem;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.unit {
|
||||
font-size: 1rem;
|
||||
color: #888;
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
.status-bar {
|
||||
margin-top: 40px;
|
||||
padding: 10px 20px;
|
||||
border-radius: 20px;
|
||||
background-color: var(--card-bg);
|
||||
font-size: 0.9rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.status-indicator {
|
||||
display: inline-block;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 50%;
|
||||
background-color: #555;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.status-indicator.active {
|
||||
background-color: #00ff88;
|
||||
box-shadow: 0 0 10px #00ff88;
|
||||
}
|
||||
|
||||
.status-indicator.error {
|
||||
background-color: #ff4444;
|
||||
box-shadow: 0 0 10px #ff4444;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% { opacity: 1; }
|
||||
50% { opacity: 0.5; }
|
||||
100% { opacity: 1; }
|
||||
}
|
||||
|
||||
.loading .value {
|
||||
animation: pulse 1.5s infinite;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Deye Inverter Monitor</h1>
|
||||
|
||||
<div class="dashboard">
|
||||
<div class="card" id="card-input">
|
||||
<h2>Grid Voltage (Input)</h2>
|
||||
<div class="value-container loading">
|
||||
<span class="value" id="input-voltage">--</span><span class="unit">V</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" id="card-output">
|
||||
<h2>Output Voltage</h2>
|
||||
<div class="value-container loading">
|
||||
<span class="value" id="output-voltage">--</span><span class="unit">V</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" id="card-load">
|
||||
<h2>Load Voltage</h2>
|
||||
<div class="value-container loading">
|
||||
<span class="value" id="load-voltage">--</span><span class="unit">V</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="status-bar">
|
||||
<span class="status-indicator" id="status-dot"></span>
|
||||
<span id="status-text">Connecting...</span>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const inputVoltageEl = document.getElementById('input-voltage');
|
||||
const outputVoltageEl = document.getElementById('output-voltage');
|
||||
const loadVoltageEl = document.getElementById('load-voltage');
|
||||
const statusDot = document.getElementById('status-dot');
|
||||
const statusText = document.getElementById('status-text');
|
||||
const valueContainers = document.querySelectorAll('.value-container');
|
||||
|
||||
async function fetchData() {
|
||||
try {
|
||||
const response = await fetch('/api/data');
|
||||
const data = await response.json();
|
||||
|
||||
if (data.status === 'success') {
|
||||
inputVoltageEl.textContent = data.input_voltage;
|
||||
outputVoltageEl.textContent = data.output_voltage;
|
||||
loadVoltageEl.textContent = data.load_voltage;
|
||||
|
||||
statusDot.className = 'status-indicator active';
|
||||
statusText.textContent = 'Connected';
|
||||
|
||||
valueContainers.forEach(el => el.classList.remove('loading'));
|
||||
} else {
|
||||
throw new Error(data.message || 'Unknown error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
statusDot.className = 'status-indicator error';
|
||||
statusText.textContent = 'Error: ' + error.message;
|
||||
// Don't reset values to '--' immediately to avoid flickering if it's just a transient network blip,
|
||||
// but maybe indicate stale data if needed. For now, keep last known values.
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch immediately, then every 5 seconds
|
||||
fetchData();
|
||||
setInterval(fetchData, 5000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user