Skip to content

Instantly share code, notes, and snippets.

@bumbu bumbu/sensors.py
Created Aug 18, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Aug 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.