pxt-calliope/docs/reference/radio/receive-string.md
Juri Wolf 5f7a8e5301
Updates for V4 (#197)
* update yotta defaults for 16kb devices

* refactor deprecated blocks

* updates for button events

* update button events

* update refference

* update docs

* update docs

* update button event blocks

* update docs

* update block id
2022-08-10 09:36:19 -07:00

2.1 KiB

receive String

Find the next string sent by radio from another @boardname@.

radio.receiveString()

~ hint

Deprecated

This API has been deprecated! Use on received string instead.

~

Returns

  • the first string that was sent. If no string was sent, then this function returns an empty (blank) string.

Example: Simple receiver

Show the string sent by another @boardname@.

radio.onDataReceived(() => {
    basic.showString(radio.receiveString());
});

Example: Two-way radio

If you load this program onto two or more @boardname@s, you can send a code word from one of them to the others by pressing button A. The other @boardname@s will receive the code word and then show it.

input.onButtonEvent(Button.A, input.buttonEventClick(), () => {
    radio.sendString("Codeword: TRIMARAN")
    basic.showString("SENT");
})

radio.onDataReceived(() => {
    basic.showString(radio.receiveString());
});

~hint

A radio that can both transmit and receive is called a transceiver.

~

Example: Mood radio

This is a simple program to send whether you are happy or sad over radio. Use the A or B button to select an emotion.

This program will also receive your friend's mood.

let data: string = "";
input.onButtonEvent(Button.A, input.buttonEventClick(), () => {
    radio.sendString("H");
});
input.onButtonEvent(Button.B, input.buttonEventClick(), () => {
    radio.sendString("S");
});
radio.onDataReceived(() => {
    data = radio.receiveString();
    if ("H" == data) {
        basic.showLeds(`
            . . . . .
            . # . # .
            . . . . .
            # . . . #
            . # # # .
            `);
    } else if ("S" == data) {
        basic.showLeds(`
            . . . . .
            . # . # .
            . . . . .
            . # # # .
            # . . . #
            `);
    } else {
        basic.showString("?");
    }
});

See also

send string, on data received

radio