Zählerstände auslesen
"Januar", 2 => "Februar", 3 => "März", 4 => "April", 5 => "Mai", 6 => "Juni", 7 => "Juli", 8 => "August", 9 => "September", 10 => "Oktober", 11 => "November", 12 => "Dezember");
$start_year = 2016;
$current_year = date("Y");
// Generating Year list to the current data
echo '';
$username="pi";
$password="raspberry";
$db_server="localhost";
$channel_id_electric="6";
$channel_id_heat="11";
try {
$conn = new PDO("mysql:host=" . $db_server . ";dbname=volkszaehler", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
echo "" . $year . "
";
echo "";
echo "Datum Stromzählerstand Wärmezählerstand ";
foreach ($months as $month => $month_name) {
$last_day = date("t", mktime(0, 0, 0, $month, 1, $year));
$str_from = "$year-$month-$last_day 0:00:00";
$date1 = new DateTime($str_from);
$time_from = $date1->getTimestamp() * 1000;
$str_to = "$year-$month-$last_day 23:59:59";
$date2 = new DateTime($str_to);
$time_to = $date2->getTimestamp() * 1000;
$str_sql_electric = "SELECT value FROM data WHERE channel_id=$channel_id_electric AND timestamp BETWEEN $time_from AND $time_to";
$str_sql_heat = "SELECT value FROM data WHERE channel_id=$channel_id_heat AND timestamp BETWEEN $time_from AND $time_to";
$sql_res = $conn->prepare($str_sql_electric);
$sql_res->execute();
$rows_electric = $sql_res->fetchAll();
$sql_res = $conn->prepare($str_sql_heat);
$sql_res->execute();
$rows_heat = $sql_res->fetchAll();
echo "" . $last_day . ". " . $month_name . " " . number_format(end($rows_electric)["value"]/1000, 0, ',', '.') . " " . number_format(end($rows_heat)["value"]/1000, 0, ',', '.') . " ";
}
echo "
";
?>
Frühere Version mit Web Adresse im lokalen Netzwerk:
Stromzaehler auslesen
format('Y-m-d H:i:s') . "\n";
$time_from = $date1->getTimestamp() * 1000;
//echo $time_from . "\n";
$str_to = "$year-$month-01 08:15:00";
$date2 = new DateTime($str_to);
$time_to = $date2->getTimestamp() * 1000;
//echo $time_to . "\n";
$username="root";
$password="raspberry";
$channel_id="11";
$str_sql = "SELECT value FROM data WHERE channel_id=$channel_id AND timestamp BETWEEN $time_from AND $time_to LIMIT 0, 1";
//echo $str_sql . "\n";
$db = mysql_connect('localhost', $username, $password);
if (!$db) {
die('Verbindung schlug fehl: ' . mysql_error());
}
$db_vz = mysql_select_db('volkszaehler', $db);
$sql_res = mysql_query($str_sql);
$row = mysql_fetch_assoc($sql_res);
echo ($row["value"]/1000) . " KWh, ";
echo "$year-$month-01 08:00 \n";
?>