1.04 Relais board and Thermostat script

In the previous article we have prepared the library layer for build the script thermostat that run every 2 minutes (with cron). Before do that, we have to attach 3 relais to our Raspberry.

I bought these

Relais board and pins

  • Pin 17 –> Floor Zero –> Relay –> when excited permits the hot water to flow in heaters in floor zero. When not excited block the hot water.
  • Pin 23 –> Floor One –> Relay –> when excited permits the hot water to flow in heaters in floor one. When not excited block the hot water.
  • Pin 22 –> Relay –> Contact to the stove. When excited tells the stove to light the fire. When not excited tells to stove to extinguish the fire

Note that this relais used a logic ACTIVE_LOW. It’s mean that when I write 0 (zero) (0volt) i will excite the relay. If i write 1 (one) (3.3Volt) i de-energized the relay.

Connect the relais board in this way
Vcc–>5Volt
Gnd–>Gnd
Pin22 –> First relay pin
Pin23 –> Second relay pin
Pin17 –> Third relay pin

In this

dav

I recommend to mark on a paper like in this photo, every PIN and his function. As you can see I use also PIN14 and 20 and 21 but for other projects… i will tell you in future.

Now, under /etc/rc.local we have to setup our PINS for say to the system what pin are for output  and what pins are for input.
Open /etc/rc.local and add these lines… Note that we have to add this lines BEFORE the exit(0)

gpio -g write 22 1 #remember is active_low logic
gpio -g mode 22 out #relay stove

gpio -g write 17 1
gpio -g mode 17 out #solenoid-valve floor zero

gpio -g write 23 1
gpio -g mode 23 out #solenoid-valve floor one

You may can think that I made a mistake because I write 3.3volt before I set output mode… but this is correct. If I set output mode first, and after this I write 1 (low state) I have a pulsing because for default the state is 0 (high) and a nanosecond after, I write 1 (low state). Now we can reboot RaspberryPI.

Thermostat script

Note that this script is an executable… and its need the executable bit activated. Also noted that if you write this code in Windows (notepad++ is the best editor) you have to set EDIT->EOL Convertion->UNIX FORMAT. This option set only one char \n for EOL (end of line) and not like windows that is two char \r\n.

Now make the executable bit activate

sudo chmod +x /var/www/html/script/thermostat

Here is the script /var/www/html//script/thermostat
This script need the libraries I explain in the previous post.

#!/usr/bin/env php
<?php include(__DIR__."/../include/init.php");

$temp["t0"]=gpio_read_temp(TEMP_F0);
$temp["t0_threshold"]=threshold(0);

$temp["t1"]=gpio_read_temp(TEMP_F1);
$temp["t1_threshold"]=threshold(1);

$temp["tZ"]=gpio_read_temp(TEMP_Z);

// natively is active_low. I need to trasform in active_high
$temp["f0"]=(gpio_read(PIN_SOLENOIDVALVE_F0)==0 ? 1 : 0);
$temp["f1"]=(gpio_read(PIN_SOLENOIDVALVE_F1)==0 ? 1 : 0);
if( gpio_read(PIN_RELAY_STOVE)==1 ) $temp["f0"]=$temp["f1"]=0;		//if stove is off, both floors are off

//*****Switch-on or Switch-off with a hysteresis curve

$contact=array();
for($i=0;$i<=1;$i++)
{
   $contact_old=$temp["p$i"];
   $contact[$i]=$contact_old;

   if( $contact_old==0 && $temp["t$i"]<=$temp["t{$i}_threshold"])
      $contact[$i]=1;
   else if( $contact_old==1 && $temp["t$i"]>=$temp["t{$i}_threshold"]+TOLERANCE)
      $contact[$i]=0;

   echo "\n\n**** Floor $i\n";
   echo "Actual: {$contact_old}\n";
   echo "Temp: ".$temp["t$i"]."\n";
   echo "Threshold: ".$temp["t{$i}_threshold"]."\n";
   echo "Threshold+TOLERANCE: ". ($temp["t{$i}_threshold"]+TOLERANCE)."\n";
   echo "Ask to light on?: ".$contact[$i]."\n";
}

$contact_STOVE=0;
//if at least one floor ask for heating, i need to light on the fire
if( $contact[0]==1 || $contact[1]==1 ) $contact_STOVE=1;


//this part is for safety. Even if I tell to the stove to extinguish the fire, the water is still very hot, and
//if I close both solenoid-valve, the water does not circulate anymore and the stove goes into overheating.
//So, if the stove go to extinguish the fire, i need to circulate all water in all floors.

if( $contact_STOVE==0 ) 
{
   $contact[0]=1;
   $contact[1]=1;
   echo "\n*** Safety. Stove go off, but is better circulate all water in all floors\n\n";
}

//remember active_low... i will re-revert the logic

gpio_set(PIN_RELAY_STOVE,($contact_STOVE==0 ? 1 : 0));
gpio_set(PIN_SOLENOIDVALVE_F0,($contact[0]==0 ? 1 : 0));
gpio_set(PIN_SOLENOIDVALVE_F1,($contact[1]==0 ? 1 : 0));

echo "\n****** Contact stove --> {$contact_STOVE}\n\n";

?>

The TOLERANCE is usefull for permit the stove to not switch on and off continuosly when its arrived to the goal temperature. The hysteresis curve works in this way. We set the goal temperature to 20°C and hypothesize we are in the ambient at 21°C. The stove is off.
The cold arrive and the temperature drops to 20.0°C. Nothing happens. After goes down to 19.9°C and the stove goes on. Ok.
The stove heats up to 20.9 and nothing happens…. the stove remain ON. When we overcome 21.0 the stove go off and the cycle loops.

Now we can test this script from console

Is everything okay? Now we can put this script in the crontab using

crontab -e

and adding this line

*/2 * * * * /var/www/html/script/thermostat

If all works….

If all works correctly, your house is thermo-controlled by your raspberry. You can define goal temperature trought two file txt. In future we can create a WebApp for your smartphone (in your WIFI) where we can control the thermostat and monitoring real house temperature.

 

Please follow and like us:

Leave a Reply

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

Fork me on GitHub