1Set up the Raspberry Pi
2Electrical Connection of Sensores and Actuators
3Set up the Connection
4Heating Control

no warranty for any of the provided information

HV_loop

Is used by:
HV_restart

The main control loop is used to call specific tasks in specific time intervalls. This is achieved by the function time_sleep_until, which waits until the period is over.

To keep the logfiles small a reset is done each day which creates a new logfile.

To prevent a php timeout the set_time_limit(0) is used.

To start the heating control execute php HV_loop.php& in a shel. To stop execution you have to do ps ux and kill so far...
...meanwhile there is a script to start the heating control automatically after reboot of RaspberryPi -- this is especially usefull if you had a power drop.
stop HV-control: sudo /etc/init.d/S999raspberry stop
start HV-control: sudo /etc/init.d/S999raspberry start

  • init task
    The control parameters are read in, the SPI modul is loaded and GPIO ports are initialized, the filters for the temperatures and the logfile are initialized and the heating control is executed the first time.
  • 1 sec task
    The main control loop, sensors are read in, the heating control is executed and the actuators are set accordingly. Sensor and actuator values are handed to the logfile generator.
  • 60sec task
    Slower task, averaged values are written to logfile.
<?php 
/* published under GPL www.gnu.org/licenses/gpl.html */
$period=1; /* sec */
$die=0;    /* final run time */

controlLoop($period,$die);

function controlLoop($period,$die){
  /* initialization only at start up */
  static $init;
  static $lastDay;
  if($init==FALSE){
    $init=TRUE;
    $next = timeSheduler("init",-1);
    $secCount=-1;
    $minCount=-1;
    $today=date('d');
  } #fi

  
  set_time_limit($die);                  /* maximal execution time, 0 means for ever */

  while(true){                           /* continue looping */
    timeSheduler("1sec",$secCount);      /* each period */
    time_sleep_until($next);             /* wait for next period */
    if($secCount==60 OR $secCount==-1){  /* lower frequency at multiples of period */
      $secCount=0;                       /* reset of counter in slowest task */
      timeSheduler("60sec",$secCount);
      $thisHour=date('H');
      $thisMin=date('i');
      $now=$thisHour.":".$thisMin;
      if((($thisHour!="00" AND $thisMin=="00") OR ($thisHour=="00" AND $thisMin=="05"))
	 OR $thisMin=="15" OR $thisMin=="30" OR $thisMin=="45"){
	timeSheduler("15min",$now);
      }
      if($thisMin=="58" ){
	timeSheduler("1h",$now);
      }
/*
      if(date('H:i')=="00:00"){
	timeSheduler("15min",$now);
	timeSheduler("1h",$now);
      }
*/
    }
    $next=$next+$period;
    $secCount++;

    /* do a reset each day to keep log files small */
    if(1.0*$today!=1.0*date('d')){
      timeSheduler("final",$secCount);
      $today=date('d');
      $next = timeSheduler("init",0);
      $secCount=0;
    }
  } #while
}

function timeSheduler($type,$secCount){
  include_once("HV_writeLog.php");
  include_once("HV_heatingControl.php");
  include_once("HV_readTemperatures.php");
  include_once("HV_setActuators.php");
  include_once("HV_setParameters.php");
  include_once("HV_getSunPrediction.php");
  include_once("HV_thermos200.php");
  static $act;
  static $keys;
  static $Sens;
  static $vent;
  static $today;
  static $now;
  static $lastLevel;
  static $ventAction=-1;
  static $veryFirstInit=TRUE;
  $result=TRUE;
  switch($type){
  case "init":
    $today=date('Y-m-d');
    $now=date('H:i');
    $act=array();
    HV_setParameters(0);
 #   $GLOBALS["ventMode"]="A";
 #   HV_writeConfig();
    exec("gpio load spi");
    $Sens = HV_look4weather($today,$now);
    $Temp = HV_readTemperatures("init");
    HV_setActuators($act,"init");
    $act = HV_heatingControl($Temp,$Sens);
    $data=array_merge($Temp,$act);
    $keys=array_keys($data);
    writeLog($data,"init",$keys,$today,$now);
    $vent = HV_thermos200("init","","");
    $vent['today']=$today;
    $vent['now']=$now;
    writeVent("init",$vent);
    /* $secCount will be even for even seconds */
    $now=time();
    if($now/2.0 == floor($now/2)){
      $next=$now+2;    
    }else{
      $next=$now+1;    
    }
    $result = $next;
    /* try to read temperatures */
    $ventAction=30;
    break;
  case "1sec":
    $Temp = HV_readTemperatures("read");
    $act = HV_heatingControl($Temp,$Sens);
    $data=array_merge($Temp,$act);
    HV_setActuators($act,"set");
    $result = writeLog($data,"collect",$keys,$today,$now);
    /* check ventilation status */
    if($ventAction>90){
      $vent = HV_thermos200("vent",$ventAction,$vent);
      $ventAction--;
    }elseif($ventAction==90){
      $ventAction=0;
    }elseif($ventAction>0){
      /* read ventilation temperatures, one action per second */    
      $ventAction--;
      $vent = HV_thermos200("Temp",$ventAction,$vent);
    }
    if($ventAction==0){
      $ventAction--;
      $vent['today']=date('Y-m-d');
      $vent['now']  =date('H:i');
      $result = writeVent("write",$vent);
    }
    break;
  case "60sec":
    HV_setParameters($secCount);
    $now=date('H:i');
    if($now=="00:02" 
       OR $now=="02:58"
       OR $now=="05:58"
       OR $now=="08:58"
       OR $now=="11:58"
       OR $now=="14:58"
       OR $now=="17:58"
       OR $now=="20:58"
       OR $now=="23:58"){
      $Sens = HV_look4weather($today,$now);
    }
    $result = writeLog("","write",$keys,$today,$now);
    break;
  case "15min":
    /* apply ventilation program in automatic mode */
    $ventAction=99;
    break;
  case "1h":
    /* log temperatures */
    if($ventAction<=0){
      $ventAction=60;
    }
    break;
  case "final":
    writeLog("","final",$keys,$today,$now);
    writeVent("final",$vent);
    break;
  default:
    $result=FALSE;
  }
  return $result;
}
?>
1HV_Admin_Login
2HV_colorMap
3HV_config
4HV_convertASCII
5HV_getSunPrediction
6HV_heatingControl
7HV_loop
8HV_readOperationState
9HV_readTemperatures
10HV_restart
11HV_serviceLog
12HV_setActuators
13HV_setParameters
14HV_showHouse
15HV_showLog
16HV_showRawValues
17HV_showWeatherForecast
18HV_thermos200
19HV_ventProg
20HV_writeLog
21index
22MoistAir
23readFilenames
24readNamedData
25tableInterpol

 

gnuplot

gnuplot is used to visualise the results - via script you can do infinite diagrams in 1 second.
Run 'test' in gnuplot to get an owerview of the available line- and symbolstyles.

Login
Valid HTML 4.01!