Пример #1
0
 def sample(self):
     util.set_gpio(self.soil, 'value', '1')
     readings = []
     for i in xrange(10):
         time.sleep(0.1)
         value = util.read_sys(self.ain)
         if value:
             readings.append(value)
     util.set_gpio(self.soil, 'value', '0')
     if len(readings) > 0:
         reading = max(readings)
         self.history.append(reading)
         return reading, max(self.history)
     else:
         return None, max(self.history)
Пример #2
0
def main_loop(args):
    log = util.Logfile(args.output)
    gft = fusiontables.GoogleAPI(args)

    plant_a = Plant(util.SOLENOID_A, util.SOIL_A, util.AIN_A, args.hsize)
    plant_b = Plant(util.SOLENOID_B, util.SOIL_B, util.AIN_B, args.hsize)

    while True:
        # Read humidity, temperature, and luminosity.
        hum, temp = util.read_hum_temp()
        lux = util.read_sys('/sys/bus/iio/devices/iio:device0/in_intensity_both_raw')

        # Read soil moisture.
        a_raw, a_max = plant_a.sample()
        time.sleep(0.5)
        b_raw, b_max = plant_b.sample()

        now = time.localtime()

        log.check_rotate(now)
        log.write(now, '%.2f %.2f %d' % (hum, temp, lux))
        log.write(now, 'A: %d (max %d), B: %d (max %d)\n' % (a_raw, a_max,
                                                             b_raw, b_max))

        try:
            gft.push_update(now, hum, temp, lux)
        except Exception as e:
            print str(e)
            log.write(now, 'GFTERROR: %s' % str(e))

        if a_max < args.water_threshold:
            plant_a.water(args.duration, log)
        if b_max < args.water_threshold:
            plant_b.water(args.duration, log)

        # Open solenoid(s), if necessary.  Add hysteresis? Check moisture again?
        time.sleep(args.interval)