Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including i2c-sensor-am2315 with all npm packages installed. Try it out:

var i2cSensorAm2315 = require("i2c-sensor-am2315")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

i2c-sensor-am2315 v1.0.4

I2C communication driver for the AOSONG AM2315 temperature and humidity sensor.

I2C Driver for Sensor AM2315

Build Status Git Issues NPM Downloads Current Version

I2C communication driver for the AOSONG AM2315 temperature and humidity sensor.

Installation

Using npm

Install via npm using:

npm install i2c-sensor-am2315 --save

Dependencies

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:

  1. Download the i2c-sensor-am2315 repo.
  2. Comment out the i2c-bus dependency in package.json
  3. Install the i2c-sensor-am2315 module with the path inside your project folder (npm install)

Usage

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));
    }
});

Result:

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 }

Conversions

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

Configuration

Custom driver configuration is available using the following methods, although rarely needed:

Driver.setI2cBusNumber(bus)

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);

Driver.setDeviceAddress(address)

Default is 0x5c - the AM2315 microcontroller uses a fixed address of 0x5c on the I2C bus, you should never have to change this value.

Driver.setReadCmdRegister(register)

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.

Driver.setDebugMode(true)

Default is false - set it to true to output additional debugging information to the console.

Compatibility

Tested to work on:

  • Raspberry Pi 2 Model B, Linux raspberrypi 4.1.15-v7+

Let me know of other tested devices and I'll add them to the list.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free