Connection

Class Reference
API Reference

Connection

The Obniz class is a class that represents a device created based on obnizID, and automatically starts connecting as soon as it is instantiated, and the function specified for onconnect is called when the connection succeeds, and the function specified for onclose is called when the connection is disconnected. When the connection is successful, the function specified in onconnect is called.

Available device specification

Type Example Description
obniz ID new Obniz('1234-5678') Simple obnizID in string. hypen is ignored
obniz ID new Obniz('sn_AAAABBB') Specifying serial number. sn_ prefix is required
IP new Obniz('192.168.0.100') IPv4 of device. Only when device is on-premise mode.
// Javascript Full 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 Full Example

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

You can configure settings at initialization

Reference

// Javascript Full Example

new Obniz('1234-5678', { /* option here */ })

AccessToken

If the device has been issued an access token, the access token or the API key of the account that owns the device will be required for connection.

These connection keys are specified as access_token as shown below.

// Javascript Full Example

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

If it is not specified, Websocket will not be able to connect and a 400 series error will occur. In the browser, errors such as HTTP Authentication faild will be displayed in the console.

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 Full Example

var obniz = new Obniz('1234-5678');
console.log(obniz.connectionState) //  => === "connecting" or "closed"
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 Full 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 Full Example

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

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

Articles