Class Obniz

Class Reference

The Obniz class contains all the features of the SDK.

In the case of HTML, it is a global variable; in the case of JS and Typescript, the following is the obniz class.

var Obniz = require('obniz');

TypeScript

import Obniz from 'obniz';

The class reference is available on Github.

Instances and Devices.

An instance that takes the place of an obniz device is instantiated with an Obniz ID and other arguments.
After instantiation, the connection to the device is made automatically and onconnect() is called when the connection is completed (see connection for details).

var obniz = new Obniz('1234-5678');
obniz.onconnect = async function() {
  obniz.io0.output(true);
}

When the connection is complete, the instance receives information about the device (what IOs it has, ADs, etc.) from the obniz cloud and configures itself. It doesn't know what the device is until it is connected, so it doesn't have any properties about the device operation.

var obniz = new Obniz('1234-5678');
obniz.io0.output(true); // => error (io0 is undefined)
obniz.onconnect = async function() {
  obniz.io0.output(true); // => If io0 is a device with an io0, it works fine.
}

Predefined devices.

Some devices, such as the M5StickC, have their on-board sensors registered in advance, and each sensor is automatically initialized and ready for use as soon as the device is connected. The following is an example of the M5StickC's acceleration and gyroscope sensors.

var obniz = new Obniz.M5StickC('1234-5678');
obniz.onconnect = async function() {
  const data = await obniz.imu.getAllDataWait();
  console.log('accelerometer: %o', data.accelerometer);
  console.log('gyroscope: %o', data.gyroscope);
}

Pre-defined devices available

Articles