Multiple level sensors added #21
@@ -1,3 +1,3 @@
|
||||
<template name="sensorData">
|
||||
<li>{{owner}} / <strong>{{temperatureValue}}°C</strong> / <strong>{{humidityValue}}%</strong> / Bačva puna: <strong>{{tankFull}}</strong> / {{created_at_formatted}}</li>
|
||||
<li>{{owner}} / <strong>{{temperatureValue}}°C</strong> / <strong>{{humidityValue}}%</strong> / Bačva puna: <strong>{{tankFull}}</strong> (L0:{{tankLevel0}}-L1:{{tankLevel1}}-L2:{{tankLevel2}}-L3:{{tankLevel3}}-L4:{{tankLevel4}}-full:{{tankFull}}) / {{created_at_formatted}}</li>
|
||||
</template>
|
||||
|
||||
@@ -13,6 +13,11 @@ Api.addRoute('sensorData', {
|
||||
SensorData.insert({
|
||||
temperatureValue: parseFloat(this.bodyParams.temperatureValue),
|
||||
humidityValue: parseFloat(this.bodyParams.humidityValue),
|
||||
tankLevel0: this.bodyParams.tankLevel0,
|
||||
tankLevel1: this.bodyParams.tankLevel1,
|
||||
tankLevel2: this.bodyParams.tankLevel2,
|
||||
tankLevel3: this.bodyParams.tankLevel3,
|
||||
tankLevel4: this.bodyParams.tankLevel4,
|
||||
tankFull: this.bodyParams.tankFull,
|
||||
owner: this.bodyParams.owner,
|
||||
controllerId: this.bodyParams.controllerId,
|
||||
@@ -24,12 +29,10 @@ Api.addRoute('sensorData', {
|
||||
|
||||
|
||||
function reactToSensorData(nextSensorReading) {
|
||||
console.log("reacting to sensor");
|
||||
var controllerId = nextSensorReading.controllerId;
|
||||
var state = stateOrDefault(controllerId).state;
|
||||
console.log(nextSensorReading , state);
|
||||
var shouldStartPumping = (!state.in_valve || state.in_valve === 'closed' || state.in_valve === 'closing') && ((parseInt(nextSensorReading.tankFull) === 0) && (state.out_valve === 'closed' || state.out_valve === 'closing'));
|
||||
|
||||
var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || state.out_valve === 'open' || state.out_valve === 'opening');
|
||||
var shouldStartPumping = (!state.in_valve || state.in_valve === 'closed') && ((parseInt(nextSensorReading.tankFull) === 0) && (state.out_valve === 'closed' || state.out_valve === 'closing'));
|
||||
|
||||
if (shouldStartPumping) {
|
||||
ControllerState.update({
|
||||
@@ -37,12 +40,14 @@ function reactToSensorData(nextSensorReading) {
|
||||
}, {
|
||||
'$set': {
|
||||
'state.in_valve': 'opening',
|
||||
'significantEvents.lastInValveOpen': new Date(),
|
||||
'time': new Date(),
|
||||
'set_by': 'server'
|
||||
}
|
||||
});
|
||||
} else if (shouldStopPumping) {
|
||||
}
|
||||
var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || state.out_valve === 'open' || state.out_valve === 'opening');
|
||||
|
||||
if (shouldStopPumping) {
|
||||
ControllerState.update({
|
||||
controller_id: controllerId
|
||||
}, {
|
||||
@@ -91,7 +96,7 @@ function stateOrDefault(id) {
|
||||
},
|
||||
time: new Date(),
|
||||
config: {
|
||||
draining_period_amount: 60,
|
||||
draining_period_amount: 40,
|
||||
draining_period_unit: 'minutes'
|
||||
},
|
||||
set_by: 'server'
|
||||
@@ -102,6 +107,3 @@ function stateOrDefault(id) {
|
||||
});
|
||||
return stateEntry;
|
||||
}
|
||||
|
||||
Meteor.sharedFunctions = Meteor.sharedFunctions || {};
|
||||
Meteor.sharedFunctions.reactToSensorData = reactToSensorData;
|
||||
|
||||
@@ -4,10 +4,32 @@ if (Meteor.isServer) {
|
||||
SyncedCron.start();
|
||||
});
|
||||
|
||||
|
||||
// Global API configuration
|
||||
var Api = new Restivus({
|
||||
useDefaultAuth: true,
|
||||
prettyJson: true
|
||||
});
|
||||
|
||||
Api.addRoute('sensorData', {
|
||||
authRequired: false
|
||||
}, {
|
||||
post: function() {
|
||||
SensorData.insert({
|
||||
temperatureValue: parseFloat(this.bodyParams.temperatureValue),
|
||||
humidityValue: parseFloat(this.bodyParams.humidityValue),
|
||||
tankLevel0: this.bodyParams.tankLevel0,
|
||||
tankLevel1: this.bodyParams.tankLevel1,
|
||||
tankLevel2: this.bodyParams.tankLevel2,
|
||||
tankLevel3: this.bodyParams.tankLevel3,
|
||||
tankLevel4: this.bodyParams.tankLevel4,
|
||||
tankFull: this.bodyParams.tankFull,
|
||||
owner: this.bodyParams.owner,
|
||||
created_at: new Date()
|
||||
});
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
GPIO_PIN_DHT = 4 # BCM
|
||||
SENSORDATA_URL = 'http://tfm.meteor.com/api/v1.0/sensorData'
|
||||
GPIO_PIN_TANKLEVEL0 = 5 # BCM
|
||||
GPIO_PIN_TANKLEVEL1 = 6 # BCM
|
||||
GPIO_PIN_TANKLEVEL2 = 13 # BCM
|
||||
GPIO_PIN_TANKLEVEL3 = 19 # BCM
|
||||
GPIO_PIN_TANKLEVEL4 = 26 # BCM
|
||||
GPIO_PIN_TANKFULL = 20 # BCM
|
||||
GPIO_PIN_OUT_VALVE = 21 # BCM
|
||||
GPIO_PIN_IN_VALVE = 18 # BCM
|
||||
|
||||
@@ -5,14 +5,30 @@ import Adafruit_DHT
|
||||
import config
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
# Try to read the state of GPIO_PIN_TANKFULL
|
||||
# Try to read the state of GPIO_PIN_TANKLEVELx and GPIO_PIN_TANKFULL
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(config.GPIO_PIN_TANKLEVEL0, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
GPIO.setup(config.GPIO_PIN_TANKLEVEL1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
GPIO.setup(config.GPIO_PIN_TANKLEVEL2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
GPIO.setup(config.GPIO_PIN_TANKLEVEL3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
GPIO.setup(config.GPIO_PIN_TANKLEVEL4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
GPIO.setup(config.GPIO_PIN_TANKFULL, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
|
||||
# tank sensor has inverse logic - 0 when it is full 1 when it is not
|
||||
# so we are inverting its value here
|
||||
tankLevel0 = (GPIO.input(config.GPIO_PIN_TANKLEVEL0) == GPIO.LOW)
|
||||
tankLevel1 = (GPIO.input(config.GPIO_PIN_TANKLEVEL1) == GPIO.LOW)
|
||||
tankLevel2 = (GPIO.input(config.GPIO_PIN_TANKLEVEL2) == GPIO.LOW)
|
||||
tankLevel3 = (GPIO.input(config.GPIO_PIN_TANKLEVEL3) == GPIO.LOW)
|
||||
tankLevel4 = (GPIO.input(config.GPIO_PIN_TANKLEVEL4) == GPIO.LOW)
|
||||
tankFull = (GPIO.input(config.GPIO_PIN_TANKFULL) == GPIO.LOW)
|
||||
|
||||
GPIO.cleanup()
|
||||
print 'Bacva Level0: {}'.format(tankLevel0)
|
||||
print 'Bacva Level1: {}'.format(tankLevel1)
|
||||
print 'Bacva Level2: {}'.format(tankLevel2)
|
||||
print 'Bacva Level3: {}'.format(tankLevel3)
|
||||
print 'Bacva Level4: {}'.format(tankLevel4)
|
||||
print 'Bacva puna: {}'.format(tankFull)
|
||||
|
||||
# Go on to DHT
|
||||
@@ -32,7 +48,7 @@ humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, config.GPIO_PIN_DHT
|
||||
# guarantee the timing of calls to read the sensor).
|
||||
# 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, "tankFull": "1" if tankFull else "0",
|
||||
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
|
||||
})
|
||||
print 'Temp={0:0.1f}*C'.format(temperature)
|
||||
|
||||
Reference in New Issue
Block a user