poolmeters mini-daemon

Post Reply
jeanmarc
Posts: 1832
Joined: Thu Aug 29, 2013 7:16 am

poolmeters mini-daemon

Post by jeanmarc » Thu Apr 17, 2014 9:15 am

Hi,

With the good advice of Roland, i've redesign the poolmeters to make it work as a daemon. It is way more faster and less CPU intensive.
The idea, is open the port once and continuously fill a ramdisk. meterN will just have to grep the values in a text file.

Don't forget to create a ramdisk or you'll have amazing IO access. The com. with the arduino is so fast that it need a sleep to reduce usage.

Code: Select all

#!/usr/bin/env python
# If running as http user, you'll need to add it to your com. group 
# ls -l /dev/ttyACM*
# crw-rw---- 1 root uucp 188, 0 Feb 18 09:02 /dev/ttyACM0
# usermod -aG uucp http
# Then create a ramdisk :
# https://wiki.archlinux.org/index.php/Ramdisk

import serial, sys
import time
import glob

PORT = (glob.glob("/dev/ttyACM*"))
ser = serial.Serial(PORT[0], 921600, timeout=10)

while True:
	ser.write("le\n".encode("utf-8")) # Elect live
	line = ser.readline(16).decode("utf-8")
	ser.write("gs\n".encode("utf-8")) # Gas state
	line += ser.readline(16).decode("utf-8")
	ser.write("ws\n".encode("utf-8"))  # Water state
	line += ser.readline(16).decode("utf-8")
	f = open('/mnt/ramdisk/metern.txt','w')
	f.write(line)
	time.sleep(0.5)
f.close()
ser.flushInput()
ser.flushOutput()
ser.close()
Then, in meterN, as live command more /mnt/ramdisk/metern.txt | grep "*W)". Simple and effective 8-)

Now for the main command, pooler need to close the live pooling and restart it when the arduino counter have been reseted. That why it's a mini-daemon as it run for a 5 min period.

Code: Select all

#!/usr/bin/php
<?php
// This script will output a meterN compatible format for the main command
// You'll need to setup the path to meterN ($pathtomn). Put the meters ID ($metnum) and the corresponding command ($cmd) :

$pathtomn = '/srv/http/metern';
$port = trim(shell_exec("ls /dev/ttyACM*")); // Detect the arduino port

$output     = shell_exec('pkill -f poolmeters.py > /dev/null 2>&1 &'); // Kill temporary the "live values fetching"

if (isset($argv[1])) {
if ($argv[1] == 'elect') {
    $metnum  = 1;
    $cmd = "arduino-serial -q -p $port -S 'relect' -r";
} elseif ($argv[1] == 'gas') {
    $metnum  = 2;
    $cmd = "arduino-serial -q -p $port -S 'rgas' -r";
} elseif ($argv[1] == 'water') {
    $metnum  = 3;
    $cmd = "arduino-serial -q -p $port -S 'rwater' -r";
} else {
    die('Abording: no valid argument given\n');
}
} else {
    die('Usage: pooler { elect | gas | water }\n');
}
// End of setup

include("$pathtomn/scripts/read_mem.php");
// Retrieve previous value in the last daily csv
$dir    = "$pathtomn/data/csv";
$output = glob($dir . '/*.csv');
sort($output);
$cnt        = count($output);
$file       = file($output[$cnt - 1]);
$cnt = count($file);

$i=0;
while(!isset($prevval)) {
$i++;
$array      = preg_split('/,/', $file[$cnt - $i]);
	if (!empty($array[$metnum])) {
    $prevval = $array[$metnum];
	}
	if($i==$cnt) {
 	$prevval =0; // didn't find any prev. value
	}
}

// Now retrieve the current value

$datareturn = shell_exec($cmd);
$datareturn = trim($datareturn);
$datareturn = preg_replace("/^${'ID'.$metnum}\(/i", '', $datareturn); // VALUE*UNIT)
$lastval    = preg_replace("/\*[a-z0-9³²]+\)$/i", '', $datareturn); // VALUE
    settype($lastval, 'float');
    settype($prevval, 'float');
    $lastval += $prevval;
    
    if (${'PASSO'.$metnum} >0 && $lastval > ${'PASSO'.$metnum}) { // counter pass over
        $lastval -= ${'PASSO'.$metnum};
    }
$lastval=round($lastval, ${'PRECI'.$metnum});
$str=utf8_decode("${'ID'.$metnum}($lastval*${'UNIT'.$metnum})\n");
echo "$str";

$output     = shell_exec("$pathtomn/scripts/comapps/poolmeters.py > /dev/null 2>&1 &"); // Restart live fetching
?>
I'll repack it later on, so far it's the day and night.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest