Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 86 lines (65 sloc) 2.1 KB
#!/usr/bin/python
# Reading an analogue sensor with
# a single GPIO pin
# Author : Matt Hawkins
# Distribution : Raspbian
# Python : 2.7
# GPIO : RPi.GPIO v3.1.0a
import RPi.GPIO as GPIO, time,commands,math
# Tell the GPIO library to use
# Broadcom GPIO references
GPIO.setmode(GPIO.BCM)
# Define function to measure charge time
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.5)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
startTime = time.time()
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
time.sleep(0.0005)
if time.time() - startTime > 10 :
measurement = 999999
break
return measurement
# Main program loop
#while True:
# print RCtime(10) # Measure timing using GPIO4
values = []
for x in range(1, 11):
value = RCtime(10)
# value = 3
values.append( 14.0 - math.log(value) )
# print values
# print "We're on time %d - value: %d" % (x,value)
#print "%d" % ( values[5] )
#sorting and taking 5th value effectively calculates median :)
#
temperature = commands.getstatusoutput('/root/temperature/measure')
values.sort()
# this measures soil sensors
soil1= str(RCtime(8))
soil2= str(RCtime(7))
# print stuff to stdout
print "%s %s %s %1.3f" % ( time.strftime('%m/%d/%Y'), time.strftime('%H:%M'), temperature[1], values[5] )
import MySQLdb
conn = MySQLdb.connect(host= "10.166.0.1",
user="pi",
passwd="aLeks1pi",
db="watering")
x = conn.cursor()
tempValue = temperature[1].split(' ')[0]
humValue = temperature[1].split(' ')[1]
timeValue = time.strftime('%Y-%m-%d %H:%M:%S')
x.execute("""INSERT INTO weather (time,temperature,humidity,light,soil1, soil2) VALUES (%s,%s,%s,%s,%s,%s)""",(timeValue,tempValue,humValue,values[5],soil1,soil2))
conn.commit()
conn.close()
# print output to the file
f = open('/root/weather/last-status','w')
f.write("Temp: %s C Humidity: %s %% Light: %1.3f s1: %s s2: %s " % ( tempValue, humValue, values[5], soil1, soil2 ) )
f.close()
You can’t perform that action at this time.