fixed bugs
This commit is contained in:
		
							
								
								
									
										21
									
								
								src/bme.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/bme.js
									
									
									
									
									
										Normal file
									
								
							@@ -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());
 | 
				
			||||||
							
								
								
									
										10
									
								
								src/main.js
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/main.js
									
									
									
									
									
								
							@@ -1,8 +1,8 @@
 | 
				
			|||||||
import {ask} from './misc.js';
 | 
					import {ask} from './misc.js';
 | 
				
			||||||
import Daemon from './daemon.js';
 | 
					import Daemon from './daemon.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const command = process.argv[1];
 | 
					const command = process.argv[2];
 | 
				
			||||||
let remote;
 | 
					let remote = 'localhost';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function help() {
 | 
					function help() {
 | 
				
			||||||
    return `
 | 
					    return `
 | 
				
			||||||
@@ -26,16 +26,16 @@ function run(cmd) {
 | 
				
			|||||||
    else if(cmd === 'help') return this.help();
 | 
					    else if(cmd === 'help') return this.help();
 | 
				
			||||||
    else if(cmd.startsWith('remote')) remote = cmd.split(' ').pop();
 | 
					    else if(cmd.startsWith('remote')) remote = cmd.split(' ').pop();
 | 
				
			||||||
    else if(cmd.startsWith('start')) new Daemon(+cmd.split(' ').pop() || 80);
 | 
					    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'))
 | 
					        if(resp.ok && resp.headers['Content-Type'].includes('json'))
 | 
				
			||||||
            return resp.json();
 | 
					            return resp.json();
 | 
				
			||||||
        else return resp.text();
 | 
					        else return resp.text();
 | 
				
			||||||
    });
 | 
					    }).catch(err => err.message);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if(command) console.log(run(command));
 | 
					if(command) console.log(run(command));
 | 
				
			||||||
else console.log(help());
 | 
					else console.log(help());
 | 
				
			||||||
while(true) {
 | 
					while(true) {
 | 
				
			||||||
    const cmd = await ask('> ');
 | 
					    const cmd = await ask('> ');
 | 
				
			||||||
    console.log(run(cmd), '\n');
 | 
					    console.log(await run(cmd), '\n');
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,6 +27,7 @@ export default class SensorSuite {
 | 
				
			|||||||
    async start() {
 | 
					    async start() {
 | 
				
			||||||
        this.intervals.push(adjustedInterval(async () => {
 | 
					        this.intervals.push(adjustedInterval(async () => {
 | 
				
			||||||
            this.data.environment = await this.statusWrapper(bme(), 'bme280');
 | 
					            this.data.environment = await this.statusWrapper(bme(), 'bme280');
 | 
				
			||||||
 | 
					            if(this.data.environment.pressure != null)
 | 
				
			||||||
                this.data.environment.altitude = SensorSuite.#hPaToAltitude(this.data.environment.pressure);
 | 
					                this.data.environment.altitude = SensorSuite.#hPaToAltitude(this.data.environment.pressure);
 | 
				
			||||||
        }, 1000));
 | 
					        }, 1000));
 | 
				
			||||||
        await sleep(500); // Offset reading sensor data
 | 
					        await sleep(500); // Offset reading sensor data
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user