#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
tempCh = 0
humCh = 1
lightCh = 2
watLvlCh = 3

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum):
  global clockpin
  global mosipin
  global misopin
  global cspin

  if ((adcnum > 7) or (adcnum < 0)):
          return -1
  GPIO.output(cspin, True)

  GPIO.output(clockpin, False)  # start clock low
  GPIO.output(cspin, False)     # bring CS low

  commandout = adcnum
  commandout |= 0x18  # start bit + single-ended bit
  commandout <<= 3    # we only need to send 5 bits here
  for i in range(5):
          if (commandout & 0x80):
                  GPIO.output(mosipin, True)
          else:
                  GPIO.output(mosipin, False)
          commandout <<= 1
          GPIO.output(clockpin, True)
          GPIO.output(clockpin, False)

  adcout = 0
  # read in one empty bit, one null bit and 10 ADC bits
  for i in range(12):
          GPIO.output(clockpin, True)
          GPIO.output(clockpin, False)
          adcout <<= 1
          if (GPIO.input(misopin)):
                  adcout |= 0x1

  GPIO.output(cspin, True)

  adcout >>= 1       # first bit is 'null' so drop it
  return adcout          

def ReadTemperature():
  temp = ((readadc(tempCh) * 330)/float(1023))-50
  temp = round(temp,2)
  return temp

def ReadLight():
  light = readadc(lightCh)
  light = - 0.00000043265 * pow(light,5) + 0.00055055 * pow(light,4) - 0.21692 * pow(light,3) + 33.554 * pow(light,2) - 2236.8 * light + 55840
  return light

def ReadHumidity():
  humidity = readadc(humCh)
  return humidity

def ReadWaterLevel():
  watLvl = readadc(watLvlCh)
  return watLvl

# =======================main=================== #

clockpin = 17
misopin = 18
mosipin = 27
cspin = 22
updateRate = 1
 
# set up the SPI interface pins
GPIO.setup(mosipin, GPIO.OUT)
GPIO.setup(misopin, GPIO.IN)
GPIO.setup(clockpin, GPIO.OUT)
GPIO.setup(cspin, GPIO.OUT)

while True:
 
  print "--------------------------------------------"
  print "level:",readadc(0)

  time.sleep(updateRate)
# ============================================== #

128bb04a7c35a45cd7dbf020434f42a91fd92de8
#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
tempCh = 0
humCh = 1
lightCh = 2
watLvlCh = 3

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum):
  global clockpin
  global mosipin
  global misopin
  global cspin

  if ((adcnum > 7) or (adcnum < 0)):
          return -1
  GPIO.output(cspin, True)

  GPIO.output(clockpin, False)  # start clock low
  GPIO.output(cspin, False)     # bring CS low

  commandout = adcnum
  commandout |= 0x18  # start bit + single-ended bit
  commandout <<= 3    # we only need to send 5 bits here
  for i in range(5):
          if (commandout & 0x80):
                  GPIO.output(mosipin, True)
          else:
                  GPIO.output(mosipin, False)
          commandout <<= 1
          GPIO.output(clockpin, True)
          GPIO.output(clockpin, False)

  adcout = 0
  # read in one empty bit, one null bit and 10 ADC bits
  for i in range(12):
          GPIO.output(clockpin, True)
          GPIO.output(clockpin, False)
          adcout <<= 1
          if (GPIO.input(misopin)):
                  adcout |= 0x1

  GPIO.output(cspin, True)

  adcout >>= 1       # first bit is 'null' so drop it
  return adcout          

def ReadTemperature():
  temp = ((readadc(tempCh) * 330)/float(1023))-50
  temp = round(temp,2)
  return temp

def ReadLight():
  light = readadc(lightCh)
  light = - 0.00000043265 * pow(light,5) + 0.00055055 * pow(light,4) - 0.21692 * pow(light,3) + 33.554 * pow(light,2) - 2236.8 * light + 55840
  return light

def ReadHumidity():
  humidity = readadc(humCh)
  return humidity

def ReadWaterLevel():
  watLvl = readadc(watLvlCh)
  return watLvl

# =======================main=================== #

clockpin = 17
misopin = 18
mosipin = 27
cspin = 22
updateRate = 1
 
# set up the SPI interface pins
GPIO.setup(mosipin, GPIO.OUT)
GPIO.setup(misopin, GPIO.IN)
GPIO.setup(clockpin, GPIO.OUT)
GPIO.setup(cspin, GPIO.OUT)

while True:
 
  print "--------------------------------------------"
  print "level:",readadc(0)

  time.sleep(updateRate)
# ============================================== #
