LoginSignup
5
10

More than 5 years have passed since last update.

micro:bitがobniz経由でInternetにつながった

Last updated at Posted at 2018-10-24

概要

micro:bitは色々なセンサー(2個のボタン、加速度センサー、磁力センサー、明るさセンサー)を持っているがInternetに繋がっていないためIoTと呼ぶのはいまいちな感じがしたので、obnizを使用してInternetにつなげてみました。

子供学習の際にも、簡単にmicro:bitをInternetにつなげることが出来るので活用の幅が広がると思います。

一方obnizも2000円程の費用で色々なセンサーを使うことが出来るので、これも活用の幅が広がりそうです。

趣味でスピーカーハック(タイマー、検査機等のスピーカーに繋げて「音が鳴る」=トリガーとして利用する)をするのですが、
機材→micro:bit→obniz
とmicro:bitを挟んだ方が過電圧等での破壊の被害コストが少ない(壊れるのは2000円のmicro:bitだけ。obnizは6000円なので壊したくない)ので、リスク低減にも役立ちます。

デモ

配線

micro:bit obniz
P0 P1 rx
P1 P0 tx
GND P3 gnd

プログラム

obniz

helloMicrobit.html
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@1.13.0/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

<div id="obniz-debug"></div>
<h1>micro:bit IF</h1>
<div id="print"></div>



<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function(){
  var uart = obniz.getFreeUart();
  uart.start({tx: 0, rx: 1, gnd:2 });

  uart.send("Hello")

  while(1){
    if(uart.isDataExists()){
        console.log(uart.readText());
    }
    await obniz.wait(10);
  }
}
</script>
</body>
</html>

参考にしたサイト:UART: 受信する←obniz本家のサンプル

micro:bit(JavaScript表示版)

obniz2microbit.js
input.onButtonPressed(Button.B, () => {
    serial.writeString("NG")
    basic.showString("NG")
})
input.onButtonPressed(Button.A, () => {
    serial.writeString("OK")
    basic.showString("OK")
})
input.onButtonPressed(Button.AB, () => {
    serial.writeNumber(input.lightLevel())
    basic.showNumber(input.lightLevel())
})
serial.redirect(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate115200
)
basic.forever(() => {

})

参考にしたサイト:マイクロビットの端子を使おう(シリアル通信)

その他参考サイト

・micro:bitの端子の説明

・クリエイター手抜きプロジェクト[555]IoT obniz編 シリアル通信でデータを送信する

obniz.uart0.start({tx:1, rx:0, gnd:2});

でGNDにもつなげないといけない事に気付く

5
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
10