periph 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +89 -66
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +72 -53
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/{transport → connection}/hx711.js +49 -22
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
|
@@ -1,50 +1,52 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
3
|
-
const { ENS160Full } = require('../../../
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { ENS160Full } = require('../../../src/chips/gas/ens160');
|
|
4
4
|
|
|
5
5
|
const AQI_LABELS = {1: 'Excellent', 2: 'Good', 3: 'Moderate', 4: 'Poor', 5: 'Unhealthy'};
|
|
6
6
|
|
|
7
7
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
8
8
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x52', 16);
|
|
9
9
|
|
|
10
|
-
const
|
|
11
|
-
const sensor = new ENS160Full(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
11
|
+
const sensor = new ENS160Full(connection); // Create ENS160 driver, (connection)
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
// --- Wait for sensor warm-up ---
|
|
15
|
+
// The ENS160 requires ~3 minutes after power-on or idle before VALIDITY_FLAG
|
|
16
|
+
// reaches 0. During warm-up, readings are unreliable. The driver surfaces the
|
|
17
|
+
// status so the application can display progress to the user.
|
|
18
|
+
console.log('Waiting for sensor warm-up...');
|
|
19
|
+
while (true) { // Wait for valid data, () → blocks until warm
|
|
20
|
+
try { await sensor.readAirQuality(); break; } catch (e) {
|
|
21
|
+
const s = await sensor.status();
|
|
22
|
+
if (s === 1) console.log('Warm-up in progress...');
|
|
23
|
+
else if (s === 2) console.log('Initial start-up (first power-on, up to 1 hour)...');
|
|
24
|
+
else console.log('No valid output');
|
|
25
|
+
_delay(1000);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
console.log('Sensor ready!');
|
|
29
|
+
|
|
30
|
+
// --- Set compensation from external sensor ---
|
|
31
|
+
// If an external temperature/humidity sensor is available, feeding its readings
|
|
32
|
+
// to the ENS160 improves accuracy outside the 20-80%RH range. Here we use a
|
|
33
|
+
// fixed 22C/45%RH as an example.
|
|
34
|
+
await sensor.setCompensation(22.0, 45.0); // Set compensation, (tempCelsius=22.0, rhPercent=45.0) → undefined
|
|
35
|
+
|
|
36
|
+
// --- Indoor air quality monitoring loop ---
|
|
37
|
+
// Reads AQI, TVOC, and eCO2 every second and prints a human-readable label.
|
|
38
|
+
// AQI 1-2 is acceptable for occupied spaces; AQI 3+ suggests ventilation.
|
|
39
|
+
for (let n = 0; n < 60; n++) {
|
|
40
|
+
const data = await sensor.readAirQuality(); // Read air quality, () → object {aqi, tvocPpb, eco2Ppm}
|
|
41
|
+
const aqi = data.aqi;
|
|
42
|
+
const label = AQI_LABELS[aqi] || 'Unknown';
|
|
43
|
+
console.log(`${n}s: AQI=${aqi} (${label}) TVOC=${data.tvocPpb} ppb eCO2=${data.eco2Ppm} ppm`);
|
|
24
44
|
_delay(1000);
|
|
25
45
|
}
|
|
26
|
-
}
|
|
27
|
-
console.log('Sensor ready!');
|
|
28
|
-
|
|
29
|
-
// --- Set compensation from external sensor ---
|
|
30
|
-
// If an external temperature/humidity sensor is available, feeding its readings
|
|
31
|
-
// to the ENS160 improves accuracy outside the 20-80%RH range. Here we use a
|
|
32
|
-
// fixed 22C/45%RH as an example.
|
|
33
|
-
sensor.setCompensation(22.0, 45.0); // Set compensation, (tempCelsius=22.0, rhPercent=45.0) → undefined
|
|
34
|
-
|
|
35
|
-
// --- Indoor air quality monitoring loop ---
|
|
36
|
-
// Reads AQI, TVOC, and eCO2 every second and prints a human-readable label.
|
|
37
|
-
// AQI 1-2 is acceptable for occupied spaces; AQI 3+ suggests ventilation.
|
|
38
|
-
for (let n = 0; n < 60; n++) {
|
|
39
|
-
const data = sensor.readAirQuality(); // Read air quality, () → object {aqi, tvocPpb, eco2Ppm}
|
|
40
|
-
const aqi = data.aqi;
|
|
41
|
-
const label = AQI_LABELS[aqi] || 'Unknown';
|
|
42
|
-
console.log(`${n}s: AQI=${aqi} (${label}) TVOC=${data.tvocPpb} ppb eCO2=${data.eco2Ppm} ppm`);
|
|
43
|
-
_delay(1000);
|
|
44
|
-
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
console.log('===DONE: 0 passed, 0 failed===');
|
|
47
|
+
await connection.close();
|
|
48
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
49
|
+
})();
|
|
48
50
|
|
|
49
51
|
function _delay(ms) {
|
|
50
52
|
const start = Date.now();
|
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
3
|
-
const { ENS160Minimal } = require('../../../
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { ENS160Minimal } = require('../../../src/chips/gas/ens160');
|
|
4
4
|
|
|
5
5
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
6
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x52', 16);
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
const sensor = new ENS160Minimal(
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const sensor = new ENS160Minimal(connection); // Create ENS160 driver, (connection)
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
11
|
+
(async () => {
|
|
12
|
+
console.log('Waiting for sensor warm-up...');
|
|
13
|
+
while (true) { // Wait for valid data, () → blocks until warm
|
|
14
|
+
try { await sensor.readAirQuality(); break; } catch (e) { _delay(1000); }
|
|
15
|
+
}
|
|
15
16
|
|
|
16
|
-
for (let i = 0; i < 10; i++) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
console.log('===DONE: 0 passed, 0 failed===');
|
|
17
|
+
for (let i = 0; i < 10; i++) {
|
|
18
|
+
const data = await sensor.readAirQuality(); // Read air quality, () → object {aqi, tvocPpb, eco2Ppm}
|
|
19
|
+
console.log(`AQI=${data.aqi} TVOC=${data.tvocPpb} ppb eCO2=${data.eco2Ppm} ppm`);
|
|
20
|
+
_delay(1000);
|
|
21
|
+
}
|
|
22
|
+
await connection.close();
|
|
23
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
24
|
+
})();
|
|
23
25
|
|
|
24
26
|
function _delay(ms) {
|
|
25
27
|
const start = Date.now();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { UARTConnection } = require('../../../src/connection/uart');
|
|
3
|
+
const { NEO6Full } = require('../../../src/chips/gnss/neo6');
|
|
4
|
+
|
|
5
|
+
// To use I2C (DDC) instead of UART:
|
|
6
|
+
// const { I2CConnection } = require('../../../src/connection/i2c');
|
|
7
|
+
// const connection = new I2CConnection(1, 0x42);
|
|
8
|
+
// const gps = new NEO6Full(connection, 'i2c');
|
|
9
|
+
// To use SPI instead of UART:
|
|
10
|
+
// const { SPIConnection } = require('../../../src/connection/spi');
|
|
11
|
+
// const connection = new SPIConnection(0, 0, { mode: 0, maxSpeedHz: 200_000 });
|
|
12
|
+
// const gps = new NEO6Full(connection, 'spi');
|
|
13
|
+
|
|
14
|
+
const UART_PORT = process.env.UART_PORT || '/dev/ttyS0';
|
|
15
|
+
|
|
16
|
+
async function main() {
|
|
17
|
+
const connection = new UARTConnection(UART_PORT, { baudRate: 9600 });
|
|
18
|
+
await connection.open();
|
|
19
|
+
const gps = new NEO6Full(connection); // Create NEO-6 driver, (connection, busType='uart')
|
|
20
|
+
|
|
21
|
+
await gps.setRate(1); // Set navigation update rate, (hz) → Promise<void>
|
|
22
|
+
// writes CFG-RATE with measRate = 1000/hz ms
|
|
23
|
+
await gps.setPlatform(0); // Set dynamic platform model, (model 0-8) → Promise<void>
|
|
24
|
+
// writes CFG-NAV5 with mask=dynModel only
|
|
25
|
+
await gps.saveConfig(); // Persist current configuration, () → Promise<void>
|
|
26
|
+
// writes CFG-CFG with saveMask=all, deviceMask=BBR|Flash|EEPROM
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < 20; i++) {
|
|
29
|
+
if (await gps.update()) { // Read + parse one NMEA sentence, () → Promise<boolean>
|
|
30
|
+
console.log(gps.latitude(), gps.longitude(), gps.altitude());
|
|
31
|
+
// decimal degrees / decimal degrees / meters MSL
|
|
32
|
+
console.log(gps.speed(), gps.course()); // Speed over ground, () → number|null m/s
|
|
33
|
+
// Course over ground, () → number|null deg
|
|
34
|
+
console.log(gps.utcTime(), gps.utcDate()); // UTC time of last fix sentence, () → string|null hhmmss.ss
|
|
35
|
+
// UTC date of last RMC sentence, () → string|null ddmmyy
|
|
36
|
+
console.log(gps.hdop()); // Horizontal dilution of precision, () → number|null
|
|
37
|
+
}
|
|
38
|
+
await new Promise(r => setTimeout(r, 50));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const navStatus = await gps.pollUbx(0x01, 0x03); // Poll a UBX message and return its payload, (msgClass, msgId) → Promise<Buffer>
|
|
42
|
+
console.log('NAV-STATUS payload:', navStatus);
|
|
43
|
+
|
|
44
|
+
await gps.coldStart(); // Force a cold start via CFG-RST, () → Promise<void>
|
|
45
|
+
|
|
46
|
+
await connection.close();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { UARTConnection } = require('../../../src/connection/uart');
|
|
3
|
+
const { NEO6Full } = require('../../../src/chips/gnss/neo6');
|
|
4
|
+
|
|
5
|
+
// To use I2C (DDC) instead of UART:
|
|
6
|
+
// const { I2CConnection } = require('../../../src/connection/i2c');
|
|
7
|
+
// const connection = new I2CConnection(1, 0x42);
|
|
8
|
+
// const gps = new NEO6Full(connection, 'i2c');
|
|
9
|
+
// To use SPI instead of UART:
|
|
10
|
+
// const { SPIConnection } = require('../../../src/connection/spi');
|
|
11
|
+
// const connection = new SPIConnection(0, 0, { mode: 0, maxSpeedHz: 200_000 });
|
|
12
|
+
// const gps = new NEO6Full(connection, 'spi');
|
|
13
|
+
|
|
14
|
+
const UART_PORT = process.env.UART_PORT || '/dev/ttyS0';
|
|
15
|
+
|
|
16
|
+
// --- Portable GPS logger ---
|
|
17
|
+
// The module self-configures at factory defaults (9600 baud NMEA, 1 Hz); no
|
|
18
|
+
// CFG messages are needed for a basic position log. Runs for 60 seconds,
|
|
19
|
+
// polling update() far faster than the 1 Hz sentence rate so no sentence is
|
|
20
|
+
// missed, and prints one line per second once a fresh GGA has been parsed.
|
|
21
|
+
async function main() {
|
|
22
|
+
const connection = new UARTConnection(UART_PORT, { baudRate: 9600 });
|
|
23
|
+
await connection.open();
|
|
24
|
+
const gps = new NEO6Full(connection); // Create NEO-6 driver, (connection, busType='uart')
|
|
25
|
+
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
while (Date.now() - start < 60_000) {
|
|
28
|
+
const gotFix = await gps.update(); // Read + parse one NMEA sentence, () → Promise<boolean>
|
|
29
|
+
|
|
30
|
+
// --- No fix yet: show the wait state ---
|
|
31
|
+
// gpsFix alone would not be trustworthy here; update() already only
|
|
32
|
+
// resolves true once the GGA fix-status field confirms a real fix,
|
|
33
|
+
// so a plain fix() === 0 check is enough to detect the waiting state.
|
|
34
|
+
if (gps.fix() === 0) {
|
|
35
|
+
console.log(`waiting for fix... satellites in use: ${gps.satellites()}`);
|
|
36
|
+
|
|
37
|
+
// --- Fix acquired: log the full position record ---
|
|
38
|
+
// Cold-start TTFF is ~26 s typical outdoors; once gotFix flips true
|
|
39
|
+
// the position, altitude, and HDOP fields below are all populated
|
|
40
|
+
// together.
|
|
41
|
+
} else if (gotFix) {
|
|
42
|
+
console.log(
|
|
43
|
+
`${gps.utcTime()} lat=${gps.latitude().toFixed(6)} lon=${gps.longitude().toFixed(6)} ` +
|
|
44
|
+
`alt=${gps.altitude().toFixed(1)} m sats=${gps.satellites()} hdop=${gps.hdop()}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
await new Promise(r => setTimeout(r, 200));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
await connection.close();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { UARTConnection } = require('../../../src/connection/uart');
|
|
3
|
+
const { NEO6Minimal } = require('../../../src/chips/gnss/neo6');
|
|
4
|
+
|
|
5
|
+
// To use I2C (DDC) instead of UART:
|
|
6
|
+
// const { I2CConnection } = require('../../../src/connection/i2c');
|
|
7
|
+
// const connection = new I2CConnection(1, 0x42);
|
|
8
|
+
// const gps = new NEO6Minimal(connection, 'i2c');
|
|
9
|
+
// To use SPI instead of UART:
|
|
10
|
+
// const { SPIConnection } = require('../../../src/connection/spi');
|
|
11
|
+
// const connection = new SPIConnection(0, 0, { mode: 0, maxSpeedHz: 200_000 });
|
|
12
|
+
// const gps = new NEO6Minimal(connection, 'spi');
|
|
13
|
+
|
|
14
|
+
const UART_PORT = process.env.UART_PORT || '/dev/ttyS0';
|
|
15
|
+
|
|
16
|
+
async function main() {
|
|
17
|
+
const connection = new UARTConnection(UART_PORT, { baudRate: 9600 });
|
|
18
|
+
await connection.open();
|
|
19
|
+
const gps = new NEO6Minimal(connection); // Create NEO-6 driver, (connection, busType='uart')
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < 20; i++) {
|
|
22
|
+
if (await gps.update()) { // Read + parse one NMEA sentence, () → Promise<boolean>
|
|
23
|
+
console.log(gps.latitude(), gps.longitude(), gps.altitude());
|
|
24
|
+
}
|
|
25
|
+
await new Promise(r => setTimeout(r, 50));
|
|
26
|
+
}
|
|
27
|
+
await connection.close();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { L3G4200DFull } = require('../../../src/chips/gyroscope/l3g4200d');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
|
|
10
|
+
(async () => {
|
|
11
|
+
const gyro = new L3G4200DFull(connection); // Create L3G4200D driver, (connection, busType='i2c')
|
|
12
|
+
const cid = await gyro.whoAmI(); // Read WHO_AM_I, () → number
|
|
13
|
+
// returns 0xD3 for L3G4200D
|
|
14
|
+
await gyro.configure(1, 0, 500); // Configure chip, (odr 0–3, bandwidth 0–3, full_scale 250/500/2000) → undefined
|
|
15
|
+
// sets CTRL_REG1 DR/BW and CTRL_REG4 FS
|
|
16
|
+
await gyro.enableAxes(true, true, true); // Enable axes, (x, y, z) → undefined
|
|
17
|
+
// sets Xen/Yen/Zen bits in CTRL_REG1
|
|
18
|
+
await gyro.setFullScale(2000); // Set full scale, (full_scale 250/500/2000) → undefined
|
|
19
|
+
// updates FS[1:0] in CTRL_REG4
|
|
20
|
+
const ready = await gyro.dataReady(); // Check data ready, () → boolean
|
|
21
|
+
// returns STATUS_REG.ZYXDA
|
|
22
|
+
const status = await gyro.status(); // Read STATUS, () → number
|
|
23
|
+
// raw status byte (ZYXOR, ZOR, YOR, XOR, ZYXDA, ZDA, YDA, XDA)
|
|
24
|
+
const temp = await gyro.temperature(); // Read temperature, () → number
|
|
25
|
+
// 8-bit signed relative count (−1 °C/digit)
|
|
26
|
+
await gyro.enableHighpass(0, 4); // Enable high-pass, (mode 0–3, cutoff 0–9) → undefined
|
|
27
|
+
// sets HPen and HPM/HPCF; cutoff depends on ODR
|
|
28
|
+
await gyro.disableHighpass(); // Disable high-pass, () → undefined
|
|
29
|
+
// clears HPen in CTRL_REG5
|
|
30
|
+
await gyro.setInterrupt(true, false, true, false, true, false, false, true); // Configure INT1, (x_high, x_low, y_high, y_low, z_high, z_low, and_mode, latch) → undefined
|
|
31
|
+
// enable high events on x/y/z; latch until INT1_SRC read
|
|
32
|
+
await gyro.setThreshold('x', 87.5); // Set X threshold, (axis 'x'/'y'/'z', threshold_dps) → undefined
|
|
33
|
+
// converts dps to raw 15-bit value via sensitivity
|
|
34
|
+
await gyro.setDuration(4, false); // Set INT1 duration, (samples 0–127, wait=false) → undefined
|
|
35
|
+
// INT1 must be true for `samples` ODR cycles before firing
|
|
36
|
+
await gyro.setDataReadyPin(true); // Route DRDY to INT2, (enable=true) → undefined
|
|
37
|
+
// sets I2_DRDY in CTRL_REG3
|
|
38
|
+
await gyro.enableFifo(2, 10); // Enable FIFO, (mode 0–4, watermark=0) → undefined
|
|
39
|
+
// mode 2 = stream mode; watermark=10 frames
|
|
40
|
+
await gyro.disableFifo(); // Disable FIFO, () → undefined
|
|
41
|
+
// bypass mode and clear FIFO_EN
|
|
42
|
+
const samples = await gyro.fifoSamples(); // Read FIFO count, () → number
|
|
43
|
+
// FSS[4:0] from FIFO_SRC_REG
|
|
44
|
+
await gyro.powerDown(); // Enter power-down, () → undefined
|
|
45
|
+
// clears PD in CTRL_REG1
|
|
46
|
+
await gyro.wakeUp(); // Wake from power-down, () → undefined
|
|
47
|
+
// sets PD; previously enabled axes restored
|
|
48
|
+
await gyro.sleep(); // Enter sleep mode, () → undefined
|
|
49
|
+
// PD=1, all axes off
|
|
50
|
+
const intSrc = await gyro.readIntSource(); // Read & clear INT1_SRC, () → number
|
|
51
|
+
// reading clears the interrupt-active bit
|
|
52
|
+
const [x, y, z] = await gyro.angularRate(); // Read X/Y/Z angular rate, () → [number, number, number] rad/s
|
|
53
|
+
console.log(`X=${x.toFixed(2)} Y=${y.toFixed(2)} Z=${z.toFixed(2)} rad/s, T=${temp}, ready=${ready}, status=0x${status.toString(16)}, fifo=${samples}, src=0x${intSrc.toString(16)}, cid=0x${cid.toString(16)}`);
|
|
54
|
+
await connection.close();
|
|
55
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
56
|
+
})();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { L3G4200DFull } = require('../../../src/chips/gyroscope/l3g4200d');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
|
|
10
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
// --- Rotation detector: 200 Hz, ±500 dps, FIFO stream with watermark 10 ---
|
|
14
|
+
// 200 Hz ODR gives 5 ms per sample — fast enough to catch hand motion but
|
|
15
|
+
// not so noisy that the FIFO drains before the watermark is reached.
|
|
16
|
+
const gyro = new L3G4200DFull(connection); // Create L3G4200D driver, (connection, busType='i2c')
|
|
17
|
+
await gyro.configure(1, 0, 500); // Configure chip, (odr=200Hz, bandwidth=0, full_scale=500) → undefined
|
|
18
|
+
await gyro.enableHighpass(0, 4); // Enable high-pass, (mode=0, cutoff=4) → undefined
|
|
19
|
+
// cutoff index 4 at 200 Hz ODR ≈ 1 Hz; strips DC drift
|
|
20
|
+
await gyro.enableFifo(2, 10); // Enable FIFO, (mode=2=stream, watermark=10) → undefined
|
|
21
|
+
|
|
22
|
+
const thresholdRadS = 90.0 * (Math.PI / 180.0);
|
|
23
|
+
let alerts = 0;
|
|
24
|
+
|
|
25
|
+
// --- Loop: wait for FIFO watermark, drain, compute mean, alert on threshold ---
|
|
26
|
+
// Stream mode keeps the oldest samples; the FIFO never blocks but the host
|
|
27
|
+
// only acts once per watermark crossing to amortise I²C overhead.
|
|
28
|
+
for (let n = 0; n < 50; n++) {
|
|
29
|
+
while ((await gyro.fifoSamples()) < 10) { // Read FIFO count, () → number
|
|
30
|
+
await sleep(5);
|
|
31
|
+
}
|
|
32
|
+
const burst = await gyro.readFifo(); // Drain FIFO, () → [[x, y, z] rad/s, ...]
|
|
33
|
+
if (!burst.length) continue;
|
|
34
|
+
let mx = 0, my = 0, mz = 0;
|
|
35
|
+
for (const s of burst) { mx += s[0]; my += s[1]; mz += s[2]; }
|
|
36
|
+
mx /= burst.length; my /= burst.length; mz /= burst.length;
|
|
37
|
+
if (Math.abs(mx) > thresholdRadS || Math.abs(my) > thresholdRadS || Math.abs(mz) > thresholdRadS) {
|
|
38
|
+
alerts++;
|
|
39
|
+
console.log(`ALERT X=${mx.toFixed(2)} Y=${my.toFixed(2)} Z=${mz.toFixed(2)} rad/s`);
|
|
40
|
+
} else {
|
|
41
|
+
console.log(` X=${mx.toFixed(2)} Y=${my.toFixed(2)} Z=${mz.toFixed(2)} rad/s`);
|
|
42
|
+
}
|
|
43
|
+
await sleep(20);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log(`Total alerts: ${alerts} / 50`);
|
|
47
|
+
await connection.close();
|
|
48
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
49
|
+
})();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { L3G4200DMinimal } = require('../../../src/chips/gyroscope/l3g4200d');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const gyro = new L3G4200DMinimal(connection); // Create L3G4200D driver, (connection, busType='i2c')
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
for (let i = 0; i < 10; i++) {
|
|
13
|
+
const [x, y, z] = await gyro.angularRate(); // Read X/Y/Z angular rate, () → [number, number, number] rad/s
|
|
14
|
+
console.log(`X=${x.toFixed(3)} Y=${y.toFixed(3)} Z=${z.toFixed(3)} rad/s`);
|
|
15
|
+
await new Promise(r => setTimeout(r, 100));
|
|
16
|
+
}
|
|
17
|
+
await connection.close();
|
|
18
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
19
|
+
})();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const { I2CConnection } = require('../../src/connection/i2c_linux');
|
|
2
|
+
const { L3GD20HFull } = require('../../src/chips/gyroscope/l3gd20h');
|
|
3
|
+
|
|
4
|
+
const conn = new I2CConnection(1, 0x6A);
|
|
5
|
+
const gyro = new L3GD20HFull(conn);
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
await gyro.configure(1, 0, 1); // Configure, (odr 0-3, bw 0-3, fullScale 0-2)
|
|
9
|
+
// sets ODR=190 Hz, bandwidth=default, full-scale=±500 dps
|
|
10
|
+
|
|
11
|
+
await gyro.configureHpFilter(0, 0); // Configure HPF, (mode 0-3, cutoff 0-15)
|
|
12
|
+
await gyro.enableHpFilter(true); // Enable HPF, (enable=true)
|
|
13
|
+
|
|
14
|
+
await gyro.configureFifo(1, 10); // Configure FIFO, (mode 0/1/2/3/7, watermark 0-31)
|
|
15
|
+
await gyro.enableFifo(true); // Enable FIFO, (enable=true)
|
|
16
|
+
|
|
17
|
+
await gyro.setPowerMode(L3GD20HFull.POWER_NORMAL); // Set power mode
|
|
18
|
+
|
|
19
|
+
const who = await gyro._readReg(0x0F, 1);
|
|
20
|
+
console.log(`WHO_AM_I: 0x${who[0].toString(16).toUpperCase()}`);
|
|
21
|
+
|
|
22
|
+
const temp = await gyro.temperature(); // Read temperature, () -> int
|
|
23
|
+
console.log(`Temperature: ${temp}`);
|
|
24
|
+
|
|
25
|
+
while (true) {
|
|
26
|
+
if (await gyro.dataReady()) { // Check data ready, () -> bool
|
|
27
|
+
const [x, y, z] = await gyro.gyro(); // Read angular rate, () -> [float, float, float] rad/s
|
|
28
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} rad/s`);
|
|
29
|
+
|
|
30
|
+
const [rx, ry, rz] = await gyro.gyroRaw(); // Read raw, () -> [int, int, int]
|
|
31
|
+
|
|
32
|
+
const level = await gyro.fifoLevel(); // FIFO level, () -> int
|
|
33
|
+
if (level > 0) {
|
|
34
|
+
const samples = await gyro.readFifo(); // Read FIFO, () -> [[float,float,float],...]
|
|
35
|
+
console.log(`FIFO: ${samples.length} samples`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
await new Promise(r => setTimeout(r, 10));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const { I2CConnection } = require('../../src/connection/i2c_linux');
|
|
2
|
+
const { L3GD20HFull } = require('../../src/chips/gyroscope/l3gd20h');
|
|
3
|
+
|
|
4
|
+
const conn = new I2CConnection(1, 0x6A);
|
|
5
|
+
const gyro = new L3GD20HFull(conn);
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
// --- Configure for shake detection at 190 Hz, ±500 dps ---
|
|
9
|
+
// 190 Hz ODR provides good temporal resolution for shake detection;
|
|
10
|
+
// ±500 dps full scale gives 17.5 mdps/digit sensitivity, suitable for
|
|
11
|
+
// detecting moderate to strong motion without clipping.
|
|
12
|
+
await gyro.configure(1, 0, 1); // Configure, (odr 0-3, bw 0-3, fullScale 0-2)
|
|
13
|
+
|
|
14
|
+
console.log('L3GD20H shake detector running. Shake the device...');
|
|
15
|
+
|
|
16
|
+
while (true) {
|
|
17
|
+
if (await gyro.dataReady()) { // Check data ready, () -> bool
|
|
18
|
+
const [x, y, z] = await gyro.gyro(); // Read angular rate, () -> [float, float, float] rad/s
|
|
19
|
+
const magnitude = Math.sqrt(x*x + y*y + z*z);
|
|
20
|
+
if (magnitude > 1.0) {
|
|
21
|
+
console.log(`SHAKE DETECTED: mag=${magnitude.toFixed(3)} (x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)})`);
|
|
22
|
+
} else {
|
|
23
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} mag=${magnitude.toFixed(3)}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const { I2CConnection } = require('../../src/connection/i2c_linux');
|
|
2
|
+
const { L3GD20HMinimal } = require('../../src/chips/gyroscope/l3gd20h');
|
|
3
|
+
|
|
4
|
+
const conn = new I2CConnection(1, 0x6A);
|
|
5
|
+
const gyro = new L3GD20HMinimal(conn);
|
|
6
|
+
|
|
7
|
+
async function loop() {
|
|
8
|
+
const [x, y, z] = await gyro.gyro(); // Read angular rate, () -> [float, float, float] rad/s
|
|
9
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} rad/s`);
|
|
10
|
+
setTimeout(loop, 100);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
loop().catch(console.error);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { DHTxxConnection } = require('periph/src/connection/dhtxx');
|
|
4
|
+
const { DHT11Full } = require('periph/src/chips/humidity/dht11');
|
|
5
|
+
|
|
6
|
+
const DATA_PIN = parseInt(process.env.DHT11_PIN || '4', 10);
|
|
7
|
+
const connection = new DHTxxConnection(DATA_PIN);
|
|
8
|
+
const dht = new DHT11Full(connection, 3); // Create DHT11 driver, (connection, max_retries=3)
|
|
9
|
+
|
|
10
|
+
(async function() {
|
|
11
|
+
const t = dht.readTemperature(); // Read temperature, () → float °C
|
|
12
|
+
// returns a fresh conversion each call
|
|
13
|
+
const h = dht.readHumidity(); // Read humidity, () → float %RH
|
|
14
|
+
// returns a fresh conversion each call
|
|
15
|
+
const r = dht.readRetry(5); // Read with retries, (max_retries=5) → {temperature, humidity}
|
|
16
|
+
// retries up to 5 times on checksum error
|
|
17
|
+
const raw = dht.readRaw(); // Read raw frame, () → Buffer
|
|
18
|
+
// returns the validated 5-byte frame
|
|
19
|
+
console.log(`t=${t} h=${h} retry_t=${r.temperature} raw[0]=0x${raw[0].toString(16).padStart(2,'0').toUpperCase()}`);
|
|
20
|
+
connection.close();
|
|
21
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
22
|
+
})();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { DHTxxConnection } = require('periph/src/connection/dhtxx');
|
|
4
|
+
const { DHT11Full } = require('periph/src/chips/humidity/dht11');
|
|
5
|
+
|
|
6
|
+
const DATA_PIN = parseInt(process.env.DHT11_PIN || '4', 10);
|
|
7
|
+
const connection = new DHTxxConnection(DATA_PIN);
|
|
8
|
+
const dht = new DHT11Full(connection, 3); // Create DHT11 driver, (connection, max_retries=3)
|
|
9
|
+
|
|
10
|
+
function comfort(h) {
|
|
11
|
+
if (h < 30.0) return 'dry';
|
|
12
|
+
if (h > 60.0) return 'humid';
|
|
13
|
+
return 'comfortable';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// --- Indoor comfort monitor ---
|
|
17
|
+
// Reads temperature and humidity every 5 seconds and prints a one-line
|
|
18
|
+
// status with a comfort assessment. Demonstrates reliable real-world polling
|
|
19
|
+
// with retry-based error recovery.
|
|
20
|
+
(async function() {
|
|
21
|
+
for (let n = 0; n < 60; n++) {
|
|
22
|
+
let r;
|
|
23
|
+
try {
|
|
24
|
+
r = dht.readRetry(3); // Read with retries, (max_retries=3) → {temperature, humidity}
|
|
25
|
+
} catch (e) {
|
|
26
|
+
// --- Handle read failure ---
|
|
27
|
+
// After all retries are exhausted, log a warning and continue.
|
|
28
|
+
// The next loop iteration will try again with a fresh sample.
|
|
29
|
+
console.log('WARN: DHT11 read failed after retries');
|
|
30
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
console.log(`${r.temperature} C, ${r.humidity} %RH, ${comfort(r.humidity)}`);
|
|
34
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
35
|
+
}
|
|
36
|
+
connection.close();
|
|
37
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
38
|
+
})();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { DHTxxConnection } = require('periph/src/connection/dhtxx');
|
|
4
|
+
const { DHT11Minimal } = require('periph/src/chips/humidity/dht11');
|
|
5
|
+
|
|
6
|
+
const DATA_PIN = parseInt(process.env.DHT11_PIN || '4', 10);
|
|
7
|
+
const connection = new DHTxxConnection(DATA_PIN);
|
|
8
|
+
const dht = new DHT11Minimal(connection); // Create DHT11 driver, (connection)
|
|
9
|
+
|
|
10
|
+
(async function() {
|
|
11
|
+
for (let i = 0; i < 5; i++) {
|
|
12
|
+
const r = dht.read(); // Read temperature & humidity, () → {temperature: float °C, humidity: float %RH}
|
|
13
|
+
console.log(`${r.temperature} C, ${r.humidity} %RH`);
|
|
14
|
+
await new Promise(r => setTimeout(r, 2000));
|
|
15
|
+
}
|
|
16
|
+
connection.close();
|
|
17
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
18
|
+
})();
|