SMA Energy Meter

Aufbauend auf dem Skript habe ich mir ein eigenes geschrieben.

Vielleicht hilft es dir als Beispiel weiter:

<?php

declare(strict_types=1);

// Hier die LogIn Daten für das SunnyPortal eintragen:
$login_email = 'xxxx@yyyyy';
$login_pass = 'zzzzz';

define ('COOKIE_FILE_NAME', 'cookie_sma.txt');

$ch = curl_init();

// Versuch sich mit Cookie an der Webseite anzumelden
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,'https://www.sunnyportal.com/FixedPages/HoManLive.aspx');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_NAME);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36');
$curlData = curl_exec($ch);


// Prüfen, ob die Anmeldung mit Cookie erfolgreich war, wenn nicht, dann wird ein normaler Login durchgeführt
preg_match('|(Forgotten password)|', $curlData, $LoginFALSE);
if ($LoginFALSE){
    echo "Anmeldung mit Cookie war nicht erfolgreich - normaler Login wird durchgeführt!".PHP_EOL;

    curl_setopt($ch, CURLOPT_URL, 'https://www.sunnyportal.com/Templates/Start.aspx');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'ctl00$ContentPlaceHolder1$Logincontrol1$txtUserName='.urlencode($login_email).'&ctl00$ContentPlaceHolder1$Logincontrol1$txtPassword='.urlencode($login_pass).'&__EVENTTARGET=ctl00$ContentPlaceHolder1$Logincontrol1$LoginBtn');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_NAME);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    preg_match('|(Current Status and Forecast)|', curl_exec($ch), $LoginTRUE);

    if (!$LoginTRUE){
        echo 'Die Anmeldung war nicht erfolgreich - bitte User und Passwort überprüfen.' . PHP_EOL;
        return;
    }
}

// Abfragen der Live Daten
curl_setopt($ch, CURLOPT_URL,"https://www.sunnyportal.com/homemanager?t=" . time());
curl_setopt($ch, CURLOPT_POST, false);
$curlData = curl_exec($ch);

$LiveData = [];
foreach(json_decode($curlData, true) as $key=>$Data){
    switch ($key){
        case '__type':
            break;
        case 'Timestamp':
            $LiveData[$key] = strtotime($Data['DateTime']);
            break;
        default:
            $LiveData[$key] = $Data;            
    }
} 

if ($_IPS['SENDER'] === 'Execute'){
    print_r($LiveData);
}


// Abfragen der Prognose Daten
curl_setopt($ch, CURLOPT_URL,"https://www.sunnyportal.com/HoMan/Forecast/LoadRecommendationData");
curl_setopt($ch, CURLOPT_POST, false);
$curlData = curl_exec($ch);

$ForecastSeries = json_decode($curlData, true)['ForecastSeries'];

$DataPoints = [];
foreach($ForecastSeries as $Serie){
    $PvMeanPower = round($Serie['PvMeanPower']['Amount']/1000, 2);
    $ConsumptionForecast = round($Serie['ConsumptionForecast']['Amount']/3600000, 2);
    $DataPoints[] = ['TimeStamp' => $Serie['TimeStamp']['DateTime'], 'WeatherId' => $Serie['WeatherId'], 'PvMeanPower' => $PvMeanPower, 'PvEnergy' => $Serie['PvEnergy']['Amount'], 'ConsumptionForecast' => $ConsumptionForecast, 'IsConsumptionRecommended' => $Serie['IsConsumptionRecommended']];
} 

if ($_IPS['SENDER'] === 'Execute'){
    print_r($DataPoints);
}

curl_close($ch);

Es erfolgt hier nur eine Anzeige der Daten bei Direktausführung. Um die Speicherung der Daten muss man sich noch selber kümmern.

Burkhard