Skip to content

Instantly share code, notes, and snippets.

@bumbu
Created August 18, 2018 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumbu/69830d3e7d26d753445fa0ba0957734f to your computer and use it in GitHub Desktop.
Save bumbu/69830d3e7d26d753445fa0ba0957734f to your computer and use it in GitHub Desktop.
Temperature sensor that controls a relay
#!/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")
@bumbu
Copy link
Author

bumbu commented Aug 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment