Skip to content

Instantly share code, notes, and snippets.

@danaspisak
Created July 2, 2013 04:42
Show Gist options
  • Save danaspisak/5906817 to your computer and use it in GitHub Desktop.
Save danaspisak/5906817 to your computer and use it in GitHub Desktop.
Python script extending the Adafruit DHTxx Sensor tutorial and sends the humidity and temperature data to a Graphite server.
import argparse
import serial
import string
import time
import os, sys
from socket import socket
CARBON_SERVER = '192.168.3.27'
CARBON_PORT = 2003
DEFAULT_SERIAL="/dev/ttyACM0"
DEFAULT_PREFIX="garage.1"
parser = argparse.ArgumentParser()
parser.add_argument('--serial', help='Serial device to read data')
parser.add_argument('--prefix', help='String prefix used for metric key')
args = parser.parse_args()
if args.serial is None:
serial_port = DEFAULT_SERIAL
else:
serial_port = args.serial
if args.prefix is None:
prefix = DEFAULT_PREFIX
else:
prefix = args.prefix
print serial_port
print prefix
ser = serial.Serial(serial_port, 9600)
sock = socket()
try:
sock.connect( (CARBON_SERVER,CARBON_PORT) )
except:
print "Couldn't connect to carbon server %(server)s :: %(port)d." % { 'server':CARBON_SERVER, 'port':CARBON_PORT }
sys.exit(1)
while 1:
msg = []
line = ser.readline()
now = int (time.time() )
print line
if len(line.split()) >= 3:
cols = line.split()
print cols
msg.append("%s.humidity %s %d" % (prefix, cols[1], now))
msg.append("%s.temp %s %d" % (prefix, cols[4], now))
message = '\n'.join(msg) + '\n'
print '-' * 80
print message
print
sock.sendall(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment