コード例 #1
0
def runLightSchedule(api, lightOnTime, lightCyclePeriod):
    while True:
        try:
            print "Running light schedule in background"
            results, code = getLightStatuses()
            results = json.loads(results.response[0])
            results = dict([(float(el[0]), el[1]) for el in results])
            if results[0] == False:
                sc.turnOn(api, light_0, "d")
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute(
                    """UPDATE lights SET status = '{}' WHERE id = {}""".format(
                        'true', 0))
                con.commit()
                dbc.disconnectFromDB(con)
                time.sleep(lightOnTime)
            results, code = getLightStatuses()
            results = json.loads(results.response[0])
            results = dict([(float(el[0]), el[1]) for el in results])
            if results[0] == True:
                sc.turnOff(api, light_0, "d")
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute(
                    """UPDATE lights SET status = '{}' WHERE id = {}""".format(
                        'false', 0))
                con.commit()
                dbc.disconnectFromDB(con)
                time.sleep(lightCyclePeriod)
        except Exception as e:
            print "Failed to run the light schedule"
            print e
コード例 #2
0
def startMixer():
    try:
        results, code = getPumpSensors()
        results = json.loads(results.response[0])
        results = dict([((el[0]), float(el[1])) for el in results])
        if results['mixer_empty'] < 2.5:
            print "Mixer it too low. Failed to start the mixer motor"
            return jsonify(), 503
        sc.turnOn(api, mixer_0, "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute(
            """UPDATE mixers SET status = {} WHERE name = '{}'""".format(
                "true", "mixer_0"))
        con.commit()
        dbc.disconnectFromDB(con)
        time.sleep(5)
        sc.turnOff(api, mixer_0, "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute(
            """UPDATE mixers SET status = {} WHERE name = '{}'""".format(
                "false", "mixer_0"))
        con.commit()
    except Exception as e:
        print "Failed to set mixer status"
        print e
    finally:
        dbc.disconnectFromDB(con)
        return jsonify(), 200
コード例 #3
0
def setPumpStatus():
    try:
        results, code = getPumpSensors()
        results = json.loads(results.response[0])
        results = dict([((el[0]), float(el[1])) for el in results])
        t = "d"
        if request.json['on'] == True:
            if (request.json['pump'] == "clean_water"
                    or request.json['pump'] == "nutrients"
                    or request.json['pump'] == "acid" or request.json['pump']
                    == "base") and results['mixer_full'] > 2.5:
                print "Tried to pump with mixer too full"
                return jsonify(), 503
            elif request.json['pump'] == "drain_water":
                if results['drain_water'] > 0:
                    print "Tried to pump with drain too full"
                    return jsonify(), 503
            elif request.json['pump'] == "mixer":
                if results['mixer_empty'] < 2.5:
                    print "Tried to pump with mixer empty"
                    return jsonify(), 503
            elif request.json['pump'] == "mister":
                if results['clean_water'] == 0:
                    print "Tries to use mister while clean water is low"
                    return jsonify(), 503
            elif request.json['pump'] == "drain_out":
                pass
            elif (request.json['pump'] != "drain_water"
                  or request.json['pump'] != "mixer"
                  or request.json['pump'] != "mister" or request.json['pump']
                  != "drain_out") and results[request.json['pump']] == 0:
                print "Tried to pump with bad low fluid level on {}".format(
                    request.json['pump'])
                return jsonify(), 503
            sc.turnOn(api, pins["{}_pump".format(request.json['pump'])], t)
        else:
            sc.turnOff(api, pins["{}_pump".format(request.json['pump'])], t)
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute(
            """UPDATE pumps SET status = {} WHERE name = '{}'""".format(
                request.json['on'], request.json['pump']))
    except Exception as e:
        print "Failed to set pump status"
        print e
    finally:
        con.commit()
        dbc.disconnectFromDB(con)
        return jsonify(), 200
コード例 #4
0
def setLightStatus():
    try:
        if request.json['on'] is True:
            sc.turnOn(api, light_0, "d")
        else:
            sc.turnOff(api, light_0, "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute("""UPDATE lights SET status = {} WHERE id = {}""".format(
            request.json['on'], request.json['light']))
    except Exception as e:
        print "Failed to set light status"
        print e
        return jsonify, 503
    finally:
        con.commit()
        dbc.disconnectFromDB(con)
        return jsonify(), 200
コード例 #5
0
def poll(api):
    while True:
        try:
            cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                       'postgres', 'homegrowplus')
            cur.execute("""SELECT * FROM pumps""")
            results = cur.fetchall()
            dbc.disconnectFromDB(con)
            results = dict([((el[0]), el[1]) for el in results])
            cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                       'postgres', 'homegrowplus')
            #pH = (-1.129*(float(sc.quantize(sc.readPin(api, pH_sensor_0, "a"), 5))) + 9.774)
            #if pH is not None:
            #    cur.execute("""UPDATE ph_sensors SET value = {} WHERE name = '{}'""".format(pH, "ph_sensor_0"))
            hum, temp = Adafruit_DHT.read(Adafruit_DHT.DHT11, temp_sensor_0)
            if hum is not None and temp is not None:
                cur.execute(
                    """UPDATE temp_sensors SET tempurature = {}, humidity = {} WHERE name = '{}'"""
                    .format(float(temp), float(hum), "temp_sensor_0"))
            val = sc.readPin(api, clean_water_conductivity_sensor, "d")
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "clean_water"))
            if val == 0:
                print "Safety shutdown for clean_water"
                if results['clean_water'] == True:
                    sc.turnOff(api, clean_water_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "clean_water"))
                if results['mister'] == True:
                    sc.turnOff(api, mister_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "mister"))
            val = sc.readPin(api, drain_water_conductivity_sensor, "d")
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "drain_water"))
            if val > 0:
                print "Safety shutdown for drain_water"
                if results['drain_water'] == True:
                    sc.turnOff(api, drain_water_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "drain_water"))
            val = sc.quantize(
                sc.readPin(api, nutrient_conductivity_sensor, "a"), 5)
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "nutrients"))
            if val < 2.5:
                print "Safety shutdown for nutrients"
                if results['nutrients'] == True:
                    sc.turnOff(api, nutrients_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "nutrients"))
            val = sc.quantize(sc.readPin(api, acid_conductivity_sensor, "a"),
                              5)
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "acid"))
            if val < 2.5:
                print "Safety shutdown for acid"
                if results['acid'] == True:
                    sc.turnOff(api, acid_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "acid"))
            val = sc.quantize(sc.readPin(api, base_conductivity_sensor, "a"),
                              5)
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "base"))
            if val < 2.5:
                print "Safety shutdown for base"
                if results['base'] == True:
                    sc.turnOff(api, base_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "base"))
            val = sc.quantize(
                sc.readPin(api, mixer_empty_conductivity_sensor, "a"), 5)
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "mixer_empty"))
            if val < 2.5:
                print "Safety shutdown for mixer"
                if results['mixer'] == True:
                    sc.turnOff(api, mixer_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "mixer"))
            val = sc.quantize(
                sc.readPin(api, mixer_full_conductivity_sensor, "a"), 5)
            if val is not None:
                cur.execute(
                    """UPDATE conductivity_sensors SET value = {} WHERE name = '{}'"""
                    .format(val, "mixer_full"))
            if val > 2.5:
                print "Safety shutdown due to mixer overflow"
                if results['clean_water'] == True:
                    sc.turnOff(api, clean_water_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "clean_water"))
                if results['nutrients'] == True:
                    sc.turnOff(api, nutrients_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "nutrients"))
                if results['acid'] == True:
                    sc.turnOff(api, acid_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "acid"))
                if results['base'] == True:
                    sc.turnOff(api, base_pump, "d")
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', "base"))
        except Exception as e:
            print "Could not poll data from the Arduino"
            print e
        finally:
            con.commit()
            dbc.disconnectFromDB(con)
            time.sleep(2)
コード例 #6
0
def fillMixer():
    while True:
        try:
            print "Filling mixer"
            cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                       'postgres', 'homegrowplus')
            cur.execute("""SELECT * FROM conductivity_sensors""")
            results = cur.fetchall()
            dbc.disconnectFromDB(con)
            results = dict([((el[0]), float(el[1])) for el in results])
            if results['mixer_full'] > 2.5:
                print "Mixer is full"
                startMixer()
                break
            else:
                print "Mixer still not full"
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results['clean_water'] == False and results[
                        'mixer_full'] < 2.5:
                    print "Filling mixer with clean water"
                    sc.turnOn(api, clean_water_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'clean_water'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(3)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['clean_water'] == True:
                    sc.turnOff(api, clean_water_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'clean_water'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results[
                        'nutrients'] == False and results['mixer_full'] < 2.5:
                    print "Filling mixer with nutrients"
                    sc.turnOn(api, nutrients_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'nutrients'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(1)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['nutrients'] == True:
                    sc.turnOff(api, nutrients_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'nutrients'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results[
                        'acid'] == False and results['mixer_full'] < 2.5:
                    print "Filling mixer with acid"
                    sc.turnOn(api, acid_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'acid'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(1)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['acid'] == True:
                    sc.turnOff(api, acid_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'acid'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results[
                        'base'] == False and results['mixer_full'] < 2.5:
                    print "Filling mixer with base"
                    sc.turnOn(api, base_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'base'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(1)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['base'] == True:
                    sc.turnOff(api, base_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'base'))
                    con.commit()
                    dbc.disconnectFromDB(con)
        except Exception as e:
            print "Failed to auto fill mixer"
            print e
コード例 #7
0
def runWaterSchedule(api, waterOnTime, waterCyclePeriod):
    while True:
        try:
            print "Running water schedule in background"
            results, code = getPumpSensors()
            results = json.loads(results.response[0])
            results = dict([((el[0]), float(el[1])) for el in results])
            pump_results, code = getPumpStatuses()
            pump_results = json.loads(pump_results.response[0])
            pump_results = dict([((el[0]), el[1]) for el in pump_results])
            if results['mixer_empty'] < 2.5:
                print "Mixer is empty"
                sc.turnOff(api, mixer_pump, "d")
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute(
                    """UPDATE pumps SET status = {} WHERE name = '{}'""".
                    format('false', 'mixer'))
                con.commit()
                dbc.disconnectFromDB(con)
                fillMixer()
            else:
                print "Mixer not empty"
                if pump_results['mixer'] == False:
                    sc.turnOn(api, mixer_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'mixer'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(waterOnTime)
                pump_results, code = getPumpStatuses()
                pump_results = json.loads(pump_results.response[0])
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['mixer'] == True:
                    sc.turnOff(api, mixer_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'mixer'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(waterCyclePeriod)
                pump_results, code = getPumpStatuses()
                pump_results = json.loads(pump_results.response[0])
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['drain_out'] == False:
                    sc.turnOn(api, drain_out_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'drain_out'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(int(0.5 * waterOnTime))
                pump_results, code = getPumpStatuses()
                pump_results = json.loads(pump_results.response[0])
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['drain_out'] == True:
                    sc.turnOff(api, drain_out_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'drain_out'))
                    con.commit()
                    dbc.disconnectFromDB(con)
        except Exception as e:
            print "Failed to run the water schedule"
            print e
コード例 #8
0
        api.pinMode(nutrients_pump, api.OUTPUT)
        api.digitalWrite(nutrients_pump, api.LOW)
        api.pinMode(acid_pump, api.OUTPUT)
        api.digitalWrite(acid_pump, api.LOW)
        api.pinMode(base_pump, api.OUTPUT)
        api.digitalWrite(base_pump, api.LOW)
        api.pinMode(mixer_pump, api.OUTPUT)
        api.digitalWrite(mixer_pump, api.LOW)
        api.pinMode(mister_pump, api.OUTPUT)
        api.digitalWrite(mister_pump, api.LOW)
        api.pinMode(drain_out_pump, api.OUTPUT)
        api.digitalWrite(drain_out_pump, api.LOW)

        p = Process(target=poll, args=(api, ))
        p.start()
        app.run(host='0.0.0.0', port=5000, threaded=True)
    except Exception as e:
        p.join()
    finally:
        gpio.cleanup()
        for pin in pins:
            sc.turnOff(api, pins[pin], "a")
            sc.turnOff(api, pins[pin], "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        for table in {'pumps', 'lights', 'mixers'}:
            cur.execute("""UPDATE {} SET status = false""".format(table))
        con.commit()
        dbc.disconnectFromDB(con)
    print "Exiting"