- #16 Best Toners ever 2020-08-08
- #15 Eletronics Stuff 2020-08-05
- #14 Create SSH Reverse Tunnel in rapid way, reliable and programmaticaly 2019-08-08
- #13 Automate, silence smartphone at work 2019-07-07
- #12 Control your lights without sonoff or other things 2019-06-06
- #11 Google Home and IFTTT, turn on the stove 2019-05-05
- #10 Google Home Hack, send voice programmaticaly with Python 2019-04-04
- #9 Google Home Hack, hidden REST API 2019-03-03
- #8 1.05 A quick WebApp for your thermostat 2019-02-02
- #7 Root for my Huawei P8 Lite 2017 PRA-LX1 2019-01-01
- #6 1.04 Relais board and Thermostat script 2018-12-12
- #5 1.03 Reading the DS18B20, prepare some libraries 2018-11-11
- #4 1.02 Use temperature sensor DS18B20 2018-10-10
- #2 1.01 Initially setup your RaspberryPI 2018-09-25
- #3 1.00 The Silvano idea – the beginning 2018-09-23
- #1 Welcome 2018-09-21
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
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
http://www.gioexperience.com/wp-content/uploads/2018/10/Senza-titolo-1-1.jpg\" alt=\"\" width=\"570\" height=\"140\" srcset=\"https://www.gioexperience.com/wp-content/uploads/2018/10/Senza-titolo-1-1.jpg 570w, https://www.gioexperience.com/wp-content/uploads/2018/10/Senza-titolo-1-1-300x74.jpg 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" style=\"box-sizing: border-box; max-width: 100%; height: auto; vertical-align: top;\">
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.