Added bms monitoring script

This commit is contained in:
Zakary Timson 2024-10-28 20:28:50 +00:00
parent 93fe5f2260
commit 9178b0a83f
3 changed files with 43 additions and 17 deletions

19
src/bms.js Normal file
View File

@ -0,0 +1,19 @@
import i2c from 'i2c-bus';
async function bmsStatus(address = 0x57) {
const i2cBus = await i2c.openPromisified(1);
const data = await Promise.all([
i2cBus.readByte(address, 0x02),
i2cBus.readByte(address, 0x04),
i2cBus.readByte(address, 0x22),
i2cBus.readByte(address, 0x23),
i2cBus.readByte(address, 0x2a),
]);
return {
charging: !!((data[0] >> 7) & 1),
percentage: data[4] / 100,
temperature: data[1] - 40,
voltage: ((data[2] << 8) | data[3]) / 1000,
}
}
console.log(await bmsStatus());

View File

@ -9,17 +9,20 @@ export default class Http extends Controller {
constructor(apollo, port = 8000) { constructor(apollo, port = 8000) {
super(apollo); super(apollo);
this.express = express(); this.express = express();
this.express.get('/api/*', async (req, res) => {
const cmd = req.params['0'];
console.log(cmd);
res.json(await this.run(cmd));
});
this.express.get('*', (req, res) => { this.express.get('*', (req, res) => {
let p = req.params['0']; let p = req.params['0'];
if(!p || p == '/') p = 'index.html'; if(!p || p == '/') p = 'index.html';
const absolute = path.join(import.meta.url, '/../../ui/', p).replace('file:', ''); const absolute = path.join(import.meta.url, '/../../ui/', p).replace('file:', '');
res.sendFile(absolute); res.sendFile(absolute);
}); });
this.express.get('/api/*', async (req, res) => {
const cmd = req.params['0'];
console.log(cmd);
res.json(await this.run(cmd));
});
this.express.listen(port); this.express.listen(port);
} }
} }

View File

@ -21,16 +21,17 @@ export default class SensorSuite {
set data(d) { this._data = d; } set data(d) { this._data = d; }
get data() { return {timestamp: new Date().getTime(), ...this._data}; } get data() { return {timestamp: new Date().getTime(), ...this._data}; }
get status() { return { sensors: 'ok' }; } _env_sensor = false;
get status() { return { env_sensor: this._env_sensor ? 'ok' : 'failed' }; }
constructor() { constructor() {
this.bme280 = new BME280({i2cBusNo: 1, i2cAddress: 0x76}); this.bme280 = new BME280({i2cBusNo: 1, i2cAddress: 0x76});
} }
async start() { start() {
await this.bme280.init(); return this.bme280.init().then(() => {
// Poll environmental data // Poll environmental data
this._env_sensor = true;
this.stopBme280 = poll(async () => { this.stopBme280 = poll(async () => {
const d = await this.bme280.readSensorData(); const d = await this.bme280.readSensorData();
this._data.humidity = d.humidity / 100; this._data.humidity = d.humidity / 100;
@ -38,6 +39,9 @@ export default class SensorSuite {
this._data.pressure = d.pressure_hPa; this._data.pressure = d.pressure_hPa;
this._data.altitude = BME280.calculateAltitudeMeters(this.data.pressure); this._data.altitude = BME280.calculateAltitudeMeters(this.data.pressure);
}, 1000); }, 1000);
}).catch(err => {
this._env_sensor = false;
});
} }
stop() { stop() {