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 »

File heizung-signal.py

Code: Select all

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

# crontab
# m h  dom mon dow   command
# 0 6 * * 1-5 /usr/bin/python /home/pi/Documents/Heizung/heizung-tag.py
# 0 8 * * 6-7 /usr/bin/python /home/pi/Documents/Heizung/heizung-tag.py
# 0 22 * * 0-4 /usr/bin/python /home/pi/Documents/Heizung/heizung-nacht.py
# 0 23 * * 5-6 /usr/bin/python /home/pi/Documents/Heizung/heizung-nacht.py

# 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/pi/Documents/Heizung/heizung_pid", "w")
pidfile.write(str(mypid))
pidfile.close()
logfile = open("/home/pi/Documents/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 NC: Tagbetrieb")
		logfile.write("%s: %s\n" % (now, "SIGUSR1: COM to NC: Tagbetrieb"))
		logfile.flush()
		GPIO.output(relay_pin, GPIO.LOW);
	elif signalNumber == signal.SIGUSR2:
		# Close Relay: open circuit
		print "%s: %s" % (now, "SIGUSR2: COM to NO: 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 NC: Tagbetrieb")
logfile.write("%s: %s\n" % (now, "INIT: COM to NC: 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/pi/Documents/Heizung/heizung_pid")

# Close Relay
GPIO.cleanup()
file heizung-tag

Code: Select all

#!/bin/bash
kill -s SIGUSR1 `cat /home/pi/Documents/Heizung/heizung_pid`
file heizung-nacht

Code: Select all

#!/bin/bash
kill -s SIGUSR2 `cat /home/pi/Documents/Heizung/heizung_pid`
Post Reply