#Timing setup
from datetime import datetime
from apscheduler.scheduler import Scheduler
import time
import datetime
import sys
now =datetime.datetime.now()
import logging #if you start getting logging errors, uncomment these two lines
logging.basicConfig()
#GPIO setup
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()
pin = 26 #pin for the adc
GPIO.setup(pin, GPIO.OUT)
led1 = 11 #pin for the short indicator led
GPIO.setup(led1, GPIO.OUT)
led2 = 13 #pin for other long indicator led
GPIO.setup(led2, GPIO.OUT)
#the adc’s SPI setup
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
import MySQLdb
con = MySQLdb.connect(‘localhost’,‘piplanter_user’,‘piplanter_pass’,‘piplanter’);
cursor = con.cursor()
#fuction that can read the adc
def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc’s (0 thru 7)
if adcnum > 7 or adcnum < 0:
return –1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
def rapidSample():
sampleTime = time.ctime()
sampleTemp1 = (((readadc(0)*3.3)/1024)/(10.0/1000)) #this translates the analog voltage to temperature in def F
sampleLght1 = readadc(1)
samplePot1 = readadc(2)
GPIO.output(led1, True) #turns the led on
time.sleep(.1) #sleeps a little bit so you can see the LED on
print “Job 1”, sampleTime,“LDR:”,sampleLght1 ,“Pot:”,samplePot1,“Temp:”,sampleTemp1 #prints the debug info
cursor.execute(“INSERT INTO piplanter_table_02(Time,Temp_F,LDR_V) VALUES(%s,’%s’,’%s’)”,(sampleTime,sampleTemp1,sampleLght1))
con.commit() #this is important for live updating
time.sleep(.1)
GPIO.output(led1, False) #turns the led off
def slowSample():
sampleTime = time.ctime()
sampleTemp1 = (((readadc(0)*3.3)/1024)/(10.0/1000)) #this translates the analog voltage to temperature in def F
sampleLght1 = readadc(1)
samplePot1 = readadc(2)
GPIO.output(led2, True) #turns the led on
time.sleep(5)
print “Job 2”, sampleTime,“LDR:”,sampleLght1 ,“Pot:”,samplePot1,“Temp:”,sampleTemp1 #prints the debug info
cursor.execute(“INSERT INTO piplanter_table_03(Time,Temp_F,LDR_V) VALUES(%s,’%s’,’%s’)”,(sampleTime,sampleTemp1,sampleLght1))
con.commit() #this is important for live updating
time.sleep(5)
GPIO.output(led2, False) #turns the led on
if __name__ == ‘__main__’:
#the following 3 lines start up the interval job and keep it going
scheduler = Scheduler(standalone=True)
scheduler.add_interval_job(rapidSample, minutes=1)
scheduler.add_interval_job(slowSample, hours=1)
scheduler.start()