obniz.js

obnizobnizOSをobniz websocket APIを使いJavaScriptから操作するためのsdkです。

ソース

GitHub

Example

import * as Obniz from 'obniz'

const obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
obniz.onconnect = async () => {

  obniz.display.print("hello!");
  obniz.switch.onchange = (state: string) => {
    console.log(state);
  }
  const servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
  servo.angle(90);
}

Example: Browser

<html>
<head>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://unpkg.com/obniz/obniz.js"></script>
</head>
<body>

<input id="text">
<button id="send">send</button>

<script>
  var obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
  obniz.onconnect = async function () {
    // embed parts
    obniz.display.print("hello!");
    obniz.switch.onchange = function(state) {
      $('body').css({
        "background-color" : (state == "push") ? "#F00" : "#FFF"
        });
    }

    // parts library
    var servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
    servo.angle(90);
    
    // peripherals
    var uart = obniz.getFreeUart();
    uart.start({tx: 5, rx: 6, baud:9600});  
    
    $('#send').click(function () {
      uart.send($("#text").val());
    });

    obniz.io7.drive("5v")
    obniz.io7.output(true)
    obniz.io8.pull("3v");
    obniz.io8.drive("open-drain");
    obniz.io8.output(false);
  }
</script>
</body>
</html>

API

Category Function/Property Minimum Support OS Class Reference
Connection new Obniz() 1.0.0 obnizcore.Obniz
obniz.onconnect 1.0.0
obniz.onclose 1.0.0
obniz.autoConnect() 1.0.0 obnizcore.Obniz
obniz.connect() 1.0.0
obniz.close() 1.0.0
obniz.onloop 1.0.0
obniz.setLoopInterval() 1.0.0
obniz.connected_network 1.0.0 obnizcore.Obniz#constructor
obniz.pingWait() 1.0.0 obnizcore.Obniz
obniz.keepWorkingAtOffline() 1.0.0
obniz.resetOnDisconnect() 1.0.0
obniz.boot_reason 7.0.0
Queue obniz.setClock() 7.0.0
obniz.setQueueMode() 7.0.0
obniz.deviceTimestamp 7.0.0
Debug obniz.debugprint 2.0.0
Hardware obniz.id 1.0.0 obnizcore.Obniz
obniz.firmware_ver 1.0.0 obnizcore.Obniz
obniz.version 1.0.0 obnizcore.Obniz
obniz.reboot() 1.0.0 obnizcore.Obniz
obniz.wait() 1.0.0 obnizcore.Obniz
obniz.sleepMinutes() 2.0.0
obniz.sleepSeconds() 2.0.0
obniz.sleepIoTrigger() 2.0.0
Components obniz.components.prepare() 7.0.0
obniz.components.powerOnInterface() 7.0.0
obniz.components.powerOffAllInterfaces() 7.0.0
obniz.components.isRS232HavingTargetWait() 7.0.0
obniz.components.startMonitoringPowerSupply() 7.0.0
obniz.components.getPowerSupplyVoltageWait() 7.0.0
obniz.components.setRelay() 7.0.0
Motion obniz.motion.start() 7.0.0
obniz.motion.onAccelerationUpdate() 7.0.0
obniz.motion.onRecognitionUpdate 7.0.0
obniz.motion.onTemperatureUpdate() 7.0.0
obniz.motion.getAccelerationWait() 7.0.0
obniz.motion.getRecognitionWait() 7.0.0
obniz.motion.getTemperatureWait() 7.0.0
Location obniz.location.start() 7.0.0 experimental
obniz.location.onupdate 7.0.0 experimental
obnizCloud obniz.metadata obnizcore.Obniz#metadata
obniz.onmessage obnizcore.Obniz#onmessage
io obniz.ioX 1.0.0 obnizcore.components.peripheralio
obniz.ioX.output() 1.0.0
obniz.ioX.inputWait() 1.0.0
obniz.ioX.input() 1.0.0
obniz.ioX.pull() 1.0.0
obniz.ioX.drive() 1.0.0
obniz.ioX.end() 1.0.0
io.animation obniz.io.animation() 1.0.0 obnizcore.components.directive
obniz.io.repeatWait() 1.0.0 obnizcore.components.directive
ad obniz.adX.start() 1.0.0 obnizcore.components.peripheralad
obniz.adX.getWait() 1.0.0 obnizcore.components.peripheralad
pwm obniz.pwmX.start() 1.0.0 obnizcore.components.peripheralpwm
obniz.pwmX.freq() 1.0.0 obnizcore.components.peripheralpwm
obniz.pwmX.duty() 1.0.0 obnizcore.components.peripheralpwm
obniz.pwmX.modulate() 1.0.0 obnizcore.components.peripheralpwm
uart obniz.uartX.start() 1.0.0 obnizcore.components.peripheraluart
obniz.uartX.send() 1.0.0 obnizcore.components.peripheraluart
obniz.uartX.setDE() 1.0.0 obnizcore.components.peripheraluart
obniz.uartX.onreceive 1.0.0 obnizcore.components.peripheraluart
spi obniz.spiX.start() 1.0.0 obnizcore.components.peripheralspi
obniz.spiX.write() 1.0.0 obnizcore.components.peripheralspi
obniz.spiX.writeWait() 1.0.0 obnizcore.components.peripheralspi
obniz.spiX.end() 1.0.0 obnizcore.components.peripheralspi
i2c obniz.i2cX.start() 1.0.0 obnizcore.components.peripherali2c
obniz.i2cX.write() 1.0.0 obnizcore.components.peripherali2c
obniz.i2cX.readWait() 1.0.0 obnizcore.components.peripherali2c
obniz.i2cX.onwritten 1.0.0 obnizcore.components.peripherali2c
canbus obniz.canbusX.start() 7.0.0 obnizcore.components.peripheralcanbus
obniz.canbusX.send() 7.0.0 obnizcore.components.peripheralcanbus
obniz.canbusX.onreceive 7.0.0 obnizcore.components.peripheralcanbus
ble obniz.ble ( See Details ) 1.0.0 obnizcore.components.ble.hci.obnizble
logicAnalyzer obniz.logicAnalyzer.start() 1.0.0 obnizcore.components.logicanalyzer
obniz.logicAnalyzer.onmeasured 1.0.0 obnizcore.components.logicanalyzer
measure obniz.measure.echo() 1.0.0 obnizcore.components.obnizmeasure
display obniz.display ( See Details ) 1.0.0 obnizcore.components.display
switch obniz.switch.getWait() 1.0.0 obnizcore.components.obnizswitch
obniz.switch.onchange 1.0.0 obnizcore.components.obnizswitch
tcp obniz.tcp ( See Details ) - obnizcore.components.tcp
wifi obniz.wifi.scanWait() 2.0.0 obnizcore.components.wifi
plugin obniz.plugin.send() 2.0.0 obnizcore.components.plugin
obniz.plugin.onreceive 2.0.0 obnizcore.components.plugin
obniz.plugin.onFrameStart 7.0.0 obnizcore.components.plugin
obniz.plugin.onFrameEnd 7.0.0 obnizcore.components.plugin
obniz.plugin.execLua() 7.0.0 obnizcore.components.plugin
obniz.plugin.reloadLua() 7.0.0 obnizcore.components.plugin
obniz.plugin_name 7.0.0 obnizcore.components.plugin
storage obniz.storage.save() 7.0.0 obnizcore.components.plugin
obniz.storage.savePluginLua() 7.0.0 obnizcore.components.plugin
obniz.storage.readWait() 7.0.0 obnizcore.components.plugin

インストール

ブラウザ

次のscriptタグをhtmlに組み込むだけです

  <script src="https://unpkg.com/obniz/obniz.js"></script>

Nodejs

npmでインストールします。

  npm install obniz

そしてjsの中でrequireして下さい。

  const Obniz = require('obniz');

接続

詳細は デバイスへの接続
obnizデバイスをobniz idを使ってインスタンス化します。
そして接続が完了した時に呼ばれる関数をセットします。

  var obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
  obniz.onconnect = async function () {

  }

接続完了後にobnizデバイスを遠隔で操作できます。

  var obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
  obniz.onconnect = async function () {
    obniz.display.print("hello!");
    obniz.switch.onchange = function(state) {
      if (state === "push") {
        obniz.display.print("Button Pressed");
      }
    }
  }
  obniz.onloop = async function () {
    // 接続中は繰り返し呼ばれる関数
  }
  obniz.onclose = async function () {
    // 切断時に呼ばれる関数
  }

IOペリフェラルも利用可能です。詳しくはそれぞれのペリフェラルドキュメントを見てください。

  var obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
  obniz.onconnect = async function () {
    obniz.io0.drive("5v");
    obniz.io0.output(true)
    obniz.io1.pull("3v");
    obniz.io1.drive("open-drain");
    obniz.io1.output(true);
    obniz.io2.drive("3v");
    obniz.io2.output(true);

    obniz.ad3.start(function(voltage){
      console.log("changed to "+voltage+" v")
    });

    var pwm = obniz.getFreePwm();
    pwm.start({io: 4});
    pwm.freq(1000);
    pwm.duty(50);

    var uart = obniz.getFreeUart();
    uart.start({tx: 5, rx: 6, baud:9600});  
    uart.onreceive = function(data, text) {
      console.log(data);
    }
    uart.send("Hello");
  }

パーツライブラリ

パーツライブラリはobniz.jsに含まれています。ドキュメントはこちらで

obniz Parts Library

obniz Boardにつながれた部品をつかうにはpartsをonconnect関数の中でインスタンス化します。どんな関数があるかなども obniz Parts Library で確認できます。

例えば LED https://obniz.com/sdk/parts/LED

  var obniz = new Obniz("0000-0000");
  obniz.onconnect = async function () {
    var led = obniz.wired("LED", {anode:0, cathode:1});
    led.blink();
  }

HC-SR40(distance measure) https://obniz.com/sdk/parts/HC-SR04

  var obniz = new Obniz("0000-0000");
  obniz.onconnect = async function () {
    var hcsr04 = obniz.wired("HC-SR04", {gnd:0, echo:1, trigger:2, vcc:3});
    hcsr04.unit("inch");
    hcsr04.measure(function( distance ){
      console.log("distance " + distance + " inch")
    })
  }

ブラウザのUIとハードウェアの連携

HTML上のUIとハードウェアの連携

<input id="slider" type="range"  min="0" max="180" />

<script src="https://unpkg.com/obniz/obniz.js"></script>
<script>
var obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
obniz.onconnect = async function () {
  var servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
  $("#slider").on('input', function() {
    servo.angle($("#slider").val())
  });
}
</script>

外部WEBサービスとの連携

DropboxやTwitterなどのwebサービスとの連携

// save data from obniz to dropbox
var obniz = new Obniz("0000-0000", { access_token: 'xxxx' });
obniz.onconnect = async function () {
  var dbx = new Dropbox({ accessToken: '<YOUR ACCESS TOKEN HERE>' });
  var button = obniz.wired("Button",  {signal:0, gnd:1});
  button.onchange = function(pressed){
    if (pressed) {
    dbx.filesUpload({path: '/obniz.txt', contents: "[Button Pressed]\n" + new Date(), mode: 'overwrite' });
    }
  };
}

2つ以上のobnizデバイスとの連携

obniz Boardにつながれたサーボモーターを別のobniz Boardにつながれたつまみから操作。

// control servomotor from potention meter which connected to another obniz.
var obnizA = new Obniz("0000-0000", { access_token: 'xxxx' });
obnizA.onconnect = async function () {
  var obnizB = new Obniz("0000-0001", { access_token: 'xxxx' });
  obnizB.onconnect = async function(){
    var meter = obnizA.wired("Potentiometer", {pin0:0, pin1:1, pin2:2});
    var servo = obnizB.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
    meter.onchange =function(position) {
      servo.angle(position * 180);
    }; 
  }
}

Articles