Skip to content

Instantly share code, notes, and snippets.

@adlerweb adlerweb/w1vz.php
Last active Jul 21, 2018

Embed
What would you like to do?
VZ/PHP/DS18B20/Raspi
<?php
if (! function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');
$mapping = array(
'000002386cd3' => 'da68e010-df8f-11e3-8d00-af6cb310cad1',
'0000023882e5' => 'e172e220-df8f-11e3-8e0d-7d816ca63379',
'00000238a7bd' => 'e5df5840-df8f-11e3-942a-6d89eca9798f'
);
$middleware = 'http://192.168.101.109/middleware.php';
$maxforks = 10;
$forks = 0;
while(true) {
foreach($mapping as $s_1w => $s_uuid) {
if($forks >= $maxforks) {
pcntl_wait($status);
$forks--;
}
$pid[$s_1w] = pcntl_fork();
if($pid[$s_1w] == -1) trigger_error('Could not fork', E_USER_WARNING);
if($pid[$s_1w] == 0) {
readSensor($s_1w, $s_uuid);
exit(0);
}
if($pid[$s_1w] > 0) {
$forks++;
}
}
foreach($mapping as $s_1w => $s_uuid) {
pcntl_waitpid($pid[$s_1w], $status);
}
$forks = 0;
sleep(10);
}
function readSensor($s_1w, $s_uuid) {
global $middleware;
if(!file_exists('/sys/bus/w1/devices/28-'.$s_1w.'/w1_slave')) {
echo 'Sensor 28-'.$s_1w.' not found'."\n";
return -2;
}
$s_wert = file_get_contents('/sys/bus/w1/devices/28-'.$s_1w.'/w1_slave');
if(strpos($s_wert, ' YES') === false) {
echo 'Sensor 28-'.$s_1w.' - checksum incorrect'."\n";
return -1;
}
if(preg_match('/t=(\-?\d+)$/', $s_wert, $match)) {
$temp = $match[1]/1000;
echo 'Sensor 28-'.$s_1w.' @ '.$temp.'°C'."\n";
if($temp == 85) {
echo 'Oops… Looks like a read error - ignoring value…'."\n";
}else{
file_get_contents($middleware.'/data/'.$s_uuid.'.json?operation=add&value='.$temp);
}
unset($temp);
}else{
echo 'Sensor 28-'.$s_1w.' - can not interpret data'."\n";
return -3;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.