TigerJython4Kids
micro:bit Robotik
bluetooth
Deutsch   English   

7. COMMUNICATION VIA BLUETOOTH

 

 

YOU LEARN HERE...

 

how two micro:bits communicate with each other via Bluetooth.

 

 

SCENARIO OF A WIRELESS COMMUNICATION

 

Two micro:bits (also called nodes)  may exchange information by sending or receiving text messages simultaneously, so act as a transmitter and a receiver. Such a device is called a transceiver. First, both nodes invoke radio.on() to turn the transceiver on.  This will create a wireless Bluetooth connection between the devices. Subsequently, each of the nodes can send a message (as a string) with radio.send(msg) to the partner node, where the message is stored in a receiver buffer. The receiver calls radio.receive() to get the "oldest" message, which is then remove from the buffer.

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



Node A has calles send('good'):



Node A has called send('morning'):



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

 

 

EXAMPLES

 

Here you create a wireless communication link to exchange information that is coded with dots and dashes (like Morse code). If one person clicks button A on the  micro:bit, the middle LED of the partner's display lights up for ½ s (like a dot), if the person clicks button B three LEDs light up (like a dash).

 
Node A
 
Node B
 
 



 
   

The main section of the program consists of an infinite loop, where first the sending and then the receiving is treated. If A is clicked, the message "p" is transmitted (short for "point"), if B is clicked, "d" is transmitted (short for "dash").

Subsequently, you call receive() to get the oldest entry in the buffer. (If the buffer is empty, you get the special value None.) Depending on whether you received "p" or "d", you take the appropriate action.

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

Morse stations uses dots and dashes that are perceived as short and long tones. To hear the tone signal you need to connect a headphone (or an active speaker) as shown in the chapter Sound. To exchange Morse code, your program should turn on the tone at the receiver side, as long as the button A on the transmitter side is pressed and stop the tone when the button is released. To do so, you send the message "d" (for "down") when the button is pressed and "u" (for "up") when the button is released.

Use a table of the Morse code and try to exchange some short messages (for example "SOS").

Program:

import radio
from microbit import *
import music

radio.on()
down = False
while True:
    if button_a.is_pressed() and not down:
        down = True
        radio.send("d")
    elif not button_a.is_pressed() and down:
        down = False
        radio.send("u")
    rec = radio.receive()
    if rec != None:
        if rec == "d":
            music.pitch(500, 1000000, wait = False)
        elif rec == "u":
            music.stop()
    sleep(10)
► Copy to clipboard

 

 

MEMO

 

A micro:bit may act as sender and receiver at the same time. The sender sends a message with send(msg). The received message is stored in the receive buffer in sequence until the receiver fetch the "oldest" message in the buffer with msg = receive(). Such a buffer is also called a "queue" or first-in-first-out buffer because always the first ("oldest") entry is retrieved.

 

 

EXERCISES

 

 

1.

Modify the pattern program so that when A is pressed, dots are sent continuously. Same for dashes when B is pressed.


2.
a) Write a program that transmit the micro:bit's current inclination (in one direction) to a receiver node that writes it out to the terminal.

b) Write the value at the receiver's LED display (scaled to a number between 0 and 9).
 

 

7-1
Professional hints:

7-2
Professional hints:
In this type of computer communication both nodes are equal. However, usually the communication takes place in accordance with the client-server principle, in which the server node first waits for a call of a client node and then decides, if it wants to communicate with it.
7-3
Professional hints:
It is a so-called "automatic Morsetaste", as it is widely used as Elbug at Morsestationen.
5-1
Professional hints:

Some sound frequencies:

Tone Frequency Tone Frequency    

h' 494 h'' 988    
a' 440 a'' 880    
g' 392 g'' 784    
f' 349 f'' 698    
e' 330 e'' 660    
d' 294 d'' 588    
c' 262 c'' 524 c''' 1048