Controlling the heater of my flat with the crowpi2

Got a programming project in mind? Tell everyone about it!
Post Reply
heinerwiegel
Posts: 2
Joined: Mon Jan 18, 2021 10:25 pm

Controlling the heater of my flat with the crowpi2

Post by heinerwiegel »

Controlling the heater with the crowpi2

This program runs indefinitely:
file heizung-signal.py

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-
# heizung-signal.py
# http://elecrow.com/
# 2021-01-05 Heiner Wiegel
# Relay HFD4/5-s 2A? 220V?

# Vaillant VRT-QZA
#
# | blau: Wechselkontakt: CO: an COM
# | gelb: Ruhekontakt: Tagbetrieb: an NC
# | rot:  Arbeitskontakt: Nachtbetrieb: an NO
# |
# |

import RPi.GPIO as GPIO
import dht11
import time
import signal, os
import datetime
import os

# define relay pin BCM Mode
relay_pin = 21
# relay connections:
# COM: common: moves
# NO: normally open: connects to COM when relay turns on
# NC: cormally closed: connects to COM when relay turns off

rgb_pin = 12 #BCM Mode

# set GPIO mode as GPIO.BCM
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
# setup relay pin as OUTPUT
GPIO.setup(relay_pin, GPIO.OUT)

dht11inst = dht11.DHT11(pin=4) #BCM Mode

mypid = os.getpid()
pidfile = open("/home/heiner/Documents/EigeneProjekte/Relay/Heizung/heizung_pid", "w")
pidfile.write(str(mypid))
pidfile.close()
logfile = open("/home/heiner/Documents/EigeneProjekte/Relay/Heizung/heizung.log", "a", buffering=0)
now = datetime.datetime.now().isoformat()
logfile.write("%s: PID=%d\n" % (now, mypid))
logfile.flush()

def receiveSIGUSR(signalNumber, frame):
	now = datetime.datetime.now().isoformat()
	if signalNumber == signal.SIGUSR1:
		# Open Relay: close circuit
		print "%s: %s" % (now, "SIGUSR1: COM to NO: Tagbetrieb")
		logfile.write("%s: %s\n" % (now, "SIGUSR1: COM to NO: Tagbetrieb"))
		logfile.flush()
		GPIO.output(relay_pin, GPIO.LOW);
	elif signalNumber == signal.SIGUSR2:
		# Close Relay: open circuit
		print "%s: %s" % (now, "SIGUSR2: COM to NC: Nachtbetrieb")
		logfile.write("%s: %s\n" % (now, "SIGUSR2: COM to NO: Nachtbetrieb"))
		logfile.flush()
		GPIO.output(relay_pin, GPIO.HIGH)
	return

signal.signal(signal.SIGUSR1, receiveSIGUSR)
signal.signal(signal.SIGUSR2, receiveSIGUSR)

# Open Relay: close circuit
print "%s: %s" % (now, "INIT: COM to NO: Tagbetrieb")
logfile.write("%s: %s\n" % (now, "INIT: COM to NO: Tagbetrieb"))
GPIO.output(relay_pin, GPIO.LOW)
while True:
        result = dht11inst.read()
	now = datetime.datetime.now().isoformat()
        if result.is_valid():
        	logfile.write("%s: Temperature: %-3.1f °C Humidity: %-3.1f%%\n" % (now, result.temperature, result.humidity))
		logfile.flush()
        time.sleep(600)

os.remove("/home/heiner/Documents/EigeneProjekte/Relay/Heizung/heizung_pid")

# Close Relay
GPIO.cleanup()
This two programs switch the heater on and of, run by hand or scheduled by crontab:
file heater-off.py

Code: Select all

#!/bin/bash
kill -s SIGUSR2 `cat /home/heiner/Documents/EigeneProjekte/Relay/Heizung/heizung_pid`
file heater-on.py

Code: Select all

#!/bin/bash
kill -s SIGUSR1 `cat /home/heiner/Documents/EigeneProjekte/Relay/Heizung/heizung_pid`

Post Reply