Go言語 + GOBOT + micro:bit で micro:bitのLED-Matrixに”hello world”のメッセージを流します。

  1. 概要
    1. Go言語 + GOBOT がBLE経由でmicro:bitを入出力デバイスとして利用できると知りRaspberry Pi配下で動かしてみました。
    2. 引数として渡すBLEデバイス名を誤っていてBLEがリンクせずダメでしたが,正しいデバイス名を渡すと簡単に動きました。
  2. 実験環境
    1. Rasberry Pi (Raspbian stretch)
    2. Go言語(ver. 1.9.2 linux/arm)
    3. GOBOTフレームワーク
    4. GORT(ver. 0.9.0)
    5. micro:bit
  3. セットアップ
    1. Go言語のインストール→「RasPiでGo言語を動かす」参照
      【重要】必ずGoコンパイラ /usr/local/go/bin/go にSUIDを付与しておきます。
    2. GOBOTのインストール→「RaPiでGo言語を動かす(Go言語でLチカ)参照
    3. GORTのインストール→「RasPi + Arduino + Go言語でLチカする(GORT)」参照
    4. micro:bitを入出力デバイス化するファームウェアの書き込み。
      micro:bitをRasPiにUSB接続してリムーバブルディスクとしてマウントし以下の操作
      
      $ gort microbit download
      $ gort microbit install /media/pi/MICROBIT
  4. サンプル・プログラム
    1. ソース(microbit-hello.go)
      package main
      
      import (
              "os"
              "time"
      	"fmt"
      
              "gobot.io/x/gobot"
              "gobot.io/x/gobot/platforms/ble"
              "gobot.io/x/gobot/platforms/microbit"
      )
      
      func main() {
      	Microbit := "BBC micro:bit ["+os.Args[1]+"]"
      	fmt.Println(Microbit)
              bleAdaptor := ble.NewClientAdaptor(Microbit)
              ubit := microbit.NewLEDDriver(bleAdaptor)
      
              work := func() {
                      ubit.Blank()
                      gobot.After(1*time.Second, func() {
                              ubit.WriteText("Hello world")
                      })
                      gobot.After(11*time.Second, func() {
                              ubit.Smile()
                      })
              }
      
              robot := gobot.NewRobot("blinkBot",
                      []gobot.Connection{bleAdaptor},
                      []gobot.Device{ubit},
                      work,
              )
      
              robot.Start()
      }
    2. 実行
      BLEデバイス名を見る
      $ gort scan ble
      LE Scan ...
      E8:3F:12:9D:D5:3E BBC micro:bit [voyage]
      4E:4B:6E:DC:26:36 (unknown)
      4E:4B:6E:DC:26:36 (unknown)
      E8:3F:12:9D:D5:3E (unknown)
      
      実行
      $ go run microbit_hello.go voyage    
      BBC micro:bit [voyage]
      2018/01/27 17:58:13 Initializing connections...
      2018/01/27 17:58:13 Initializing connection BLEClient-39C7D67A ...
      2018/01/27 17:58:13 Initializing devices...
      2018/01/27 17:58:13 Initializing device Microbit LED-466A41E1 ...
      2018/01/27 17:58:13 Robot blinkBot initialized.
      2018/01/27 17:58:13 Starting Robot blinkBot ...
      2018/01/27 17:58:13 Starting connections...
      2018/01/27 17:58:13 Starting connection BLEClient-39C7D67A...
      2018/01/27 17:58:15 Starting devices...
      2018/01/27 17:58:15 Starting device Microbit LED-466A41E1...
      2018/01/27 17:58:15 Starting work...
      
      ビルドして実行
      $ go build microbit_hello.go
      $ sudo ./microbit_hello voyage
      
      sudoで実行したくないとき
      $ sudo chown root:root microbit_hello
      $ sudo chmod +s microbit_hello
      以後は
      $./microbit_hello voyage
    3. GOBOT付属サンプルプログラム
      ~/go/src/gobot.io/x/gobot/examples/ にGOBOTのサンプルがある。
      これを動かす時のBLEデバイス名は次の赤文字部分を"で囲って引数として渡す。
      $ gort scan ble
      LE Scan ...
      E8:3F:12:9D:D5:3E BBC micro:bit [voyage]
      4E:4B:6E:DC:26:36 (unknown)
      4E:4B:6E:DC:26:36 (unknown)
      E8:3F:12:9D:D5:3E (unknown)
      
      $ go run microbit_led.go "BBC micro:bit [voyage]"

       

Follow me!