Skip to content

Instantly share code, notes, and snippets.

@djdunc
Created February 4, 2024 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djdunc/58b11f8e0dd00cdf8ebb5569df32e96c to your computer and use it in GitHub Desktop.
Save djdunc/58b11f8e0dd00cdf8ebb5569df32e96c to your computer and use it in GitHub Desktop.
script for sending solar inverter data to mqtt
import requests
from bs4 import BeautifulSoup
import paho.mqtt.client as mqtt
url = "http://192.168.1.232/monitor.htm"
username = 'xxx'
password = 'xxx'
# MQTT broker details
mqtt_broker = 'mqtt.cetools.org'
mqtt_port = 1884
mqtt_username = 'xxx'
mqtt_password = 'xxx'
client = mqtt.Client('dwsolar')
log_file_path = "solar.log"
def write_to_log(file_path, power, today, total):
try:
with open(file_path, 'w') as log_file:
log_file.write(f"Power Now: {power}, Todays Total Energy: {today}, Total Energy: {total}\n")
print("Variables written to log file successfully.")
except Exception as e:
print(f"An error occurred while writing to the log file: {e}")
def read_from_log(file_path):
try:
with open(file_path, 'r') as log_file:
content = log_file.read().strip()
if content:
# Extracting values from the log file content
parts = content.split(',')
power = float(parts[0].split(':')[-1].strip())
today = float(parts[1].split(':')[-1].strip())
total = float(parts[2].split(':')[-1].strip())
return power, today, total
else:
print("Log file is empty.")
except Exception as e:
print(f"An error occurred while reading and converting from the log file: {e}")
try:
# Make an HTTP GET request with authentication
response = requests.get(url, auth=(username, password))
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Make an HTTP GET request with authentication
response = requests.get(url, auth=(username, password))
# Process the response data here
# parse the HTML using BeautifulSoup
solarpage = BeautifulSoup(response.content, 'html.parser')
# Find the table with id 'displayme'
table = solarpage.find('table', {'id': 'displayme'})
# Find the rows in the table
rows = table.find_all('tr')
# Create a dictionary to store the values
data = {}
# Loop through each row and extract the values
for row in rows:
columns = row.find_all(['td', 'th'])
if len(columns) == 2:
key = columns[0].text.strip()
value = columns[1].text.strip()
data[key] = value
# Extract the values for 'Power Now' and 'Total Energy'
power_now = data.get('Power Now:', 'N/A').rstrip('W')
today_energy = data.get('Today\'s Energy:', 'N/A').rstrip('kWh')
total_energy = data.get('Total Energy:', 'N/A').rstrip('kWh')
print(f"URL : Power Now (W): {power_now}, Todays Total Energy (kWh): {today_energy}, Total Energy (kWh): {total_energy}")
# Write variables to the log file
write_to_log(log_file_path, power_now, today_energy, total_energy)
else:
print(f"Request failed with status code: {response.status_code}")
# Read and convert values from the log file
result = read_from_log(log_file_path)
if result:
power_now, today_energy, total_energy = result
power_now = 0 # I dont want to show the last value in the mqtt feed
print(f"File: Power Now (W): {power_now}, Todays Total Energy (kWh): {today_energy}, Total Energy (kWh): {total_energy}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
# Handle the error gracefully, e.g., by logging the error or taking appropriate action
# Read and convert values from the log file
result = read_from_log(log_file_path)
if result:
power_now, today_energy, total_energy = result
power_now = 0 # I dont want to show the last value in the mqtt feed
print(f"File: Power Now (W): {power_now}, Todays Total Energy (kWh): {today_energy}, Total Energy (kWh): {total_energy}")
# Even when inverter is offline I still want to send the last known data to MQTT
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_broker, mqtt_port, 60)
client.publish('personal/ucjtdjw/33ph/EM/solar/now/W', power_now)
client.publish('personal/ucjtdjw/33ph/EM/solar/today-total/kWh', today_energy)
client.publish('personal/ucjtdjw/33ph/EM/solar/total-since-11-2018/kWh', total_energy)
# Disconnect from MQTT broker
client.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment