Skip to content

Instantly share code, notes, and snippets.

@rothwerx
Created April 11, 2014 04:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rothwerx/10440939 to your computer and use it in GitHub Desktop.
Python: Raspberry Pi: Read photocell values and graph with Graphite. I used this with cron for determining the best place to grow tomatoes.
!/usr/bin/env python
import RPi.GPIO as GPIO
import datetime
import socket
import time
import os
metric_path = "sun.loc1"
DEBUG = 1
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
CARBON_SERVER = '0.0.0.0'
CARBON_PORT = 2003
def RPlight (RPpin):
reading = 0
GPIO.setup(RPpin, GPIO.OUT)
GPIO.output(RPpin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(RPpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RPpin) == GPIO.LOW):
reading += 1
return reading
# ex "metric_path value timestamp\n"
light = RPlight(18)
# Make more light a higher number. Seems more logical to me.
light = 10000 - light
message = '%s %d %d\n' % (metric_path, light, int(time.time()))
print 'sending message:\n%s' % message
sock = socket.socket()
sock.connect((CARBON_SERVER, CARBON_PORT))
sock.sendall(message)
sock.close()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment