Created
August 18, 2018 19:46
-
-
Save bumbu/69830d3e7d26d753445fa0ba0957734f to your computer and use it in GitHub Desktop.
Temperature sensor that controls a relay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import RPi.GPIO as GPIO | |
import time | |
import sys | |
import Adafruit_DHT | |
RELAY_OFF = GPIO.LOW | |
RELAY_ON = GPIO.HIGH | |
relay_pin = 23 | |
humidity_sensor = Adafruit_DHT.AM2302 | |
humidity_pin = 4 | |
current_relay_state = RELAY_OFF | |
temp_min = 23.0 | |
temp_max = 24.0 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(relay_pin,GPIO.OUT) | |
try: | |
while True: | |
humidity, temperature = Adafruit_DHT.read_retry(humidity_sensor, humidity_pin) | |
if humidity is not None and temperature is not None: | |
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)) | |
if temperature < temp_min: | |
current_relay_state = RELAY_ON | |
print('on') | |
elif temperature > temp_max: | |
current_relay_state = RELAY_OFF | |
print('off') | |
GPIO.output (relay_pin,current_relay_state) | |
else: | |
print('Failed to get reading. Try again!') | |
time.sleep(2) | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
print ("Bye") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save changes into spreadsheet https://github.com/adafruit/Adafruit_Python_DHT/blob/master/examples/google_spreadsheet.py