Skip to content

Instantly share code, notes, and snippets.

@atarp
Last active May 3, 2024 12:17
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save atarp/11dfe096a554db81d94615e3bfc65424 to your computer and use it in GitHub Desktop.
Save atarp/11dfe096a554db81d94615e3bfc65424 to your computer and use it in GitHub Desktop.
Python script to read data from PPC Smart Meter Gateway Customer Interface (SMGW) via the HAN interface
#!/usr/bin/env python3
import requests, base64
from requests.auth import HTTPDigestAuth
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
user= '12345678'
password = 'secret'
url = 'https://192.168.1.200/cgi-bin/hanservice.cgi'
s = requests.Session()
res=s.get(url,auth=HTTPDigestAuth(user, password),verify=False)
cookies = { 'Cookie' : res.cookies.get('session')}
soup = BeautifulSoup(res.content, 'html.parser')
tags = soup.find_all('input')
token = tags[0].get('value')
action = 'meterform'
post_data = "tkn=" + token + "&action=" + action
res = s.post(url, data=post_data,cookies=cookies,verify=False)
soup = BeautifulSoup(res.content, 'html.parser')
sel = soup.find(id='meterform_select_meter')
meter_val = sel.findChild()
meter_id = meter_val.attrs.get('value')
post_data = "tkn=" + token + "&action=showMeterProfile&mid=" + meter_id
res= s.post(url, data=post_data,cookies=cookies,verify=False)
soup = BeautifulSoup(res.content, 'html.parser')
table_data = soup.find('table', id="metervalue")
result_data = {
'value': table_data.find(id="table_metervalues_col_wert").string,
'unit': table_data.find(id="table_metervalues_col_einheit").string,
'timestamp': table_data.find(id="table_metervalues_col_timestamp").string,
'isvalid': table_data.find(id="table_metervalues_col_istvalide").string,
'name': table_data.find(id="table_metervalues_col_name").string,
'obis': table_data.find(id="table_metervalues_col_obis").string
}
print(result_data['timestamp']+ " " + result_data['value']+ " " + result_data['unit'])
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment