Skip to content

A javascript library to interact with BBC micro:bit and p5.js over WebUSB

License

Notifications You must be signed in to change notification settings

nkymut/microbit-webusb-p5js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microbit-webusb-p5js

A javascript library to interact with BBC micro:bit using web usb API.

Based on the library created by bsiever/microbit-webusb and updated to provide the same API for UART properties as nkymut/microbit-webble-p5js and enabled easy transition from WebUSB to WebBluetooth development interoperability.

Setup

  1. Upgrade Micro:bit to latest firmware (above version 249) ** Updating your micro:bit firmware

  2. Program the Micro:bit with one of the example programs that generates serial data

  3. Setup a Web Server & Open the project's page to run the sample application. VSCode + Live Server Extention

Constructor

  • microBit=new uBitWebUSB()

Properties

  • microBit.connected

Functions

  • microBit.connectDevice()

    Search for WebUSB devices and connect.  

    example:

    connectButton = createButton("connect");
    connectButton.mousePressed(
      function(){
        microBit.searchDevice();
      }
    );
  • microBit.onConnect(callbackFunction)

    Register a callback function invoked when the microBit connects

    example:

    microBit.onConnect(
      function(){
        console.log("connected");
      }
    );
  • microBit.onDisconnect(callbackFunction)

    Register a callback function invoked when the microBit disconnects

    example:

    microBit.onDisconnect(
      function(){
        console.log("disconnected");
      }
    );
    • microBit.onReceiveUART(callbackFunction)
    • microBit.onReceiveSerial(callbackFunction)
    • microBit.setReceiveUARTCallback(callbackFunction)

    Register a callback function to be invoked when UART data is received.

    example:

    microBit.setReceiveUARTCallback(
      function(data){
        console.log("UART received",data);
        receivedText = data;
      }
    );
  • microBit.writeUARTData(string)

    Send text line to microbit via UART.

    example:

    var messageText = "Hello!";
    microBit.writeUARTData(messageText);
  • microBit.sendSerial(string)

    Send text line to microbit via UART. an alias of writeUARTData()

    example:

    var messageText = "Hello!";
    microBit.sendSerial(messageText);

Examples

Check the examples folder for working examples.

UART Echo example

Learn how to exchange text messages between the microbit and a p5.js sketch via UART, update microBit's LED message from the sketch.

micro:bit code

Try it here

UART LightSensor example

Learn how to receive lightsensor value from the microbit and update a p5.js sketch via UART.

micro:bit code

Try it here

Neopixel Control example

Learn how to control the color of Neopixel LED connected to micro:bit from p5.js sketch via UART.

micro:bit code

Try it here

References

About

A javascript library to interact with BBC micro:bit and p5.js over WebUSB

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published