gobot.io/x/gobot@v1.16.0/platforms/microbit/README.md (about) 1 # Microbit 2 3 The [Microbit](http://microbit.org/) is a tiny computer with built-in Bluetooth LE aka Bluetooth 4.0. 4 5 ## How to Install 6 ``` 7 go get -d -u gobot.io/x/gobot/... 8 ``` 9 10 You must install the Microbit firmware from [@sandeepmistry] located at [https://github.com/sandeepmistry/node-bbc-microbit](https://github.com/sandeepmistry/node-bbc-microbit) to use the Microbit with Gobot. This firmware is based on the micro:bit template, but with a few changes. 11 12 If you have the [Gort](https://gort.io) command line tool installed, you can install the firmware using the following commands: 13 14 ``` 15 gort microbit download 16 gort microbit install /media/mysystem/MICROBIT 17 ``` 18 19 Substitute the proper location to your Microbit for `/media/mysystem/MICROBIT` in the previous command. 20 21 Once the firmware is installed, make sure your rotate your Microbit in a circle to calibrate the magnetometer before your try to connect to it using Gobot, or it will not respond. 22 23 You can also follow the firmware installation instructions at [https://github.com/sandeepmistry/node-bbc-microbit#flashing-microbit-firmware](https://github.com/sandeepmistry/node-bbc-microbit#flashing-microbit-firmware). 24 25 The source code for the firmware is located at [https://github.com/sandeepmistry/node-bbc-microbit-firmware](https://github.com/sandeepmistry/node-bbc-microbit-firmware) however you do not need this source code to install the firmware using the installation instructions. 26 27 ## How to Use 28 29 The Gobot platform for the Microbit includes several different drivers, each one corresponding to a different capability: 30 31 - AccelerometerDriver 32 - ButtonDriver 33 - IOPinDriver 34 - LEDDriver 35 - MagnetometerDriver 36 - TemperatureDriver 37 38 The following example uses the LEDDriver: 39 40 ```go 41 package main 42 43 import ( 44 "os" 45 "time" 46 47 "gobot.io/x/gobot" 48 "gobot.io/x/gobot/platforms/ble" 49 "gobot.io/x/gobot/platforms/microbit" 50 ) 51 52 func main() { 53 bleAdaptor := ble.NewClientAdaptor(os.Args[1]) 54 ubit := microbit.NewLEDDriver(bleAdaptor) 55 56 work := func() { 57 ubit.Blank() 58 gobot.After(1*time.Second, func() { 59 ubit.WriteText("Hello") 60 }) 61 gobot.After(7*time.Second, func() { 62 ubit.Smile() 63 }) 64 } 65 66 robot := gobot.NewRobot("blinkBot", 67 []gobot.Connection{bleAdaptor}, 68 []gobot.Device{ubit}, 69 work, 70 ) 71 72 robot.Start() 73 } 74 ``` 75 76 ### Using Microbit with GPIO and AIO Drivers 77 78 The IOPinDriver is a special kind of Driver. It supports the DigitalReader, DigitalWriter, and AnalogReader interfaces. 79 80 This means you can use it with any gpio or aio Driver. In this example, we are using the normal `gpio.ButtonDriver` and `gpio.LedDriver`: 81 82 ```go 83 package main 84 85 import ( 86 "os" 87 88 "gobot.io/x/gobot" 89 "gobot.io/x/gobot/drivers/gpio" 90 "gobot.io/x/gobot/platforms/ble" 91 "gobot.io/x/gobot/platforms/microbit" 92 ) 93 94 func main() { 95 bleAdaptor := ble.NewClientAdaptor(os.Args[1]) 96 97 ubit := microbit.NewIOPinDriver(bleAdaptor) 98 button := gpio.NewButtonDriver(ubit, "0") 99 led := gpio.NewLedDriver(ubit, "1") 100 101 work := func() { 102 button.On(gpio.ButtonPush, func(data interface{}) { 103 led.On() 104 }) 105 button.On(gpio.ButtonRelease, func(data interface{}) { 106 led.Off() 107 }) 108 } 109 110 robot := gobot.NewRobot("buttonBot", 111 []gobot.Connection{bleAdaptor}, 112 []gobot.Device{ubit, button, led}, 113 work, 114 ) 115 116 robot.Start() 117 } 118 ``` 119 120 ## How to Connect 121 122 The Microbit is a Bluetooth LE device. 123 124 You need to know the BLE ID of the Microbit that you want to connect to. 125 126 ### OSX 127 128 If you connect by name, then you do not need to worry about the Bluetooth LE ID. However, if you want to connect by ID, OS X uses its own Bluetooth ID system which is different from the IDs used on Linux. The code calls thru the XPC interfaces provided by OSX, so as a result does not need to run under sudo. 129 130 For example: 131 132 go run examples/microbit_led.go "BBC micro:bit" 133 134 OSX uses its own Bluetooth ID system which is different from the IDs used on Linux. The code calls thru the XPC interfaces provided by OSX, so as a result does not need to run under sudo. 135 136 ### Ubuntu 137 138 On Linux the BLE code will need to run as a root user account. The easiest way to accomplish this is probably to use `go build` to build your program, and then to run the requesting executable using `sudo`. 139 140 For example: 141 142 go build examples/microbit_led.go 143 sudo ./microbit_led "BBC micro:bit" 144 145 ### Windows 146 147 Hopefully coming soon...