Solar Heating and Ventilation Control based on Raspberry Pi
Keywords: RaspberryPI , Home Automation, Temperature Sensor, AD converter, Relay, Solar, Heating, Ventilation, Control with Web Interface,Data Logging
Abstract: To create a solar heating control this article describes how to connect temperature sensor via SPI to a RaspberryPI and how to use GPIO-pins to control relays. In the second part there are detailed control algorithms for a combined solar and combustion heating with web interface to visualize the data logging and as control pannel.
Motivation
When my heating control broke down I decided to create my own to
- implement my own control algorithms
- have an easy to use web interface
- be able to repair it myself!
- safe money
As heating setup I have one central hot water storage tank (550 l Water including 180 l tap water), which provides heat for warm water consumption and heating of my house.
There are two possibilities to heat up this tank which have to be controlled:
- Solar heating One solar collector (6.93 m2, with pump and sensors for hot loading temperature and return flow temperature .
- Wood pellet burner The Calimax "Twist 80/20" oven has its own control for lighting the fire and control the circulation pump. To activate it there are contacts which have to be closed without potentitial by my control. To do this a second relay is needed.
So far I have 4 sensors ( Ttank , Tcoll , Tload , Tret ) and 2 actuators ( Asolar , Aoven ) to handle...
So let's set up the RaspberryPI, design the electronis, implement the communication and design the control.
1.0 Set up the RaspberryPi
- Dowload and install the RaspberryPi Imager on your regular computer.
- Start rpi-imager and
- set hostname
- enable ssh
- set user + password
- set local settings
- Write ISO to SSD.
- Plug SSD into RaspberryPi, connect Monitor, Mouse, Keyboard and boot; initial boot is slow, keep patient
- sudo raspi-config
- System Options > Boot / Auto Login > Console
- System Options > Hostname
- System Options > WLAN
- Interface Options > ssh = yes
- Localisation Options > Locale = de_DE.UTF-8 en_US.UTF-8
- reboot
- connect LAN and login to console
sudo apt-get update
sudo apt-get upgrade
sudo shutdown -r now - route ssh to some different port:
sudo nano /etc/ssh/sshd_config- Port 65022
- LoginGraceTime 1m
- MaxAuthTries 6
- PubkeyAuthentication yes
- AuthorizedKeysFile .ssh/authorized_keys
cat your_id_rsa.pub > ~/.ssh/authorized_keys
or, from extern computer do:
ssh-copy-id -p 65022@
Set access rights:
sudo chown -R.ssh/
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
restart service:
sudo service sshd restart
Before you logout, try to login in from different shell first!
ssh -p 65022 -X user@IP
Be aware of your firewall settings. - find IP address by ifconfig and try to login via
ssh -p 12345 user@192.168.xxx.xxx
(If you should work on a Windows PC, you may use PowerShell.) - Install further usefull applications:
sudo apt-get install emacs
sudo apt-get install gnuplot
sudo apt-get install meld
sudo apt-get install git-cola
1.1 Set up the Web Server
- If there should be apache be default, uninstall it:
sudo systemctl stop apache2
sudo apt-get purge apache2 apache2-utils apache2-bin apache2.2-common -y
sudo apt-get autoremove -y - install lighttpd
sudo apt-get install lighttpd -y
sudo systemctl start lighttpd
sudo systemctl enable lighttpd - To get your IP use hostname -I or ifconfig
- install php
sudo apt install php php-cgi -y
sudo lighttpd-enable-mod fastcgi
sudo lighttpd-enable-mod fastcgi-php
sudo systemctl restart lighttpd - Change the port number:
sudo nano /etc/lighttpd/lighttpd.conf
server.port = 12380 - finally restart sudo /etc/init.d/lighttpd restart
Edit default page:
sudo chown pi:www-data /var/www/html/index.php
nano /var/www/html/index.php
<?php echo "<html>"; echo phpinfo(); echo "</html>"; ?>
Hardening of the Raspberry Pi
Install firewall and block most ports:
sudo apt install ufw -y
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp comment "SSH"
sudo ufw enable
sudo ufw status
sudo ufw allow 80,443/tcp comment "HTTP/HTTPS"
Hurdle brute force attacks:
sudo apt install fail2ban -y
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
maxretry = 4
bantime = 1h
sudo systemctl enable fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
Get HVC sources from github
Start by creating a new ssh-key:
ssh-keygen -t ed25519 -C "arne@...de"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub
And copy the key to github / settings / Deploy key
and set write access.
Initially close the repository to local:
cd ~/ && git clone git@github.com:ArneDS/HVC.git
then start git-cola and Open Git Repository HVC
Copy the HVC to the web server:
sudo mkdir /var/www/html/HVC
ln -s /var/www/html/HVC www
sudo cp -r HVC/src/* /var/www/html/HVC/
sudo chown -R arne:www-data /var/www/html/
sudo chmod g+w /var/www/html/HVC/
cd www
sudo chmod g+w *.svg
sudo chmod g+w *.gp
sudo chmod g+w *.dat
sudo chmod -R g+w Configuration/
Setup HVC service
Install required python packages:
sudo apt install python3-portalocker
sudo apt install python3-colorama
Service to get weather forecast from DWD:
python3 HvcWeather.py
sudo nano /etc/crontab
6 4,10,16,22 * * * arne ( cd /var/www/html/HVC && /usr/bin/python3 ./HvcWeather.py > /tmp/cron.log 2>&1 )
sudo service cron restart
Service to record photovoltaik and 1-wire sensors:
python3 HvcRecordValues.py
sudo cp HvcRecord.service /etc/systemd/system/HvcRecord.service
sudo chmod +x /etc/systemd/system/HvcRecord.service
sudo systemctl enable HvcRecord.service
sudo systemctl start HvcRecord.service
sudo systemctl status HvcRecord.service
Service to run the HVC:
python3 HvcMain.py
sudo cp HVC.service /etc/systemd/system/HVC.service
sudo chmod +x /etc/systemd/system/HVC.service
sudo systemctl enable HVC.service
sudo systemctl start HVC.service
sudo systemctl status HVC.service
Backup the Raspberry Pi
Once you have HVC up and running on your freshly installed Raspberry Pi, you might want to store an image of the SD-card as backup.
Shutdown the Raspberry and plug the SD-card into your regular Linux machine,
check for available partitions by fsblk.
To create the backup to your external disk, do:
sudo dd if=/dev/mmcblk0 bs=4M status=progress | gzip > /media/arne/exthdd/raspberryHVC01.img.gz
And in case of emergency to restore HVC to a (new) SD-card, do:
gzip -dc /media/arne/exthdd/raspberryHVC01.img.gz | sudo dd of=/dev/mmcblk0 bs=4M status=progress conv=fsync