query("SET NAMES utf8");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$readingsQuery = $pdo->query("SELECT * FROM log ORDER BY time DESC LIMIT 1");
$readings = $readingsQuery->fetch();
$time = format_date_time($readings[1]);
$solarVolts = $readings[2];
$solarAmps = round($readings[3],1);
$solarWatts = round($readings[4],0);
$batteryVolts = $readings[5];
$batteryAmps = round($readings[6],1);
$batteryLevel = $readings[7];
$loadAmps = round($readings[8],1);
$loadWatts = round($readings[9],0);

$infoQuery = $pdo->query("SELECT * FROM info");
$info = $infoQuery->fetchAll(PDO::FETCH_KEY_PAIR);

// Modify the battery level
$batteryWidth = ($batteryVolts - 11.3) * (100 - 0) / (12.8 - 11.3) + 0;
if ($batteryWidth > 100) $batteryWidth = 100;
$batteryLevelModified = round(($batteryWidth+$batteryLevel) / 2);

echo '{"time":"'.$time.'",
		"sV":'.$solarVolts.',
		"sI":'.$solarAmps.',
		"sP":'.$solarWatts.',
		"bV":'.$batteryVolts.',
		"bI":'.$batteryAmps.',
		"battLevel":'.$batteryLevelModified.',
		"load":"'.$info['loadstate'].'",
		"lI":'.$loadAmps.',
		"lP":'.$loadWatts.'}';
		
$pdo = null;

?>