View on GitHub

microbit-radio-send-object

MakeCode/Micro:bit Radio Extension to Send Objects

Open this page at https://nix0n.github.io/microbit-radio-send-object/

Micro:bit Radio Send Object Extension

This extension extends the built-in radio namespace by providing new functions like radio.sendObject() and radio.onReceivedObject().

The radio.sendObject() function depends on the built-in radio.sendString() function to send a JSON encoding of the passed value. Micro:bit’s built-in radio.sendString() has a limit of 19 characters. To work around this, radio.sendObject() uses a combination of special control characters (STX, ETX) and maximally sized chunks sent by radio.sendString(); which allows for near limitless object size.

Notice: It’s recommended that you enable serial number transmission, using radio.setTransmitSerialNumber(true). This allows for multiple devices simultaneously sending streams. It also provides loopback detection in the rare instance the receive event is on the same device it was transmitted.

Use as Extension

This repository can be added as an extension in MakeCode.

Import this project

To Import this repository in MakeCode.

Usage

radio.sendObject(value)

Send object over radio

PARAMETERS

RETURNS

void

EXAMPLE

// Notice: JSON encoding longer than 19 characters
radio.sendObject({ foo: 'bar', baz: true })

radio.onReceivedObject(cb)

Register event handler to listen for received object

PARAMETERS

RETURNS

void

EXAMPLE

radio.onReceivedObject(function (receivedObject: any, props: number[]) {
    console.log(`Object was sent by: ${props[RadioPacketProperty.SerialNumber]}`)
    console.log(`Object received: ${JSON.stringify(receivedObject)}`)
})

Other

License

This code is made available under the the MIT License.

Metadata (used for search, rendering)