forked from AgilatechSystems/bme280
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme280.js
645 lines (547 loc) · 18.7 KB
/
bme280.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
module.exports = class Bme280 {
constructor(options) {
let opts = options || {};
this.device = {};
this.device.name = (opts.hasOwnProperty('name')) ? opts.name : 'Bme280';
this.device.type = (opts.hasOwnProperty('type')) ? opts.type : 'sensor';
this.device.active = false;
this.device.interface = (opts.hasOwnProperty('interface')) ? opts.interface : 'i2c';
this.device.bus = (opts.hasOwnProperty('bus')) ? opts.bus : 1;
this.device.addr = (opts.hasOwnProperty('addr')) ? opts.addr : 0x76;
this.device.device = (opts.hasOwnProperty('device')) ? opts.device : 0;
this.device.elevation = (opts.hasOwnProperty('elevation')) ? Number(opts.elevation) : 0;
this.device.mode = (opts.hasOwnProperty('mode')) ? opts.mode : 'forced';
this.device.refresh = (opts.hasOwnProperty('refresh')) ? opts.refresh : 10000;
this.device.version = require('./package.json').version;
this.device.parameters = [
{ name: 'pressure', type: 'float', value: NaN },
{ name: 'temperature', type: 'float', value: NaN },
{ name: 'humidity', type: 'float', value: NaN }
];
this.isStale = true;
this.timer = null;
this.mutex = false;
if (this.device.interface == 'spi') {
const spi = require('spi-device');
this.bus = spi.openSync(this.device.bus, this.device.device);
}
else {
const i2c = require('i2c-bus');
this.bus = i2c.openSync(this.device.bus);
}
if (opts.hasOwnProperty('spiaddr') && (opts.spiaddr >= 0) && (opts.spiaddr < 8)) {
this._init_multispi(opts);
}
this._initialize();
}
deviceName() {
return this.device.name;
}
deviceType() {
return this.device.type;
}
deviceVersion() {
return this.device.version;
}
deviceNumValues() {
return this.device.parameters.length;
}
typeAtIndex(idx) {
return this.device.parameters[idx].type;
}
nameAtIndex(idx) {
return this.device.parameters[idx].name;
}
deviceActive() {
return this.device.active;
}
getDataFromDevice() {
return new Promise( (resolve, reject) => {
if (!this.device.active) {
reject("Device not active");
}
// the device is sleeping if it is in either sleep or forced mode, so we need
// to wake it up before a measurement is taken by selecting forced mode
if ((this.device.mode === 'sleep') || (this.device.mode === 'forced')) {
this.setMode('forced');
}
// read the entire data block at once and pry out the values as we need them
this._readRegisters(this.register.PRESSUREDATA, 8).then( buffer=> {
this._setTemperature(Bme280.uint20(buffer[3], buffer[4], buffer[5]));
this._setPressure(Bme280.uint20(buffer[0], buffer[1], buffer[2]));
this._setHumidity(Bme280.uint16(buffer[6], buffer[7]));
resolve();
}).catch(err => {
reject(err);
});
});
}
valueAtIndex(idx) {
return new Promise ((resolve, reject) => {
if (!this._isIdxInRange(idx)) {
reject(`Bme280 Error: index ${idx} out of range`);
}
// no need to fetch all parameters from the device every single time someone
// wants to access a single value. So check to see if the data is stale...
else if (this.isStale) {
this.getDataFromDevice().then(() => {
this._resetStaleTimer();
resolve(this.device.parameters[idx].value);
}).catch(err => {
reject(err);
});
}
else {
resolve(this.device.parameters[idx].value);
}
});
}
getValueByName(name) {
return new Promise((resolve, reject) => {
const regexParam = /pressure|temperature|humidity/;
if (!regexParam.test(name)) {
reject(`getValueByName error : unknown parameter '${name}'`);
}
var idx;
this.device.parameters.forEach((param, index) => {
if (param.name === name) {
idx = index;
}
});
this.valueAtIndex(idx).then(val => {
resolve(val);
}).catch(err => {
reject(err);
});
});
}
setMode(newMode) {
const regexMode = /^sleep|forced|normal$/;
if (regexMode.test(newMode)) {
this.device.mode = newMode;
}
else {
return;
}
const ctrl_meas = (this.sampling.X1 << 5) | (this.sampling.X1 << 3) | this.mode[this.device.mode];
this._writeRegister(this.register.CONTROL, ctrl_meas).then(async () => {
// wait until measurement has been completed,
// otherwise we would read the values from the last measurement
while ((await this._readRegister(this.register.STATUS)) && 0b1000) {
await this._sleep(4);
}
}).catch(err => {
this._logError(`Could not set mode ${newMode}`);
});
}
reset() {
this.isStale = true;
clearTimeout(this.timer);
this.timer = null;
this.device.parameters.forEach(param => {
param.value = NaN;
})
this._initialize();
}
_initialize() {
this.device.active = false;
this._loadConstants();
this._readChipId().then( async res => {
// reset the device using soft-reset
// this makes sure the IIR is off, etc.
this._writeRegister(this.register.SOFTRESET, this.constant.RESET).then( async () => {
// As per data sheet, startup time is 2 ms, so we'll double that
await this._sleep(4);
await this._setCalibration(); // read trimming parameters, see DS 4.2.2
this._setSampling();
// if chip is still reading calibraion, delay
while (await this._isReadingCalibration()) {
await this._sleep(112);
}
this.device.active = true;
}).catch(err => {
this._logError(err);
});
}).catch( err => {
let errdev = `i2c device on bus ${this.device.bus} with address ${this.device.addr}`;
if (this.device.interface == 'spi') {
errdev = `spi device ${this.device.bus}.${this.device.device}`;
}
this._logError(`Could not initialize ${errdev} : ${err}`);
});
}
_init_multispi(opts) {
this.device.multispi = true;
this.gpio = require('onoff').Gpio;
// The following options are only used to activate multiple SPI devices using a
// 3-to-8 line decoder demultiplexer similar to the 74HC138. If only a single device
// is being controlled by this module, then these options can be ignored.
// The spiaddr is an address from 0-7 which is used by the demultiplexer to activate
// the corresponding output, which in turn activates the sensor's chip select.
this.device.spiaddr = opts.spiaddr;
// The spiselect is a GPIO pin which acts as a hardware mutex. It must be set high to enable
// any one of the multiple sensors. Because of this, before a sensor can be selected, it
// first must check to see that this GPIO is low and then set it high to lock the mutex.
if (opts.hasOwnProperty('spiselect') && (opts.spiselect > 0)) {
this.device.spimutex = new this.gpio(opts.spiselect, 'out');
// The spibits are used to address a single sensor. To do this, the GPIOs which
// are physically connected to the demultiplexer chip must be defined here.
if ((opts.hasOwnProperty('spibit0') && (opts.spibit0 > 0)) &&
(opts.hasOwnProperty('spibit1') && (opts.spibit1 > 0)) &&
(opts.hasOwnProperty('spibit2') && (opts.spibit2 > 0))) {
this.device.spibit0 = new this.gpio(opts.spibit0, 'out');
this.device.spibit1 = new this.gpio(opts.spibit1, 'out');
this.device.spibit2 = new this.gpio(opts.spibit2, 'out');
const exitHandler = (evtName) => {
process.on(evtName, _ => {
// this is a bit dangerous, but helps to avoid a stuck mutex
if (this.mutex) {
this.device.spimutex.writeSync(0);
}
process.exit(0);
});
}
exitHandler('SIGINT');
exitHandler('SIGTERM');
exitHandler('SIGHUP');
}
else {
this._logError('Multiplex SPI failed due to incompatible gpio spibit definition');
this.device.multispi = false;
}
}
else {
this._logError('Multiplex SPI failed due to incompatible spiselect definition');
this.device.multispi = false;
}
}
_readChipId() {
return new Promise((resolve, reject) => {
this._readRegister(this.register.CHIPID).then(chipId => {
if (chipId == this.constant.CHIP_ID) {
resolve(true);
}
else {
reject(`Unexpected chip ID ${chipId.toString(16)}`);
}
}).catch(err => {
reject(err);
});
});
}
_writeRegister(addr, data) {
return new Promise( (resolve, reject) => {
if (this.device.interface == 'spi') {
const message = [{
sendBuffer: Buffer.alloc(2),
byteLength: 2
}];
addr = addr & 0x7F;
message[0].sendBuffer[0] = addr;
message[0].sendBuffer[1] = data;
this._selectspi().then(() => {
this.bus.transfer(message, (err, mesg) => {
this._deselectspi();
if (err) {
reject(err);
}
else {
resolve();
}
});
}).catch(err => {
reject(`Could not select spi: ${err}`);
});
}
else {
this.bus.writeByte(this.device.addr, addr, data, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
}
});
}
_readRegister(addr) {
return new Promise( (resolve, reject) => {
this._readRegisters(addr, 1).then(buf => {
resolve(buf.readUInt8(0));
}).catch( err => {
reject(err);
});
});
}
_readRegisters(addr, len) {
addr = addr | 0x80;
return new Promise( (resolve, reject) => {
if (this.device.interface == 'spi') {
const message = [{
sendBuffer: Buffer.alloc(len+1),
receiveBuffer: Buffer.alloc(len+1),
byteLength: len+1
}];
message[0].sendBuffer[0] = addr;
this._selectspi().then(() => {
this.bus.transfer(message, (err, mesg) => {
this._deselectspi();
if (err) {
reject(err);
}
else {
resolve(mesg[0].receiveBuffer.slice(1));
}
});
}).catch(err => {
reject(`Could not select spi: ${err}`);
});
}
else {
this.bus.readI2cBlock(this.device.addr, addr, len, Buffer.alloc(len), (err, bytesRead, buffer) => {
if (err) {
reject(err);
}
else {
resolve(buffer);
}
});
}
});
}
_selectspi() {
return new Promise(resolve => {
if (this.device.multispi) {
if (this.device.spimutex.readSync() == 0) {
this._activatespi();
resolve();
}
else {
var locktime = setInterval( () => {
if (this.device.spimutex.readSync() == 0) {
this._activatespi();
clearInterval(locktime);
resolve();
}
}, 200);
}
}
else {
resolve();
}
});
}
_deselectspi() {
if (this.device.multispi) {
this.device.spimutex.writeSync(0);
this.mutex = false;
}
}
_activatespi() {
this.device.spibit0.writeSync(this.device.spiaddr & 1);
this.device.spibit1.writeSync(this.device.spiaddr >>> 1 & 1);
this.device.spibit2.writeSync(this.device.spiaddr >>> 2 & 1);
this.mutex = true;
this.device.spimutex.writeSync(1);
}
_setPressure(adc_P) {
let var1 = this.t_fine / 2 - 64000;
let var2 = var1 * var1 * this.calibration.dig_P6 / 32768;
var2 = var2 + var1 * this.calibration.dig_P5 * 2;
var2 = var2 / 4 + this.calibration.dig_P4 * 65536;
var1 = (this.calibration.dig_P3 * var1 * var1 / 524288 + this.calibration.dig_P2 * var1) / 524288;
var1 = (1 + var1 / 32768) * this.calibration.dig_P1;
// need to avoid division by zero
if (var1 !== 0) {
let p = 1048576 - adc_P;
p = ((p - var2 / 4096) * 6250) / var1;
var1 = this.calibration.dig_P9 * p * p / 2147483648;
var2 = p * this.calibration.dig_P8 / 32768;
p = (p + (var1 + var2 + this.calibration.dig_P7) / 16) / 100;
if (this.device.elevation > 0) {
p = this._seaLevelPressure(p);
}
this.device.parameters[0].value = Math.round(p * 100) / 100;
}
else {
this.device.parameters[0].value = NaN; // uh oh, we must be in deep space
}
}
_seaLevelPressure(pressure_mb) {
return pressure_mb * Math.pow((1 - ((0.0065 * this.device.elevation) / (this.device.parameters[1].value + 0.0065 * this.device.elevation + 273.15))), -5.257);
}
_setTemperature(adc_T) {
let var1 = ((((adc_T >> 3) - (this.calibration.dig_T1 << 1))) * this.calibration.dig_T2) >> 11;
let var2 = (((((adc_T >> 4) - this.calibration.dig_T1) * ((adc_T >> 4) - this.calibration.dig_T1)) >> 12) * this.calibration.dig_T3) >> 14;
this.t_fine = var1 + var2;
// Temperature is pretty simple
this.device.parameters[1].value = Math.round(((this.t_fine * 5 + 128) >> 8) / 10) / 10;
}
_setHumidity(adc_H) {
let var1 = this.t_fine - 76800;
var1 = (adc_H - (this.calibration.dig_H4 * 64 + this.calibration.dig_H5 / 16384 * var1)) *
(this.calibration.dig_H2 / 65536 * (1 + this.calibration.dig_H6 / 67108864 * var1 * (1 + this.calibration.dig_H3 / 67108864 * var1)));
var1 = var1 * (1 - this.calibration.dig_H1 * var1 / 524288);
const hum = (var1 > 100) ? 100 : (var1 < 0 ? 0 : var1);
this.device.parameters[2].value = Math.round(hum * 10) / 10;
}
_resetStaleTimer() {
this.isStale = false;
if (this.timer == null) {
this.timer = setTimeout(() => {
this.isStale = true;
clearTimeout(this.timer);
this.timer = null;
}, this.device.refresh);
}
}
_sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
_logError(err) {
console.error(`${this.device.name} ERROR: ${err}`);
}
_isIdxInRange(idx) {
if ((idx < 0) || (idx >= this.device.parameters.length)) {
return false;
}
return true;
}
async _setCalibration() {
let buffer = await this._readRegisters(this.register.DIG_T1, 24);
let h1 = await this._readRegister(this.register.DIG_H1);
let h2_buf = await this._readRegisters(this.register.DIG_H2, 2);
let h2 = Bme280.int16(h2_buf[1], h2_buf[0]);
let h3 = await this._readRegister(this.register.DIG_H3);
let h4 = await this._readRegister(this.register.DIG_H4);
let h5 = await this._readRegister(this.register.DIG_H5);
let h5_1 = await this._readRegister(this.register.DIG_H5 + 1);
let h6 = await this._readRegister(this.register.DIG_H6);
this.calibration = {
dig_T1: Bme280.uint16(buffer[1], buffer[0]),
dig_T2: Bme280.int16(buffer[3], buffer[2]),
dig_T3: Bme280.int16(buffer[5], buffer[4]),
dig_P1: Bme280.uint16(buffer[7], buffer[6]),
dig_P2: Bme280.int16(buffer[9], buffer[8]),
dig_P3: Bme280.int16(buffer[11], buffer[10]),
dig_P4: Bme280.int16(buffer[13], buffer[12]),
dig_P5: Bme280.int16(buffer[15], buffer[14]),
dig_P6: Bme280.int16(buffer[17], buffer[16]),
dig_P7: Bme280.int16(buffer[19], buffer[18]),
dig_P8: Bme280.int16(buffer[21], buffer[20]),
dig_P9: Bme280.int16(buffer[23], buffer[22]),
dig_H1: h1,
dig_H2: h2,
dig_H3: h3,
dig_H4: (h4 << 4) | (h5 & 0xF),
dig_H5: (h5_1 << 4) | (h5 >> 4),
dig_H6: h6
}
}
_setSampling() {
// TODO: allow sampling and standby parameters to be user-configured
const ctrl_hum = this.sampling.X1;
const config = (this.standby.MS_1000 << 5) | (this.filter.X1 << 3);
// pressure sampling temperature sampling
const ctrl_meas = (this.sampling.X1 << 5) | (this.sampling.X1 << 3) | this.mode[this.device.mode];
// you must make sure to also set register.CONTROL after setting the
// CONTROLHUMID register, otherwise the values won't be applied (see DS 7.4.3)
this._writeRegister(this.register.CONTROLHUMID, ctrl_hum).then( ()=> {
this._writeRegister(this.register.CONFIG, config).then( () => {
this._writeRegister(this.register.CONTROL, ctrl_meas).catch(err => {
this._logError('setSampling register.CONTROL error');
});
}).catch(err => {
this._logError('setSampling register.CONFIG error');
});
}).catch(err => {
this._logError('setSampling register.CONTROLHUMID error');
});
}
_isReadingCalibration() {
return new Promise(resolve => {
this._readRegister(this.register.STATUS).then(async status => {
resolve((status & 1));
});
});
}
_loadConstants() {
this.register = {
DIG_T1: 0x88, //Bme280_TEMP_PRESS_CALIB_DATA_ADDR
DIG_T2: 0x8A,
DIG_T3: 0x8C,
DIG_P1: 0x8E,
DIG_P2: 0x90,
DIG_P3: 0x92,
DIG_P4: 0x94,
DIG_P5: 0x96,
DIG_P6: 0x98,
DIG_P7: 0x9A,
DIG_P8: 0x9C,
DIG_P9: 0x9E,
DIG_H1: 0xA1,
DIG_H2: 0xE1, //Bme280_HUMIDITY_CALIB_DATA_ADDR
DIG_H3: 0xE3,
DIG_H4: 0xE4,
DIG_H5: 0xE5,
DIG_H6: 0xE7,
CHIPID: 0xD0,
VERSION: 0xD1,
SOFTRESET: 0xE0,
CAL26: 0xE1, // R calibration stored in 0xE1-0xF0
CONTROLHUMID: 0xF2, //Bme280_CTRL_HUM_ADDR
STATUS: 0XF3,
CONTROL: 0xF4, //Bme280_PWR_CTRL_ADDR Bme280_CTRL_MEAS_ADDR
CONFIG: 0xF5, //Bme280_CONFIG_ADDR
PRESSUREDATA: 0xF7, //Bme280_DATA_ADDR
TEMPDATA: 0xFA, // 0xF7 to 0xFE is burst for temp, pres, and hum
HUMIDDATA: 0xFD
}
this.sampling = {
NONE: 0b000,
X1: 0b001,
X2: 0b010,
X4: 0b011,
X8: 0b100,
X16: 0b101
};
this.mode = {
sleep: 0b00,
forced: 0b01,
normal: 0b11
};
this.filter = {
OFF: 0b000,
X2: 0b001,
X4: 0b010,
X8: 0b011,
X16: 0b100
};
// inactive duration (standby time) in normal mode
this.standby = {
MS_0_5: 0b000, // 000 = 0.5 ms
MS_62_5: 0b001, // 001 = 62.5 ms
MS_125: 0b010, // 010 = 125 ms
MS_250: 0b011, // 011 = 250 ms
MS_500: 0b100, // 100 = 500 ms
MS_1000: 0b101, // 101 = 1000 ms
MS_10: 0b110, // 110 = 10 ms
MS_20: 0b111 // 111 = 20 ms
};
this.constant = {
CHIP_ID: 0x60,
RESET: 0xB6
}
}
static int16(msb, lsb) {
let val = Bme280.uint16(msb, lsb);
return val > 32767 ? (val - 65536) : val;
}
static uint16(msb, lsb) {
return msb << 8 | lsb;
}
static uint20(msb, lsb, xlsb) {
return ((msb << 8 | lsb) << 8 | xlsb) >> 4;
}
}