Skip to content

Instantly share code, notes, and snippets.

@katzefudder
Last active January 1, 2018 19:04
Show Gist options
  • Save katzefudder/2d5d3f1fbe4b258b4de063552e70e1c2 to your computer and use it in GitHub Desktop.
Save katzefudder/2d5d3f1fbe4b258b4de063552e70e1c2 to your computer and use it in GitHub Desktop.
DHT11 + LDR on my RasPi
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
import dht11
import graphitesend
#define the pin that goes to the circuit
pin_for_temp = 15
pin_for_light = 40
graphite_server='hostname'
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.cleanup()
def readLight (pin_for_light):
count = 0
#Output on the pin for
GPIO.setup(pin_for_light, GPIO.OUT)
GPIO.output(pin_for_light, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_for_light, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_for_light) == GPIO.LOW):
count += 1
return count
def readTemperatureAndHumidity(pin_for_temp):
instance = dht11.DHT11(pin_for_temp)
result = instance.read()
if result.is_valid():
return result
else:
print("Error: %d" % result.error_code)
return False
def sendData(metric, value):
try:
g = graphitesend.init(graphite_server=graphite_server)
print g.send(metric, value)
except Exception as e:
print e
pass
try:
while True:
result = readTemperatureAndHumidity(pin_for_temp)
if (result != False):
sendData('raspberry.temperature', result.temperature)
sendData('raspberry.humidity', result.humidity)
sendData('raspberry.light', readLight(pin_for_light))
time.sleep(10)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment