Connection

Class Reference
API Reference

Instantiate an obniz device with obniz id. Connection will be established automatically. onconnect will be called whenc success. onclose will be called when disconnected from a device.

// Javascript Example

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

}
obniz.onclose = async function() {

}

Operations like turning on/off an io becomes possible only after connection is established, so any operations you want device to undertake must be written in onconnect

Operations written in onclose function never delivered to a device because connection is already lost. So you can't turn off a moving motor when disconnected. See more at Disconnection

// Javascript Example

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

You can configure settings at initialization

Reference

For example, specify access token when you configured on obniz Cloud device console.

// Javascript Example

new Obniz('1234-5678', {access_token: 'your token here'})
Automatic Prompt

If obniz id is incorrect, connection will never be established. In nodejs, an error occurs.
In HTML, obniz.js shows a prompt message. The user can put in a correct obniz id into it.
It shows up only when the format is invalid. If you specify obniz id which doesn't exist, this would never be shown.

Connection State

Current connection state can be read from connectionState

// Javascript Example

var obniz = new Obniz('1234-5678');
console.log(obniz.connectionState) // => === "connecting"
obniz.onconnect = async function() {
 console.log(obniz.connectionState) // => === "connected"
}

Pre-configured Device

We recommend you to use pre configured device classes.
obniz.js will get device information like how many io it has when first connection established.

// Javascript Example

new obniz = new Obniz("1234-5678")
obniz.io0 => undefined
obniz.onconnect = async function() {
  obniz.io0 => exist if device has io0
}

obniz Board and M5StickC and other official devices are pre configured in obniz.js. You can find from here.
https://obniz.github.io/obniz/obnizjs/classes/obnizcore.obniz.html

And below is M5StickC pre configured class reference.
https://obniz.github.io/obniz/obnizjs/classes/obnizcore.hardware.m5stickc.html
You can instantiate it like below method. Params are same as "new Obinz()"

// Javascript Example

new obniz = new Obniz.M5StickC("1234-5678")

By using it, peripherals and internal sensors are pre configured.

Articles