Ciao a tutti, mi sono permesso di apportare delle modifiche a reqshelly. Ovviamente mi farebbe piacere un vostro parere in quanto NON SO PROGRAMMARE
Come scritto in precedenza reqshelly (almeno su shellyplug, ma penso anche con gli altri shelly) ha 2 problemi.
Il primo riguarda la rilevazione dei consumi che su shelly sono in W-minuto e non Wh, questo semplicemente si corregge dividendo per 60 il valore letto....
Il secondo è più problematico in quanto se va via la corrente a shelly il contatore parte da zero. Quindi ho modificato lo script prendendo spunto da poolerboiler che legge dal CSV l'ultima lettura rilevata e creando un file di appoggio con l'ultima lettura di shelly per effettuare dei confronti....
Le modifiche le ho inserite in un file "incluso" in reqshelly per mia comodità
Qui il file reqshelly da me modificato:
Codice: Seleziona tutto
#!/usr/bin/php
<?php
// This script will output a meterN compatible format for read Shelly 2.5 and Shelly 1PM device
//
// Script reqshelly.php v1.0 by Flanesi
//
// You'll need to setup correct permission chmod +x
// then ln -s /var/www/comapps/reqshelly.php /usr/local/bin/reqshelly
// Request command with 'reqshelly IP power0 IDmetern' or 'reqshelly IP energy0 IDmetern'
if (isset($_SERVER['REMOTE_ADDR'])) {
die('Direct access not permitted');
}
if (!isset($argv[1]) or !isset($argv[2]) or !isset($argv[3])) {
die("Abording: no valid argument given.\nUsage: reqshelly IP (power0|power1|energy0|energy1) IDmetern\n");
} elseif ($argv[2] == 'power0') {
$url = "http://"."$argv[1]"."/status";
$pagina = file_get_contents($url);
$json_output = json_decode($pagina, true);
$val = $json_output['meters'][0]['power'];
$outstr = "$argv[3]($val*W)";
} elseif ($argv[2] == 'power1') {
$url = "http://"."$argv[1]"."/status";
$pagina = file_get_contents($url);
$json_output = json_decode($pagina, true);
$val = $json_output['meters'][1]['power'];
$outstr = "$argv[3]($val*W)";
} elseif ($argv[2] == 'energy0') {
$url = "http://"."$argv[1]"."/status";
$pagina = file_get_contents($url);
$json_output = json_decode($pagina, true);
$val = $json_output['meters'][0]['total'];
include("/var/www/comapps/shellymario.php");
$outstr = "$argv[3]($val*W)";
} elseif ($argv[2] == 'energy1') {
$url = "http://"."$argv[1]"."/status";
$pagina = file_get_contents($url);
$json_output = json_decode($pagina, true);
$val = $json_output['meters'][1]['total'];
include("/var/www/comapps/shellymario.php");
$outstr = "$argv[3]($val*W)";
// and so on ....
} else {
die("Usage: reqshelly IP (power0|power1|energy0|energy1) IDmetern\n");
}
echo "$outstr";
?>
mentre il file shellymario.php contiene le istruzioni per "correggere" i problemi....
ho creato una cartella sotto metern per contenere i file di appoggio: /var/www/metern/data/shelly
Codice: Seleziona tutto
<?php
$pathtomn = '/var/www/metern';
$dirshelly = '/var/www/metern/data/shelly';
$metnum = $argv[3]; // Meter ID Consumi
$fileshelly="$dirshelly"."/shelly"."$metnum.txt";
define('checkaccess', TRUE);
include("$pathtomn/config/config_main.php");
include("$pathtomn/config/config_met$metnum.php");
$shelly_energy_prec = file_get_contents($fileshelly);
// acquisisce il valore precedente dal csv
$dir = '/var/www/metern/data/csv';
$output = array();
$output = glob($dir . '/*.csv');
sort($output);
$cnt = count($output);
if (file_exists($output[$cnt - 1])) {
$file = file($output[$cnt - 1]); // today
$contalines = count($file);
if ($contalines > 1) {
$prevarray = preg_split("/,/", $file[$contalines - 1]);
} elseif ($contalines == 1 && file_exists($output[$cnt - 2])) { // yesterday, only header
$file = file($output[$cnt - 2]);
$contalines = count($file);
$prevarray = preg_split("/,/", $file[$contalines - 1]);
}
$cons_val_first = trim($prevarray[$metnum]);
} else {
$cons_val_first = null;
}
$shelly_energy = round($val/60, ${'PRECI' . $metnum});
$shelly_energy_csv = $cons_val_first;
settype($lastval, 'float');
settype($prevcount, 'float');
settype($cons_val_first, 'float');
settype($shelly_energy, 'float');
settype($shelly_energy_csv, 'float');
settype($shelly_energy_prec, 'float');
if ($shelly_energy < $shelly_energy_prec) {
$val = $shelly_energy + $shelly_energy_csv;
file_put_contents("$dirshelly/shelly$metnum.txt", $shelly_energy);
} ELSE {
$val = $shelly_energy + $shelly_energy_csv - $shelly_energy_prec;
file_put_contents("$dirshelly/shelly$metnum.txt", $shelly_energy);
}
$val = round($val, ${'PRECI' . $metnum});
?>
Attendo un vs parere, intanto l'ho testato da qualche giorno e funziona correttamente.