bluetooth
Deutsch   English   

7. BLUETOOTH COMMUNICATION

 

 

YOU LEARN HERE...

 

how two computers exchange information with each other via Bluetooth.

 

 

WIRELESS COMMUNICATION

 

In communication between two micro:bits (also called nodes), each node is both a transmitter and a receiver. First, both nodes switch on the sender/receiver device with radio.on(). This establishes a wireless Bluetooth connection. Subsequently, each of the nodes can send a message (as a string) to the other node with radio.send(msg), which is first stored there in a receive buffer like in a queue. With radio.receive() you fetch the “oldest” message, which is then deleted from the buffer.

Node A and node B have called radio.on():



Node A has called send('good'):



Node A has called send('day'):



Node B has called msg = receive(). msg contains 'good':

 

 

EXAMPLES

 

You create a wireless communication with another person (with a second micro:bit) to exchange information with dots and dashes (like in Morse code). If you click button A on one micro:bit, the middle LED of the display on the other micro:bit lights up for ½ second (like a dot), if you click button B, three LEDs light up (like a dash).

 
Node A
 
Node B
 
 



 
   

The central part of the program consists of an infinite loop in which first sending and then receiving is controlled. When A is clicked, you transmit the message “p” (short for “point”), when B is clicked, you transmit “d” (short for “dash”).

Then use receive() to get the oldest entry in the queue. (If this is empty, you get the special value None.) Depending on whether you have received “p” or “d”, you execute the corresponding action.

The following program must run on both micro:bits.

Program:

import radio
from microbit import *

point = Image('00000:00000:00900:00000:00000')
dash = Image('00000:00000:09990:00000:00000')

def draw(img):
    display.show(img)
    sleep(500)
    display.clear()
    sleep(500)

radio.on()
while True:
    if button_a.was_pressed():
        radio.send("p")
    elif button_b.was_pressed():
        radio.send("d")
    rec = radio.receive()
    if rec != None:
        if rec == "p":
            draw(point)
        elif rec == "d":
            draw(dash)
    sleep(10)
►Copy to clipboard

Real Morse code stations almost always work with short and long tones. To do this, you must connect headphones (or an active loudspeaker as shown in the Sound chapter). Your program should now switch on the sound at the receiver as long as button A is pressed. As soon as the button is pressed, you send the message “d” (for “down”) to the receiver; when you release the button, you send “u” (for “up”). To do this, you must introduce a Boolean variable down, where you remember whether the button is pressed or released.

Use the Morse code to orient yourself and try to exchange a few short messages (e.g. “SOS”).

 

 

REMEMBER YOU...

 

that the sender sends a message with send(msg), which is then stored in the receive buffer in sequence until the receiver picks up the “oldest” message in the buffer with msg = receive().

 

 

TO SOLVE BY YOURSELF

 

 

1.

Modify the pattern program so that dots are sent continuously when button A is pressed and dashes (3x as long as dots) are sent when button B is pressed.


2.
a) Write a program that communicates the current inclination (in one direction) of the micro:bit to a receiver node that writes out the value in the terminal.

b) Write out the value on the receiver's display (scaled to a number between 0 and 9)