Skip to content

Commit 21324fa

Browse files
committed
feat: differential pressure
1 parent da0bec7 commit 21324fa

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bmp180 | 77 | ?? | pressure
1919
bh1750 | 5c | 10 | light
2020
cajoe | 08 | 00 | radiation
2121
ads1115 | 48 | ?? | solar irradiance & rain intensity
22+
sdp810 | 25 | 362F | differential pressure
2223

2324
Ads1115 is a 16-bit analog to digital convertor and can have multiple targets.
2425

src/db.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ module.exports = class Database {
5656
radiationNow float,
5757
radiationAvg float,
5858
solarIrradiance int(11),
59-
rainIntensity int(11)
59+
rainIntensity int(11),
60+
differentialPressure float
6061
);`
6162
);
6263
} catch (e) {
@@ -104,14 +105,15 @@ module.exports = class Database {
104105
"windDirNow, windGust, windSpeedAvg, windDirAvg, temperature, " +
105106
"humidity, pressure, lightness, dewPoint, solarIrradiance, " +
106107
"absoluteHumidity, feelsLikeTemp, radiationNow, radiationAvg, " +
107-
"rainIntensity)" +
108-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
108+
"rainIntensity, differentialPressure)" +
109+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
109110
[
110111
Date.now(), data.windSpeedNow, data.windDirNow, data.windGust,
111112
data.windSpeedAvg, data.windDirAvg, data.temperature,
112113
data.humidity, data.pressure, data.lightness, data.dewPoint,
113114
data.solarIrradiance, data.absoluteHumidity, data.feelsLikeTemp,
114-
data.radiationNow, data.radiationAvg, data.rainIntensity
115+
data.radiationNow, data.radiationAvg, data.rainIntensity,
116+
data.differentialPressure
115117
]);
116118
log("DB", 0, "Saved weather to database.");
117119
} catch (e) {

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const readPressure = require("./sensors/bmp180");
3131
const readLight = require("./sensors/bh1750");
3232
const readRadiation = require("./sensors/cajoe");
3333
const readAnalog = require("./sensors/ads1115");
34+
const readDifferentialPressure = require("./sensors/sdp810");
3435

3536
const rain = new (require("./sensors/rs1"))();
3637
const db = new (require("./db"))();
@@ -149,13 +150,15 @@ parser.on("data", async data => {
149150
pressure,
150151
lightness,
151152
radiation,
152-
[ solarIrradiance, rainIntensity ]
153+
[ solarIrradiance, rainIntensity ],
154+
differentialPressure
153155
] = await Promise.all([
154156
readTemp(),
155157
readPressure(),
156158
readLight(),
157159
readRadiation(),
158-
readAnalog()
160+
readAnalog(),
161+
readDifferentialPressure()
159162
]);
160163

161164
// correction math for analog sensors
@@ -197,6 +200,7 @@ parser.on("data", async data => {
197200
pressure,
198201
lightness,
199202
solarIrradiance,
203+
differentialPressure: parseFloat(differentialPressure.toFixed(2)),
200204
radiationNow: radiation,
201205
radiationAvg: parseFloat(arrAvg(radiationValues).toFixed(2)),
202206
dewPoint: parseFloat(dewPoint(temp, hum).toFixed(1)),

src/sensors/sdp810.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { log } = require("../utils");
2+
const i2c = require("i2c-bus");
3+
4+
const addr = 0x25;
5+
6+
/**
7+
* Read the differential pressure
8+
* @returns {Promise<number>} the differential pressure
9+
*/
10+
module.exports = async () => {
11+
try{
12+
let i2c1 = await i2c.openPromisified(1);
13+
await i2c1.i2cWrite(addr, 2, Buffer.from([0x36, 0x2F]));
14+
await new Promise(res => setTimeout(res, 45));
15+
let buf = Buffer.alloc(9);
16+
await i2c1.i2cRead(addr, buf.length, buf)
17+
if (crc(Uint8Array.prototype.slice.call(buf, 0, 2)) !== buf[2]) throw new Error("Crc 1 failed");
18+
if (crc(Uint8Array.prototype.slice.call(buf, 3, 5)) !== buf[5]) throw new Error("Crc 2 failed");
19+
if (crc(Uint8Array.prototype.slice.call(buf, 6, 8)) !== buf[8]) throw new Error("Crc 3 failed");
20+
let dp = ((buf[0] << 8) | buf[1]) / ((buf[6] << 8) | buf[7]);
21+
log("SENSOR", 2, `New differential pressure data, ${dp}`);
22+
await i2c1.close();
23+
return dp
24+
} catch(e) {
25+
log("SENSOR", 0, `sdp810, ${e}`, true);
26+
}
27+
};
28+
29+
function crc(data) {
30+
const polynomial = 0x31
31+
let crc = 0xFF
32+
for (const byte of data) {
33+
crc ^= byte
34+
for (let i = 8; i > 0; --i) {
35+
crc = crc & 0x80 ? (crc << 1) ^ polynomial : crc << 1
36+
crc &= 0xFF
37+
}
38+
}
39+
return crc;
40+
}

0 commit comments

Comments
 (0)