Connection/Communication to an obniz

To use io/peripherals of an obniz, connection via the internet is needed.
Now, Let's try to establish connection to an obniz by using obniz.js

obniz.js use WebSocket API to connect an obniz.
So, obniz.js also can connect by just providing obniz id.

From this lesson, the program works on both HTML and Node.js.
First instantiate obniz with Obnizwhich last time we installed and obniz id.
Like below

var obniz = new Obniz('1234-5678');

After this, it obniz will connect to an obniz with id 1234-5678 automatically.
And connection will be established after soon then you can send/receive commands.

We want to be notified when it was established. This is how to handle it

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

// here is the section called when established.
In the other hand, this section will be never called when obniz is not online.

The timing of communication

OK,,, So When to use ios of an obniz?
It is "after connection established".
If you want to output voltage from an io of an obniz, then you should write it here

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

If do like below, you will get an error. Because connection was not established. You can't send anything.

var obniz = new Obniz('1234-5678');
obniz.io0.output(true) // This will throw error
obniz.onconnect = async function(){

}

Local connect

obniz.js try to connect to an obniz directly by using WebSocket. obniz Cloud support it.
When it is established, almost all communication through local network. It reduce latency dramatically.
This is automatically done and default configration is "want to do".
See more detail on obniz.js communication doc.

close

A connection could be lost. For example, empty of a battery.
Then onclose will be called.

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

}
obniz.onclose = async function() {

}

You can write what you want to do when a connection lost.
But imoprtant point is you can't write what you want to do by an obniz.
Because connection was already lost.
So, operations like stop a motor after connection can't be placed here.

You can configure reset or not after connection lost before it happen.
See more detail on obniz.js communication doc.

AccessToken

obniz owner can restrict connection to an obniz on obniz Cloud.
In that case, connection without access token will be denied.
Pass access token to obniz.js like below.

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

Two or more obniz

When establishing connection to two or more obniz, just do like below simply.

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

}
obnizB.onconnect = async function() {

}
obnizA.onclose = async function() {

}
obnizB.onclose = async function() {

}

Or onconnect and onclose have obniz instance as first argument. So function will be used for both onconnect.

var obnizA = new Obniz('1234-5678');
var obnizB = new Obniz('0000-0000');

var onconnect = async function(obniz) {

}

var onclose = async function(obniz) {

}

obnizA.onconnect = onconnect
obnizB.onconnect = onconnect
obnizA.onclose = onclose
obnizB.onclose = onclose

Options

More options for connection available.
See more detail on obniz.js communication doc.



Next : IO: output

You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers

Forum

Visit our developer’s forum to discuss and discover technologies.

Forum

Contact

Feel free to contact out support and technical team.

Contact us