Controlling and timing of a ventilation system

Reason for controlling the ventilation system

During the winter season 2016 / 17 I had the idea to control my ventilation system that is installed in the cellar with Siri. My electrician told me, that I could realise it with a 4 port relays. That was the needed key information! I tried to buy a relays for my Raspberry Pi, but it was not so easy! Most relays needs a 5 V power supply. After a couple of days I found the PIFACE RELAY+. I has 4 ports and is made for the Raspberry Pi. You can switch it easily with commands to open and close the ports.

Installation

In the first step you have to put it on the Raspberry Pi and to control the jumpers. The last delivered boards had a different jumper setting. So I had to investigate how to set it correctly.

Then you have to install wiringpi. With that you can easily switch the relais. I use a version of wiringpi, that is not up to date, but works fine. Please take a look at “Tricks and Tips” –> “Installation of a Raspberry Pi and PiFace Relais+”.

sudo mkdir wiringPi
cd wiringPi
wget http://archive.raspberrypi.org/debian/pool/main/w/wiringpi/wiringpi_2.31_armhf.deb
sudo dpkg -i wiringpi_2.31_armhf.deb
dpkg: warning: downgrading wiringpi from 2.46 to 2.31
(Reading database ... 133215 files and directories currently installed.)
Preparing to unpack wiringpi_2.31_armhf.deb ...
Unpacking wiringpi (2.31) over (2.46) ...
Setting up wiringpi (2.31) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Processing triggers for man-db (2.7.6.1-2) ...

gpio -v
gpio version: 2.31
Copyright (c) 2012-2015 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Now you can easily switch the relais with simple commands! Try this:

gpio -p write 200 1
gpio -p write 200 0

The Scripting

The challenge of controlling a ventilation system is, that you can not find appropriate indicators to do that! That’s why I decided to do it with a simple time control, that sets the ventilation level 1 – 4.

To do that, I created the following listener script. That has the advantage, that you can integrate other scripts and devices to switch the levels, without any interference. Later on, I more to that point.

The listener script:

#!/bin/bash

HOMEPATH=/root/steuerung

##########################################
# Entferne beim Reboot bestehende piepe! #
# lege neue Pipelines an                 #
# Setze Berechtigungen der Pipe          #
# Warte in einer while Schleife, bis     #
# Werte kommen und schalte dann das      #
# Relais!                                #
##########################################

# Listener Anfang #
rm -rf /root/steuerung/stufen_pipe
mknod /root/steuerung/stufen_pipe p
chmod 666 /root/steuerung/stufen_pipe
while true
do
# Listener Ende! #
#################################
# Wartet auf Daten aus der Pipe #
#################################
STUFE=($(cat /root/steuerung/stufen_pipe ))

case "$STUFE" in
1) gpio -p write 200 0
gpio -p write 201 0
gpio -p write 202 0
gpio -p write 203 0
sleep 1
gpio -p write 200 1;;
2) gpio -p write 200 0
gpio -p write 201 0
gpio -p write 202 0
gpio -p write 203 0
sleep 1
gpio -p write 201 1;;
3) gpio -p write 200 0
gpio -p write 201 0
gpio -p write 202 0
gpio -p write 203 0
sleep 1
gpio -p write 202 1;;
4) gpio -p write 200 0
gpio -p write 201 0
gpio -p write 202 0
gpio -p write 203 0
sleep 1
gpio -p write 203 1;;
0) echo "AUS!!!"
gpio -p write 200 0
gpio -p write 201 0
gpio -p write 202 0
gpio -p write 203 0 ;;
esac
done 

The script is stable and is executed for a long time. It waits for commands out of a time scheduler that controls every 60 min the ventilation level or the Home-App for switching the ventilation level.

Background of the pipe: If you want to realise that a process can be controlled by other processes, you can use a pipe and a “while – do” for that. I thought around 2 days about that before I found that solution! It is very stable and you can connect many other scripts to control the ventilation levels. It is possible to connect other Services like “Alexa” oder “Google Home” or KNX Systems to it! The only restriction is, that they have to write a number in the pipe!

This is the script for the time scheduler:

 #!/bin/bash
HOMEPATH=/root/steuerung

##########################
# AUS und Notaus-Prüfung!#
##########################
date=1
actdate=2

if [ -f notaus ]
   then
   date=($(cat notaus | awk '{print $2}' | awk -F "." '{print $1}'))
   actdate=($(date | awk '{print $2}' | awk -F "." '{print $1}'))
fi

if [ -f AUS ]
   then
   echo "AUS gesetzt!" >> /root/steuerung/logs.log

   if [ -f notaus ]
      then
      rm notaus
      echo "lösche notausdatei" >> /root/steuerung/logs.log
   fi

   else

   if [ $date -eq $actdate ]
      then
      echo "Notaus Datei gefunden!" >> /root/steuerung/logs.log
      echo "Tue nix! Notaus gedrückt" >> /root/steuerung/logs.log
      exit

      else
      echo "Datumssteuerung wird verwendet!" >> /root/steuerung/logs.log
      echo "Setze Steuerung korrekt!" >> /root/steuerung/logs.log

##################################
#   prüfen ob listener läuft     #
##################################

ps ax | grep 'listener' | grep -v 'grep'  ||  /root/steuerung/listener.sh &

##################################
# Setzen des Datums und der Zeit #
##################################

datum=($(date +%d.%m))
# echo $datum

zeit=($(date +%H:%M))
#echo $zeit

stunde=($(date +%H))

##################################
# Luefterstufe waehlen           #
##################################
# cat $HOMEPATH/zeittabelle.txt | grep -i $stunde | awk '{print $2}'
cat /root/steuerung/zeittabelle.txt | grep -i $stunde | awk '{print $2}' > /nas/data/luefterstufe.txt
cat /root/steuerung/zeittabelle.txt | grep -i $stunde | awk '{print $2}' > /root/steuerung/stufen_pipe
date >> /root/steuerung/logs.log
   fi
fi

It is important that the listener is executed. If it is not running, you need to restart it. All you need is a 60 min crontab entry. This script does it for you.

ps ax | grep 'listener' | grep -v 'grep' || /root/steuerung/listener.sh &

The scheduler table looks like this:

00:00 0
01:00 0
02:00 0
03:00 0
04:00 0
05:00 0
06:00 0
07:00 1
08:00 3
09:00 2
10:00 1
11:00 1
12:00 4
13:00 4
14:00 1
15:00 1
16:00 1
17:00 1
18:00 4
19:00 4
20:00 2
21:00 1
22:00 0
23:00 0

With the following script you switch the levels of the ventilation system. This is the script for the 1st Level: stufe_1.sh.

#!/bin/bash
HOMEPATH=/root/steuerung
ps ax | grep 'listener' | grep -v 'grep'  ||  /root/steuerung/listener.sh &
echo 1 > /root/steuerung/stufen_pipe
echo 1 > /nas/data/luefterstufe.txt

The level 2 – 4 are analog to edit. The level 0 (off) looks like that:  stufe_0.sh

#!/bin/bash
HOMEPATH=/root/steuerung
ps ax | grep 'listener' | grep -v 'grep'  ||  /root/steuerung/listener.sh &
echo 0 > /root/steuerung/stufen_pipe
echo aus > /nas/data/luefterstufe.txt

And now the most interesting point! The control of the ventilation system by the Home-App.

Homekit Integration

To integrate a Raspberry Pi in HomeKit you need a NodeJS based integration layer, called HomeBridge. You need a control center as well. In the first step I used my iPad for that, but some weeks later, I ordered a Apple TV 4k doing that job!

On the Raspberry Pi the HomeBridge is needed.

There are two ways to install it:

  1. Used this Installation guide,
  2. or use the short version in the “Tipps and tricks” Menu.

You need a plugin to realise the control. I found the “cmdswitch2” plugin that can be used for it. You can download it here. It can execute Unix / Linux scripts directly. The installation is done bye this command:

npm install -g homebridge-cmdswitch2

In the read.me in the repository you can find the configuration of the plugin. My modifications you can find here:

{
    "bridge": {
        "name": "Lüftung",
        "username": "CC:22:3D:E3:CE:33",
        "port": 51826,
        "pin": "031-45-154"
    },
"accessories": [
],

"platforms": [{
"platform": "cmdSwitch2",
"name": "CMD Switch",
"switches": [{
"name" : "Stufe 1",
"on_cmd": ". /root/steuerung/stufe_1.sh &",
"off_cmd": ". /root/steuerung/stufe_0.sh &",
"state_cmd": "cat /nas/data/luefterstufe.txt | grep -i 1",
"polling": true,
"interval": 2
}, {
"name" : "Stufe 2",
"on_cmd": ". /root/steuerung/stufe_2.sh &",
"off_cmd": ". /root/steuerung/stufe_0.sh &",
"state_cmd": "cat /nas/data/luefterstufe.txt | grep -i 2",
"polling": true,
"interval": 2
}, {
"name" : "Stufe 3",
"on_cmd": ". /root/steuerung/stufe_3.sh &",
"off_cmd": ". /root/steuerung/stufe_0.sh &",
"state_cmd": "cat /nas/data/luefterstufe.txt | grep -i 3",
"polling": true,
"interval": 2
}, {
"name" : "Stufe 4",
"on_cmd": ". /root/steuerung/stufe_4.sh &",
"off_cmd": ". /root/steuerung/stufe_0.sh &",
"state_cmd": "cat /nas/data/luefterstufe.txt | grep -i 4",
"polling": true,
"interval": 2
}, {
"name" : "Stufe 0",
"on_cmd": ". /root/steuerung/stufe_0.sh &",
"off_cmd": ". /root/steuerung/stufe_0.sh &",
"state_cmd": "cat /nas/data/luefterstufe.txt | grep -i aus",
"polling": true,
"interval": 2
}, {
"name" : "Automatik",
"on_cmd": "/root/steuerung/automatik.sh &",
"off_cmd": ". /root/steuerung/stufe_0.sh &",
"state_cmd": "cat /nas/data/luefterstufe.txt | grep -i automatik",
"polling": true,
"interval": 2
}]
}]
}

With that configuration it is possible to switch the ventilation levels with the Home-App.

That is it! The relais is switchable via HomeKit and a time scheduler. The current level is shown in the Home-App.

You can find many other plugins for showing the temperature or humidity! Take a look at:  https://alexbloggt.com/homebridge-einrichten-erweitern/

Some Fotos:

Home-App and the ventilation levels:

The old switch for switching the levels manually:

The installed Raspberry Pi with the PiFace+ Board and the switch board for the ventilation system:

Leave a Reply

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