Hi,
when browsing through the forum I saw this post: Monitor Android Battery Level with Pimatic.
A few month ago I had a problem when I needed to monitor the battery level of my “kitchen tablet”. I have an old Samsung Galaxy Tab integrated into the wallboard in the kitchen an it is always on and I wanted that the battery only is loading when the battery level is low. So I put a switch there which I can control via pimatic. To automate this switch I needed a tool to send the battery level to pimatic. As I don’t use tasker (and I didn’t even know about that tool at this time) I searched for a solution. So I wrote a little app with cordova that can be installed on android devices.
It’s a very basic app which does nothing else then sending the battery level in a variable to a script on a server that can be defined in a text field.
It also runs in the background and saves the configuration to the card.
Here’s a screenshot of the app:
Screenshot
This script is running on my server to update the variable, the feedback of this script is shown in the field “Ergebnis letztes Update” of the app, in my case it’s a php script, but it could be any other language:
<?php
error_reporting(1);
/* Definition der Variablen */
//username and password of account
$username = ""; //username pimatic
$password = ""; //password pimatic
//login form action url
$url="http://PIMATIC_URL:PIMATIC_PORT/api/variables/" . $_POST["var"];
$path = "/tmp";
$cookie_file_path = $path."/cookie2.txt";
$debug = true;
if ($debug) file_put_contents("putvar.log", date('Y-m-d H:i:s') . "\n", FILE_APPEND); //Debug
/* Auslesen der POST-Variablen */
$json_string = $_POST["valueOrExpression"];
if ($debug) file_put_contents("putvar.log", implode(", ", array_keys($_POST)) . " => " . implode(", ", $_POST) . "\n", FILE_APPEND); //Debug
/* Umwandeln der POST-Daten in json */
$data = array("type" => "value", "valueOrExpression" => $json_string);
$data_string = json_encode($data);
/* Senden der Daten via cUrl */
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
$json2 = curl_exec($ch);
if ($debug) file_put_contents("putvar.log", $json2 . "\n", FILE_APPEND);
echo date('Y-m-d H:i:s') . " \n " . $json2;
curl_close($ch);
?>
If anybody wants the apk please let me know.
But I cannot garantee that it runs on every android version, I tested it with CyanogenMod 10 (Android 4.2.2), Android 4.4 and 5.
Martin