diff --git a/app/client/state.js b/app/client/state.js index 611d538..17bbd67 100644 --- a/app/client/state.js +++ b/app/client/state.js @@ -46,7 +46,15 @@ Template.state.helpers({ var sensor = last_sensor_reading(); var stateObject = controller_state(); if (sensor) { - if (parseInt(sensor.tankFull) === 0 && stateObject.state.in_valve === 'open' && stateObject.state.out_valve === 'closed') return "/images/barrellFillingUp.png"; + var startPumpingLevelReached; + if (sensor.startPumpingAt == 'TANKLEVEL0') startPumpingLevelReached = (parseInt(sensor.tankLevel0) === 0) + else if (sensor.startPumpingAt == 'TANKLEVEL1') startPumpingLevelReached = (parseInt(sensor.tankLevel1) === 0) + else if (sensor.startPumpingAt == 'TANKLEVEL2') startPumpingLevelReached = (parseInt(sensor.tankLevel2) === 0) + else if (sensor.startPumpingAt == 'TANKLEVEL3') startPumpingLevelReached = (parseInt(sensor.tankLevel3) === 0) + else if (sensor.startPumpingAt == 'TANKLEVEL4') startPumpingLevelReached = (parseInt(sensor.tankLevel4) === 0) + else startPumpingLevelReached = true; + + if (parseInt(sensor.tankFull) === 0 && startPumpingLevelReached && stateObject.state.in_valve === 'open' && stateObject.state.out_valve === 'closed') return "/images/barrellFillingUp.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closed')) return "/images/barrellFull.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringFull.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringFull.png"; diff --git a/app/server/api.js b/app/server/api.js index 49016c7..7540ac8 100644 --- a/app/server/api.js +++ b/app/server/api.js @@ -19,6 +19,8 @@ Api.addRoute('sensorData', { tankLevel3: this.bodyParams.tankLevel3, tankLevel4: this.bodyParams.tankLevel4, tankFull: this.bodyParams.tankFull, + startPumpingAt: this.bodyParams.startPumpingAt, + stopPumpingAt: this.bodyParams.stopPumpingAt, owner: this.bodyParams.owner, controllerId: this.bodyParams.controllerId, created_at: new Date() @@ -32,7 +34,16 @@ function reactToSensorData(nextSensorReading) { console.log("reacting to sensor"); var controllerId = nextSensorReading.controllerId; var state = stateOrDefault(controllerId).state; - var shouldStartPumping = (!state.in_valve || state.in_valve === 'closed') && ((parseInt(nextSensorReading.tankFull) === 0) && (state.out_valve === 'closed' || state.out_valve === 'closing')); + + var startPumpingLevelReached; + if (nextSensorReading.startPumpingAt == 'TANKLEVEL0') startPumpingLevelReached = (parseInt(nextSensorReading.tankLevel0) === 0) + else if (nextSensorReading.startPumpingAt == 'TANKLEVEL1') startPumpingLevelReached = (parseInt(nextSensorReading.tankLevel1) === 0) + else if (nextSensorReading.startPumpingAt == 'TANKLEVEL2') startPumpingLevelReached = (parseInt(nextSensorReading.tankLevel2) === 0) + else if (nextSensorReading.startPumpingAt == 'TANKLEVEL3') startPumpingLevelReached = (parseInt(nextSensorReading.tankLevel3) === 0) + else if (nextSensorReading.startPumpingAt == 'TANKLEVEL4') startPumpingLevelReached = (parseInt(nextSensorReading.tankLevel4) === 0) + else startPumpingLevelReached = true; + + var shouldStartPumping = (!state.in_valve || state.in_valve === 'closed') && ((parseInt(nextSensorReading.tankFull) === 0) && startPumpingLevelReached && (state.out_valve === 'closed' || state.out_valve === 'closing')); if (shouldStartPumping) { ControllerState.update({ @@ -45,7 +56,16 @@ function reactToSensorData(nextSensorReading) { } }); } - var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || state.out_valve === 'open' || state.out_valve === 'opening'); + + var stopPumpingLevelReached; + if (nextSensorReading.stopPumpingAt == 'TANKLEVEL0') stopPumpingLevelReached = (parseInt(nextSensorReading.tankLevel0) === 1) + else if (nextSensorReading.stopPumpingAt == 'TANKLEVEL1') stopPumpingLevelReached = (parseInt(nextSensorReading.tankLevel1) === 1) + else if (nextSensorReading.stopPumpingAt == 'TANKLEVEL2') stopPumpingLevelReached = (parseInt(nextSensorReading.tankLevel2) === 1) + else if (nextSensorReading.stopPumpingAt == 'TANKLEVEL3') stopPumpingLevelReached = (parseInt(nextSensorReading.tankLevel3) === 1) + else if (nextSensorReading.stopPumpingAt == 'TANKLEVEL4') stopPumpingLevelReached = (parseInt(nextSensorReading.tankLevel4) === 1) + else stopPumpingLevelReached = false; + + var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || stopPumpingLevelReached || state.out_valve === 'open' || state.out_valve === 'opening'); if (shouldStopPumping) { ControllerState.update({ diff --git a/app/server/startup.js b/app/server/startup.js index b80d947..9471617 100644 --- a/app/server/startup.js +++ b/app/server/startup.js @@ -24,6 +24,8 @@ if (Meteor.isServer) { tankLevel3: this.bodyParams.tankLevel3, tankLevel4: this.bodyParams.tankLevel4, tankFull: this.bodyParams.tankFull, + startPumpingAt: this.bodyParams.startPumpingAt, + stopPumpingAt: this.bodyParams.stopPumpingAt, owner: this.bodyParams.owner, created_at: new Date() }); diff --git a/controller/config/copy__init__.py.example b/controller/config/copy__init__.py.example index 5267ffd..52ec8d4 100644 --- a/controller/config/copy__init__.py.example +++ b/controller/config/copy__init__.py.example @@ -10,6 +10,8 @@ GPIO_PIN_TANKLEVEL4 = 26 # BCM GPIO_PIN_TANKFULL = 20 # BCM GPIO_PIN_OUT_VALVE = 21 # BCM GPIO_PIN_IN_VALVE = 18 # BCM +START_PUMPING_AT = 'TANKFULL' # start pumping when this level = 0 +STOP_PUMPING_AT = 'TANKFULL' # stop pumping when this level = 1 API_BASE_URL = 'http://agrar.zoblak.com/api/v1.0' CONTROLLER_ID = '120' # every controller must have a different one STATE_FILE = '/var/run/controller_state' diff --git a/controller/sensors.py b/controller/sensors.py index 46c5df3..9bfa85e 100644 --- a/controller/sensors.py +++ b/controller/sensors.py @@ -35,6 +35,8 @@ print 'Bacva puna: {}'.format(tankFull) SENSOR_TYPE = Adafruit_DHT.DHT11 controller_id = config.CONTROLLER_ID owner = "Controller: %s" % controller_id +startPumpingAt = config.START_PUMPING_AT +stopPumpingAt = config.STOP_PUMPING_AT # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). @@ -49,7 +51,7 @@ humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, config.GPIO_PIN_DHT # If this happens try again! if temperature is not None and humidity is not None: response = requests.post(config.SENSORDATA_URL, json={"owner": owner, "temperatureValue": temperature, "humidityValue":humidity, "tankLevel0": "1" if tankLevel0 else "0","tankLevel1": "1" if tankLevel1 else "0","tankLevel2": "1" if tankLevel2 else "0","tankLevel3": "1" if tankLevel3 else "0", "tankLevel4": "1" if tankLevel4 else "0", "tankFull": "1" if tankFull else "0", - "controllerId": controller_id + "startPumpingAt": startPumpingAt,"stopPumpingAt": stopPumpingAt,"controllerId": controller_id }) print 'Temp={0:0.1f}*C'.format(temperature) print 'Humidity={0:0.1f}%'.format(humidity)