pxt-calliope/docs/reference/radio/on-data-packet-received.md
Sam El-Husseini e3975e65e5 pxt-microbit Accessibility PR (#529)
* Accessibility changes
2017-09-07 13:42:08 -07:00

2.2 KiB

On Data Packet Received

Run part of a program when the @boardname@ receives a number or string over radio.

radio.onDataPacketReceived(({receivedNumber, receivedString, time, serial, signal}) => { });

~hint

To add or remove the parts of the packet from the block, try clicking the blue gear in the corner!

~

Callback Parameters

  • packet - the packet that was received by the radio. The packet has the following properties:
    • receivedNumber - The number that was sent in this packet or 0 if this packet did not contain a number. See send number and send value
    • receivedString - The string that was sent in this packet or the empty string if this packet did not contain a string. See send string and send value
    • time - The system time of the @boardname@ that sent this packet at the time the packet was sent.
    • serial - The serial number of the @boardname@ that sent this packet or 0 if the @boardname@ did not include its serial number.
    • signal - How strong the radio signal is from -128 (weak) to -42 (strong).

Example

This program keeps sending numbers that says how fast the @boardname@ is slowing down or speeding up. It also receives numbers for the same thing from nearby @boardname@s. It shows these numbers as a bar graph.

basic.forever(() => {
    radio.sendNumber(input.acceleration(Dimension.X));
})
radio.onDataPacketReceived(({ receivedNumber }) => {
    led.plotBarGraph(receivedNumber, 1023);
})

Example

This program uses the signal strength from received packets to graph the approximate distance between two @boardname@s.

basic.forever(() => {
    radio.sendNumber(0)
})
radio.onDataPacketReceived(({ signal, receivedNumber }) => {
    led.plotBarGraph(
        Math.abs(signal + 42),
        128 - 42
    )
})

See also

send number, send string, send value, set group

radio