#!/usr/bin/python # -*- coding: utf-8 -*- """ Python script to control fan ON/OFF in a solar air heating panel. Three temperature sensors are connected measuring the inlet (=outsside ambient temperature), temp_in and inside room temperature (temp_room) and the high temperature near the outlet of the solar array (temp_out). The temperature is logged and saved to a txt file. @author: Lennart Kühlmeier """ import os import glob import time from time import strftime #Control parameters max_temp_in=22 # Fan stops when temp_in>max_temp_in fan_on_delta=10# Fan starts when temp_array>temp_in+fan_on_delta fan_off_delta=5 # Fan stops when temp_array<temp_in+fan_off_delta # Kernel module loading os.system('modprobe w1-gpio') # registers the new sensor connected to GPIO4 os.system('modprobe w1-therm') # adds the temperature support # All recognized onewire addresses are now found under: /sys/bus/w1/devices/ # To list: #cd /sys/bus/w1/devices/ #ls #wiringPI commands to set the GPIO pin for the relay as output and low (=off) os.system("gpio -g mode 22 out") # Set GPIO22 as output base_dir = '/sys/bus/w1/devices/' # 28-00000549843c is the high temperature sensor placed inside the solar array. # 28-000005893e8a is the sensor located outside near the intake on the array. # 28-0000054e585e is the sensor placed inside measuring room temperature. # Functions to read temperature from sensors################ def read_temp_raw(device): f = open(device+ '/w1_slave', 'r') lines = f.readlines() f.close() return lines def read_temp(device): lines = read_temp_raw(device) while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw(device) equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 return temp_c ########################################################### #Control loop. Very simple. Thins are kept in Kelvin. I know. No need but the #engineering mind can only cope with absolute temperatures # The '28-XXXXXXXXX' are the addresses of the temperature sensor. # Find your own like this: #cd /sys/bus/w1/devices/ #ls temp_room = read_temp(glob.glob(base_dir + '28-0000054e585e')[0])+273 temp_in = read_temp(glob.glob(base_dir + '28-000005893e8a')[0])+273 temp_out = read_temp(glob.glob(base_dir + '28-00000549843c')[0])+273 if temp_room<max_temp_in+273: if temp_out>temp_room+fan_on_delta: os.system("gpio -g write 22 1") if temp_out<temp_out+fan_off_delta: os.system("gpio -g write 22 0") if temp_room>max_temp_in+273: os.system("gpio -g write 22 0")