Skip to content

Instantly share code, notes, and snippets.

@starbuck93
Created March 22, 2016 07:03
Show Gist options
  • Save starbuck93/371c43454639c57aaab5 to your computer and use it in GitHub Desktop.
Save starbuck93/371c43454639c57aaab5 to your computer and use it in GitHub Desktop.
raspberry pi two ds18b20 temperature sensors
<?php
$paths = array();
$command = "find /sys/bus/w1/devices -name '28-*'";
exec($command,$paths);
$fullpath1 = $paths[0] . "/w1_slave";
$fullpath2 = $paths[1] . "/w1_slave";
$filename1 = $fullpath1;
$filename2 = $fullpath2;
//----- get first one -----//
$handle1 = fopen($filename1, "r") or die("Unable to open file!");
$myString1 = fread($handle1,filesize($filename1));
fclose($handle1);
$tempC1 = ((float) substr($myString1, strpos($myString1,"t=")+2))/1000;
$tempF1 = ($tempC1*(9.0 / 5.0)) + 32.0;
//----- get second one ----//
$handle2 = fopen($filename2, "r") or die("Unable to open file!");
$myString2 = fread($handle2,filesize($filename2));
fclose($handle2);
$tempC2 = ((float) substr($myString2, strpos($myString2,"t=")+2))/1000;
$tempF2 = ($tempC2*(9.0 / 5.0)) + 32.0;
//----- echo the whole string -----//
$str = '{"temperature":'.number_format($tempF1,2).',"temperature2":'.number_format($tempF2,2).'}';
echo $str;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment