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.

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

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

You can configure settings at initialization


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

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

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.

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.

And below is M5StickC pre configured class reference.
You can instantiate it like below method. Params are same as “new Obinz()”

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

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