2016-10-25 01:30:21 +02:00
# On Data Packet Received
2016-11-02 01:44:37 +01:00
Run part of a program when the @boardname @ receives a
2017-03-16 15:57:41 +01:00
[number ](/types/number ) or [string ](/types/string ) over ``radio``.
2016-10-25 01:30:21 +02:00
```sig
2016-10-26 01:39:13 +02:00
radio.onDataPacketReceived(({receivedNumber, receivedString, time, serial, signal}) => { });
2016-10-25 01:30:21 +02:00
```
2017-09-07 22:42:08 +02:00
## ~hint
2016-10-26 01:39:13 +02:00
To add or remove the parts of the packet from the block, try clicking the blue gear in the corner!
2017-09-07 22:42:08 +02:00
## ~
2016-10-26 01:39:13 +02:00
2017-09-07 22:42:08 +02:00
## Callback Parameters
2016-10-25 01:30:21 +02:00
2016-10-26 01:39:13 +02:00
* ``packet`` - the [packet ](/reference/radio/packet ) that was received by the radio. The packet has the following properties:
2017-03-16 15:57:41 +01:00
* `receivedNumber` - The [number ](/types/number ) that was sent in this packet or `0` if this packet did not contain a number. See [send number ](/reference/radio/send-number ) and [send value ](/reference/radio/send-value )
* `receivedString` - The [string ](/types/string ) that was sent in this packet or the empty string if this packet did not contain a string. See [send string ](/reference/radio/send-string ) and [send value ](/reference/radio/send-value )
2016-11-02 01:44:37 +01:00
* `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.
2017-01-28 20:53:36 +01:00
* `signal` - How strong the radio signal is from `-128` (weak) to `-42` (strong).
2016-10-25 01:30:21 +02:00
2017-09-07 22:42:08 +02:00
## Example
2016-10-25 01:30:21 +02:00
2016-11-02 01:44:37 +01:00
This program keeps sending numbers that says how fast the @boardname @ is
2016-10-25 01:30:21 +02:00
slowing down or speeding up. It also receives numbers for the same
2016-11-02 01:44:37 +01:00
thing from nearby @boardname@s . It shows these numbers as a
2016-10-25 01:30:21 +02:00
[bar graph ](/reference/led/plot-bar-graph ).
```blocks
basic.forever(() => {
radio.sendNumber(input.acceleration(Dimension.X));
})
2018-10-16 00:32:09 +02:00
radio.onReceivedNumber(function (receivedNumber) {
2016-10-25 01:30:21 +02:00
led.plotBarGraph(receivedNumber, 1023);
})
```
2017-09-07 22:42:08 +02:00
## Example
2017-01-28 20:53:36 +01:00
This program uses the signal strength from received packets to graph the
approximate distance between two @boardname@s .
```blocks
basic.forever(() => {
radio.sendNumber(0)
})
radio.onDataPacketReceived(({ signal, receivedNumber }) => {
led.plotBarGraph(
Math.abs(signal + 42),
128 - 42
)
})
```
2018-03-08 22:23:18 +01:00
## Troubleshooting
The on radio data event can only be created once, due to the hardware restrictions.
The radio set group might need to be set, synchronized , before the radio events will function.
2017-09-07 22:42:08 +02:00
## See also
2016-10-25 01:30:21 +02:00
[send number ](/reference/radio/send-number ),
[send string ](/reference/radio/send-string ),
[send value ](/reference/radio/send-value ),
[set group ](/reference/radio/set-group )
```package
radio
2018-03-08 22:23:18 +01:00
```