obniz with runkit

You can use obniz on runkit
It is useful for easy test of obniz.js.

Start with runkit

Look at this code.

// Runkit Example

const Obniz = require("obniz");
const yourObnizId = "OBNIZ_ID_HERE";  // write your obniz id

let obniz = new Obniz(yourObnizId);

let connected = await obniz.connectWait({timeout:10});

if(connected){
  obniz.display.clear();
  obniz.display.print("Hello obniz!");

  await obniz.wait(1000);
  obniz.close();
}

The code are editable on this page, and can run now!

Write your obniz id instead of OBNIZ_ID_HERE.
Click OBNIZ_ID_HERE in the code, and write.

For example, it will become like this.
const yourObnizId = "1234-5678";

After that, click "run" which is the bottom of code.

Run your code and will display "Hello obniz!" on your obniz.

Serverless with obniz

If you make serverless code with obniz, it need a little change from HTML or Node.js.
The difference is connect / disconnect.

  • Connecting

On HTML or Node.js, you can wait for connect to obniz.
but on the serverless, it need to response something immediately.
You cannot wait connecting to obniz long time.

So you can use let connected = await obniz.connectWait({timeout:10}) instead of obniz.onconnect = function(){}.
It means try to connect and timeout 10sec, and return true if connected.

Please use if(connected){} after that.

  • Disconnecting

On HTML , you can close the page and automatically disconnect.
On Node.js, you can stop app and disconnected.

But on serverless, you cannot control when disconnect.
It means sometimes remain connection after serverless function finished.

It will be trouble, so you can use obniz.close(); when function finishe.
Only that, you will see text on the obniz display a moment.
With await obniz.wait(1000);, you will see 1 sec.

Run runkit with your account

  1. login runkit
  2. You will see code editor.

  1. Copy this code and paste.
// Runkit Example

const Obniz = require("obniz");
const yourObnizId = "OBNIZ_ID_HERE";  // write your obniz id

let obniz = new Obniz(yourObnizId);

let connected = await obniz.connectWait({timeout:10});

if(connected){
  obniz.display.clear();
  obniz.display.print("Hello obniz!");

  await obniz.wait(1000);
  obniz.close();
}

Click run!

Did your obniz display Hello obniz!?
When you want to retry, click green button.

Stay LED Light

Let's try to turn on LED from runkit.
Connect obniz and LED like this.

  • anode : pin0
  • cathode : pin1

// Runkit Example

const Obniz = require("obniz");
const yourObnizId = "OBNIZ_ID_HERE";  // write your obniz id

let obniz = new Obniz(yourObnizId);

let connected = await obniz.connectWait({timeout:10});

if(connected){
  let led = obniz.wired("LED", {anode:0, cathode:1});
  led.on();

  await obniz.wait(1000);
  obniz.close();
}

This is code of turn on LED during 1sec.

If you want to stay LED on, use resetOnDisconnect func.
It means stay last state of obniz.

Let's use it.

// Runkit Example

const Obniz = require("obniz");
const yourObnizId = "OBNIZ_ID_HERE";  // write your obniz id

let obniz = new Obniz(yourObnizId);

let connected = await obniz.connectWait({timeout:10});

if(connected){
     let led = obniz.wired("LED", {anode:0, cathode:1});
     led.on();

     obniz.resetOnDisconnect(false);
     obniz.close();
}

Click Run!
LED will be stay on.

If you want to cange off, you need to write like this.

// Runkit Example

const Obniz = require("obniz");
const yourObnizId = "OBNIZ_ID_HERE";  // write your obniz id

let obniz = new Obniz(yourObnizId);

let connected = await obniz.connectWait({timeout:10});

if(connected){
     let led = obniz.wired("LED", {anode:0, cathode:1});
     led.off();

     obniz.resetOnDisconnect(false);
     obniz.close();
}


Next : [Runkit]Use webhook

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