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.
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
You can configure settings at initialization
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.
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 faildwill be displayed in the console.
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.
Current connection state can be read from connectionState
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.
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()"
By using it, peripherals and internal sensors are pre configured.