@@ -17,6 +17,11 @@ Hub to hub communication is still a work in progress. This page explains how
1717you can install an experimental release on the hub to try it out. This
1818functionality may change in the future and some things may not work.
1919
20+ Last changed on 2023-05-16. Be sure to use [ Pybricks Beta] and the most recent
21+ firmware.
22+
23+ [ Pybricks Beta ] : https://beta.pybricks.com
24+
2025# Data broadcasting
2126
2227When a Bluetooth device is on but not yet connected to anything, it typically
@@ -34,106 +39,55 @@ This means that any number of nearby hubs can receive the broadcasted message
3439without having to set up a connection. This can be quite convenient, but you
3540can only broadcast small amounts of information at once, as explained below.
3641
37- ** Topics **
42+ ** Channels **
3843
39- All broadcasting data is labeled with a _ topic _ . This helps you tell data
40- apart when multiple hubs are broadcasting at the same time. For example, one
41- hub might be broadcasting tilt sensor data with the topic `` "tilt" `` while
42- another hub broadcasts measurements with the topic `` "distance" `` .
44+ In Pybricks, all broadcasting data is assigned a * channel * . This helps you tell
45+ data apart when multiple hubs are broadcasting at the same time. For example,
46+ one hub might be broadcasting tilt sensor data on channel 1 while another hub
47+ broadcasts measurements on channel 2 .
4348
44- To prepare the hub to send and receive data with these topics , you initialize
45- the broadcast class as follows:
49+ To prepare the hub to send and receive data with these channels , you initialize
50+ the hub class as follows:
4651
4752``` python
48- # Import the experimental Broadcast feature.
49- from pybricks.experimental import Broadcast
53+ # Prepare the hub for sending.
54+ hub = ThisHub(broadcast_channel = 1 )
55+ ```
5056
51- # Prepare the hub for sending and/or receiving these topics.
52- radio = Broadcast(topics = [" tilt" , " distance" ])
57+ ``` python
58+ # Prepare the hub for receiving.
59+ hub = ThisHub(observe_channels = [1 , 2 ])
5360```
5461
55- ** Sending and receiving data tuples **
62+ ** Sending and receiving data**
5663
57- You can send data as a tuple of values :
64+ You can send data like this :
5865
5966``` python
60- # Get some example data as a tuple.
61- example_data = (123 , 456 )
67+ roll, pitch = sensor.tilt()
6268
6369# Send it out to anyone listening.
64- radio.send( " tilt " , example_data )
70+ hub.ble.broadcast(roll, pitch )
6571```
6672
6773On another hub, you can receive it as follows:
6874
6975``` python
7076# Try to read previously received tilt data.
71- data = radio.receive( " tilt " )
77+ data = hub.ble.observe( 1 )
7278
7379# Check if there was any data yet:
74- if data:
80+ if data is not None :
7581 # There was, so let's print it.
7682 pitch, roll = data
7783 print (pitch, roll)
78-
79- ```
80-
81- When sending data tuples like these, your values will be automatically encoded
82- into a format suitable for broadcasting.
83-
84- The following data tuples are allowed:
85-
86- ``` python
87- # You can send up to 8 small values in the range +/- 32767.
88- # Each value counts as 2 bytes.
89- data = (- 1 , 2 , 3000 , 4 , 5 , 6 , - 32767 , 32767 )
90-
91- # You can send up to 5 big values. Each value counts as 4 bytes.
92- data = (50000 , - 50000 , 123456 , 78910 , 43210 )
93-
94- # You can send up to 5 floating point values. Each counts as 4 bytes.
95- data = (1.234 , 3.1428 )
96-
97- # You can send up to 8 strings. Each character counts as 1 byte.
98- data = (" Hello" , " World" )
99-
100- # You can combine up to eight of the above types.
101- # The total size must be 20 bytes or less.
102- data = (123 , - 50000 , 3.1428 , " Hi!" )
103-
10484```
10585
106- ** Sending and receiving raw data**
107-
108- If you prefer to encode data yourself, you can send and receive bytes directly:
86+ When sending data, your values will be automatically encoded into a format
87+ suitable for broadcasting. You can send integers (` 1 ` , ` -5 ` , ...), floats
88+ (` 1.0 ` ` -5.3 ` , ...), booleans (` True ` , ` False ` ), strings (` "example" ` ), bytes
89+ (` b"\x00\x02" ` ) and ` None ` .
10990
110- ``` python
111-
112- # Send out up to 23 bytes to anyone listening.
113- radio.send_bytes(" tilt" , b " byte data!" )
114-
115- # Read up to 23 bytes.
116- data = radio.receive_bytes(" tilt" )
117- ```
118-
119- # Installing the experimental firmware
120-
121- To use the broadcasting feature, you have to install a special version of the
122- Pybricks firmware that includes the `` Broadcast `` class:
123-
124- 1 . Download the firmware file for your hub:
125- - [ Technic Hub] ( ./technichub-firmware-build-2178.zip )
126- - [ City Hub] ( ./cityhub-firmware-build-2178.zip )
127- - [ Essential Hub] ( ./essentialhub-firmware-build-2178.zip )
128- - [ Inventor Hub and Prime Hub] ( ./primehub-firmware-build-2178.zip )
129- 2 . In [ Pybricks Beta] ( https://beta.pybricks.com/ ) , open the settings menu.
130- 3 . Click `` Install Pybricks Firmware `` .
131- 4 . Instead of selecting your hub, choose `` Advanced `` at the bottom.
132- 5 . Follow the instructions to select the firmware file you just downloaded.
133- 6 . The installation now proceeds as usual. Make sure to choose a descriptive
134- hub name. This makes it easy to distinguish them later.
135- 7 . Start coding. You can open [ Pybricks Beta] ( https://beta.pybricks.com/ ) in
136- multiple different tabs. Use one tab for each hub.
13791
13892# Running the example programs
13993
@@ -168,13 +122,17 @@ sensor, you can delete the lines that make use of the Ultrasonic Sensor.
168122{% include_relative vehicle.py % }
169123```
170124
171- # Known issues : Slow communication while connected to computer
125+ # Known issue : Slow communication while connected to computer
172126
173127If the hub is still connected to the computer, the bluetooth chip is quite busy
174128and data broadcasting may be slow. Especially on the Technic Hub and the City
175129Hub.
176130
177- This is something we are still working on. To work around it, just load the
178- program onto the hub and disconnect from your computer. You can just restart
179- the program with the hub button.
131+ To work around it, just load the program onto the hub and disconnect from your
132+ computer. You can just restart the program with the hub button.
133+
134+ # Known issue: Broadcasting using the City hub does not work
180135
136+ There is a bug in the Bluetooth chip firmware that prevents broadcasting
137+ from working properly on the City hub. It was working in previous experimental
138+ implementations because we were broadcasting data the "wrong" way.
0 commit comments