I started using a raspberry pi 2 a few weeks ago with the intention of creating a system that can take a temperature reading from a thermostat sending it back to the pi and then closing or opening a relay to power an actuator that would heat an area.
So far this has been going well. I've spent some time reading through "programming your raspberry pi" by Simon Monk, and that has helped alot.
I have connected 2 heat sensors to the pi using a bread board which can talk to the pi perfectly and give me temperature readings. I have also connected a pi face to the board to give me the relays and outputs I need to open and close according to the temprature.
The problem is, if I get a temprature reading from the sensor saying 20°, for example, my code says if the temprature is 22 or less it should open a relay, above 22 it should close the relay. Currently it closes the relay no matter what the temprature is, or if I swap the greater than signs so it is opposite it opens them no matter what.
A second problem, I've set the code so if the relays open it controls outputs 3 and 4 but 3 blinks once and 4 remains on until told to close.
Any help is greatly appritiated
This is the code,
Code: Select all
import os import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') temp_sensor_1 = '/sys/bus/w1/devices/28-0000073ee0a2/w1_slave' def temp_raw_1(): f = open(temp_sensor_1, 'r') lines = f.readlines() f.close() return lines def read_temp_1(): lines_1 = temp_raw_1() while lines_1.strip()[-3:] != 'YES': time.sleep(0.2) lines_1 = temp_raw_1() temp_output_1 = lines_1.find('t=') if temp_output_1 != -1: temp_string_1 = lines_1.strip()[temp_output_1+2:] temp_c_1 = float(temp_string_1) / 1000.0 return temp_c_1 temp_sensor_2 = '/sys/bus/w1/devices/28-0000043dfb4e/w1_slave' def temp_raw(): f = open(temp_sensor_2, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = temp_raw() while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = temp_raw() temp_output = lines.find('t=') if temp_output != -1: temp_string = lines.strip()[temp_output+2:] temp_c = float(temp_string) / 1000.0 return temp_c while True: print('outside;') print(read_temp_1()) print ('inside;') print(read_temp()) print ('---') if read_temp_1 <= 22: import pifacedigitalio as p p.init() p.digital_write(3,1) print ('outside Relay 3 Open') print ('-----') if read_temp_1 > 22: import pifacedigitalio as p p.init() p.digital_write (3,0) print ('outside Relay 3 Closed') print('-----') if read_temp <= 22: import pifacedigitalio as p p.init() p.digital_write(4,1) print ('inside Relay 4 Open') print ('----') if read_temp > 22: import pifacedigitalio as p p.init() p.digital_write (4,0) print ('inside Relay 4 Closed') print ('-----') time.sleep(25)