I2C communication driver for the AOSONG AM2315 temperature and humidity sensor.
Install via npm
using:
npm install i2c-sensor-am2315 --save
To successfully install i2c-bus, and thus this library from npm, access to the i2c-dev interface and library is needed. If you plan to develop on a Mac or PC:
- Download the i2c-sensor-am2315 repo.
- Comment out the
i2c-bus
dependency in package.json - Install the
i2c-sensor-am2315
module with the path inside your project folder (npm install)
The driver supports an asynchronous read()
command:
// init prerequisites
var Driver = require("i2c-sensor-am2315");
// create device
var device = new Driver();
// read the sensor
device.read(function (err, data) {
if (err) {
console.error(err);
} else {
console.log("Original in K");
console.log(data);
console.log("Convert K to °F");
console.log(device.convertKelvinToFahrenheit(data));
}
});
You can also use the async-await syntax with the readAsync()
command:
// init prerequisites
var Driver = require("i2c-sensor-am2315");
// create device
var device = new Driver();
// read the sensor
var data = await device.readAsync();
console.log("Original in K");
console.log(data);
console.log("Convert K to °F");
console.log(device.convertKelvinToFahrenheit(data));
Original in K
{
temperature: 296.25,
temperatureUnit: 'K',
humidity: 36.3,
humidityUnit: '%RH',
crcCheck: true,
validReading: true
}
Convert K to °F
{
temperature: 73.58,
temperatureUnit: '°F',
humidity: 36.3,
humidityUnit: '%RH',
crcCheck: true,
validReading: true
}
Temperature is reported by default in SI units (Kelvin), but the driver comes with built-in temperature conversion functions to °F and °C. The functions can convert both atomic numeric values as well as an object with the same structure as the output data. Methods:
var output = Driver.convertKelvinToCelsius(input);
var output = Driver.convertKelvinToFahrenheit(input);
var output = Driver.convertCelsiusToKelvin(input);
var output = Driver.convertCelsiusToFahrenheit(input);
var output = Driver.convertFahrenheitToKelvin(input);
var output = Driver.convertFahrenheitToCelsius(input);
The output matches the input, if input is numeric, then output is numeric. If input is an object, then so is output, however the function does not modify the original input values
Custom driver configuration is available using the following methods, although rarely needed:
Default is 1 - the default I2C bus on the majority of Raspeberry Pi devices. Use an integer value relevant to your device/controller.
// init prerequisites
var Driver = require('i2c-sensor-am2315');
// create device
var device = new Driver;
// set to bus 0 (older Raspberry Pi devices)
device.setI2cBusNumber(0);
Default is 0x5c - the AM2315 microcontroller uses a fixed address of 0x5c on the I2C bus, you should never have to change this value.
Default is 0x03 - also a value you should not have to change, unless you customize this driver library to implement a different read register command.
Default is false - set it to true to output additional debugging information to the console.
Tested to work on:
- Raspberry Pi 2 Model B, Linux raspberrypi 4.1.15-v7+
- Raspberry Pi 4
- Raspberry Pi Zero 2
Let me know of other tested devices and I'll add them to the list.