Skip to content

Commit

Permalink
Correction altitude calculation #5
Browse files Browse the repository at this point in the history
  • Loading branch information
L Diaz committed Jun 4, 2020
1 parent 5b3199b commit 00dfad1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
11 changes: 9 additions & 2 deletions bme280.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<label for="node-input-extra"><i class="fa fa-check"></i> Extra Data</label>
<input type="checkbox" id="node-input-extra" style="width: 60%;" />
</div>

<div class="form-row">
<label for="node-input-preasure"><i class="fa fa-tag"></i> Topic</label>
<input type="text" id="node-input-preasure">
</div>

</script>

<script type="text/javascript">
Expand All @@ -38,8 +44,9 @@
return v.length==4 && v[0]=="0" && v[1].toUpperCase()=="X" && !isNan(n) &&
n>=0x10 && n<0x78;
}},
topic: {value:'bme280', required:false },
extra:{value:false, required: true}
topic: { value:'bme280', required:false },
extra: { value:false, required: true},
preasure:{ value:1013.25, required: false, validate: RED.validators.number() }
},
outputs:1,
inputs:1,
Expand Down
39 changes: 17 additions & 22 deletions bme280.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,36 @@ module.exports = function (RED) {
node.addr = parseInt(n.address, 16);
node.topic = n.topic || "";
node.extra = n.extra || false;
node.preasure= parseFloat(n.preasure) || 1013.25;
node.initialized = false;
node.isBME = false;

// init the sensor
node.status({ fill: "grey", shape: "ring", text: "Init..." });
node.log("Initializing on bus" + node.bus + " addr:" + node.addr);
node.sensor = new BME280({ i2cBusNo: node.bus, i2cAddress: node.addr });
node.sensor.init().then(function (ID) {
node.initialized = true;
node.type = ID == BME280.CHIP_ID_BME280() ? "BME280" : "BMP280";
node.isBME = (node.type == "BME280");
node.status({ fill: "green", shape: "dot", text: node.type + " ready" });
node.log("Sensor " + node.type + " initialized.");
}).catch(function (err) {
node.status({ fill: "red", shape: "ring", text: "Sensor Init Failed" });
node.error("Sensor Init failed ->" + err);
});
var fnInit= function() {
node.sensor.init().then(function (ID) {
node.initialized = true;
node.type = ID == BME280.CHIP_ID_BME280() ? "BME280" : "BMP280";
node.isBME = (node.type == "BME280");
node.status({ fill: "green", shape: "dot", text: node.type + " ready" });
node.log("Sensor " + node.type + " initialized.");
}).catch(function (err) {
node.status({ fill: "red", shape: "ring", text: "Sensor Init Failed" });
node.error("Sensor Init failed ->" + err);
});
};
// Init
fnInit();
// trigger measure
node.on('input', function (_msg) {
if (!node.initialized) {
//try to reinit node until no sensor is found
node.sensor.init().then(function (ID) {
node.initialized = true;
node.type = ID == BME280.CHIP_ID_BME280() ? "BME280" : "BMP280";
node.isBME = (node.type == "BME280");
node.status({ fill: "green", shape: "dot", text: node.type + " ready" });
node.log("Sensor " + node.type + " initialized.");
}).catch(function (err) {
node.status({ fill: "red", shape: "ring", text: "Sensor Init Failed" });
node.error("Sensor Init failed ->" + err);
});
fnInit();
return null;
}
node.sensor.readSensorData().then(function (data) {

_msg.payload = data;
data.model = node.type;
if (!node.isBME && _msg.payload.humidity !== undefined) delete _msg.payload.humidity;
Expand All @@ -56,7 +51,7 @@ module.exports = function (RED) {
pl.heatIndex = BME280.calculateHeatIndexCelcius(data.temperature_C, data.humidity);
pl.dewPoint_C = BME280.calculateDewPointCelcius(data.temperature_C, data.humidity);
}
pl.altitude_M = BME280.calculateAltitudeMeters(data.pressure_hPa, 0);
pl.altitude_M = BME280.calculateAltitudeMeters(data.pressure_hPa, node.preasure);
pl.temperature_F = BME280.convertCelciusToFahrenheit(data.temperature_C);
pl.pressure_Hg = BME280.convertHectopascalToInchesOfMercury(data.pressure_hPa);
}
Expand Down

0 comments on commit 00dfad1

Please sign in to comment.