1.03 Reading the DS18B20, prepare some libraries

I write scripts in bash and php7. I work in the


directory because in future we will write a nice WebApp. Well… begin create a path /include/ where we write some library and configuration file. And a path /script/ where we write scripts launched from the CRON.

WinSCP Logo
WinSCP Logo

I suggest to use WinSCP [click here]… a beautiful software that consent to you to write and modify in the raspberry like a network-path… but through ssh!

tree path in the raspberry
tree path in the raspberry

The init.php file

I wrote a file called init.php in the /include/ path… And this file include all other library files… In this way when I want to write a script I just include this.
Here is the file /include/init.php

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");
header("Access-Control-Allow-Origin: *"); 
error_reporting(E_ALL & ~E_NOTICE);

//include all files begin with "lib_"
$d = dir(__DIR__);
while (false !== ($e = $d->read())) 
   if($e=="." || $e=="..") continue;
   if( substr($e,0,4)=="lib_" ) include(__DIR__."/".$e);

define('PIN_SOLENOIDVALVE_F0',17);   //floor zero
define('PIN_SOLENOIDVALVE_F1',23);   //floor one

define('TEMP_Z', "28-00000677ca05"); //external sensor
define('TEMP_F0',"28-00044cfb6bff"); //floor zero
define('TEMP_F1',"28-00044eea7aff"); //floor one
define('TOLERANCE',1); //Celsius

//for write or read threshold temperature in floor $nfloor
function threshold($nfloor,$t=999)
   file_put_contents($file,"14"); //default for first time

   if( $t<999 )  file_put_contents($file,$t);

   return $x;


The first part of the code is for avoid cache. In CLI script, is useless to avoid cache, i know… but the same init.php may be used for web script or web page…
In the second part of the script, I include ALL FILES that begin with “lib_” and after i define some constants… Physical address of our sensors, and the pin number corresponding to relais. We will view detailing after.

I wrote a function for read or write the temperature that i want to maintain in every floor (threshold). I think that the simplest solution is write this in two files. For the first time the threshold is 14°Celsius. When I want to modify the temperature in my house I write in these files…. obviously not manualy but with a beatiful webApp… that we see in future.

My lib_gpio file

I usualy programming with layers. I wrote a libray lib_gpio.php that wrap WiringPi library (seen here) and the DS18B20 (seen here).
Note that I will use ALWAYS the BCM_GPIO notation for the gpio pins.
Here the /include/lib_gpio.php


//$pin is always in standard BCM_GPIO numeration
function gpio_set($pin,$val)
   exec("sudo gpio -g write $pin $val");

function gpio_read($pin)
   $val = exec("sudo gpio -g read $pin");
   return intval(trim($val));

//caching for tot seconds
function gpio_read_temp($code)
   if( !file_exists($cachefile) || filemtime($cachefile)+$cachetime<time()    )
      $cmd="sudo cat /sys/bus/w1/devices/{$code}/w1_slave";
      $buf = exec($cmd);

      if( $yes_no<=0 ) return 99;



      return round(file_get_contents($cachefile),1);

   return round($temp,1);



As you saw, I only wrap the WiringPI library and add a cache system for the temperature. In reality my library is more complicated like that… but I want to explain other things on other posts.
In the next post we attach relais board and finaly see the thermostat script.

Please follow and like us:

One thought on “1.03 Reading the DS18B20, prepare some libraries

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub