I got some more sensors, like an Bosch BME280 which uses I²C bus. Neat as it can measure temperature, air pressure and humidity.
Using it on an OrangePi Zero is simple:
- Enable the I2C-0 module: in /boot/armbianEnv.txt add “i2c0” (or use i2c1 or i2c2) to the overlay.
- Create /etc/udev/rules.d/60-i2c-tools.rules with the following content:
KERNEL=="i2c-0" , GROUP="i2c", MODE="0660"
KERNEL=="i2c-[1-9]*", GROUP="i2c", MODE="0666"
- Add yourself to the i2c group:
useradd -a -G i2c harald
You now have an accessible /dev/i2c-0 (or i2c-1 or i2c-2). You can check you have a BME280 visible on the I2C bus:
harald@opz2:~$ i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
0x76 is the default I2C address of a BME280 (0x77 being the alternative address).
Now read it via Node.js (package from here):
const BME280 = require('bme280-sensor');
// The BME280 constructor options are optional.
//
const options = {
i2cBusNo : 0, // defaults to 1
i2cAddress : 0x76 //BME280.BME280_DEFAULT_I2C_ADDRESS() // defaults to 0x77
};
const bme280 = new BME280(options);
// Read BME280 sensor data, repeat
//
const readSensorData = () => {
bme280.readSensorData()
.then((data) => {
// temperature_C, pressure_hPa, and humidity are returned by default.
// I'll also calculate some unit conversions for display purposes.
//
data.temperature_F = BME280.convertCelciusToFahrenheit(data.temperature_C);
data.pressure_inHg = BME280.convertHectopascalToInchesOfMercury(data.pressure_hPa);
console.log(`data = ${JSON.stringify(data, null, 2)}`);
setTimeout(readSensorData, 2000);
})
.catch((err) => {
console.log(`BME280 read error: ${err}`);
setTimeout(readSensorData, 2000);
});
};
// Initialize the BME280 sensor
//
bme280.init()
.then(() => {
console.log('BME280 initialization succeeded');
readSensorData();
})
.catch((err) => console.error(`BME280 initialization failed: ${err} `));
And it works:
harald@opz2:~/js/bme280$ node bme280.js
Found BMx280 chip ID 0x60 on bus i2c-0, address 0x76
BME280 initialization succeeded
data = {
"temperature_C": 23.19,
"humidity": 35.62696153399403,
"pressure_hPa": 1003.0408627864141,
"temperature_F": 73.742,
"pressure_inHg": 29.619784150102433
}
[...]