Python: Raspberry Pi: Read photocell values and graph with Graphite. I used this with cron for determining the best place to grow tomatoes.
This file contains 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/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