diff --git a/src/bme.js b/src/bme.js new file mode 100644 index 0000000..d22fc6a --- /dev/null +++ b/src/bme.js @@ -0,0 +1,21 @@ +import i2c from 'i2c-bus'; + +export async function bme(address = 0x76) { + const i2cBus = await i2c.openPromisified(1); + const data = await Promise.all([ + i2cBus.readByte(address, 0xFA), + i2cBus.readByte(address, 0xFB), + i2cBus.readByte(address, 0xFC), + i2cBus.readByte(address, 0xF7), + i2cBus.readByte(address, 0xF8), + i2cBus.readByte(address, 0xF9), + i2cBus.readByte(address, 0xFD), + i2cBus.readByte(address, 0xFE), + ]); + return { + temperature: (((data[0] << 12) | (data[1] << 4) | (data[2] >> 4)) / 16384.0 - 5120.0) / 100, + pressure: ((data[3] << 12) | (data[4] << 4) | (data[5] >> 4)) / 25600, + humidity: ((data[6] << 8) | data[7]) / 1024.0, + }; +} +console.log(await bme()); diff --git a/src/main.js b/src/main.js index 1ac7262..59ce562 100644 --- a/src/main.js +++ b/src/main.js @@ -1,8 +1,8 @@ import {ask} from './misc.js'; import Daemon from './daemon.js'; -const command = process.argv[1]; -let remote; +const command = process.argv[2]; +let remote = 'localhost'; function help() { return ` @@ -26,16 +26,16 @@ function run(cmd) { else if(cmd === 'help') return this.help(); else if(cmd.startsWith('remote')) remote = cmd.split(' ').pop(); else if(cmd.startsWith('start')) new Daemon(+cmd.split(' ').pop() || 80); - else return fetch(`${remote}/api/${cmd}`).then(resp => { + else return fetch(`${remote.startsWith('http') ? '' : 'http://'}${remote}/api/${cmd}`).then(resp => { if(resp.ok && resp.headers['Content-Type'].includes('json')) return resp.json(); else return resp.text(); - }); + }).catch(err => err.message); } if(command) console.log(run(command)); else console.log(help()); while(true) { const cmd = await ask('> '); - console.log(run(cmd), '\n'); + console.log(await run(cmd), '\n'); } diff --git a/src/sensor-suite.js b/src/sensor-suite.js index ec871fe..4c4b2ed 100644 --- a/src/sensor-suite.js +++ b/src/sensor-suite.js @@ -27,7 +27,8 @@ export default class SensorSuite { async start() { this.intervals.push(adjustedInterval(async () => { this.data.environment = await this.statusWrapper(bme(), 'bme280'); - this.data.environment.altitude = SensorSuite.#hPaToAltitude(this.data.environment.pressure); + if(this.data.environment.pressure != null) + this.data.environment.altitude = SensorSuite.#hPaToAltitude(this.data.environment.pressure); }, 1000)); await sleep(500); // Offset reading sensor data this.intervals.push(adjustedInterval(async () => {