Leggi la documentazione e fammi 2 brick che comunicano, uno legge il colore, l'altro…
- User
Leggi la documentazione e fammi 2 brick che comunicano, uno legge il colore, l'altro cambia colore del pulsante in base al colore letto dall'altro hub
Skip to primary navigation Skip to content Skip to footer Pybricks Pybricks Code Get Started Documentation Projects Community Pricing Toggle search Hub to hub communication using data broadcasting Improve project Hub to hub communication using data broadcasting This project shows how you can exchange data between two Pybricks hubs.
Play Data broadcastingPermalink When a Bluetooth device is on but not yet connected to anything, it typically broadcasts (advertises) some information about itself. This tells you what it can do before you connect.
For example, Bluetooth earphones will advertise that they can play music, so that your phone knows what to look for when it scans for speaker devices.
By constantly changing what information the hub advertises, we can broadcast small amounts of information to nearby devices that are scanning for this information.
This means that any number of nearby hubs can receive the broadcasted message without having to set up a connection. This can be quite convenient, but you can only broadcast small amounts of information at once, as explained below.
Channels
In Pybricks, all broadcasting data is assigned a channel. This helps you tell data apart when multiple hubs are broadcasting at the same time. For example, one hub might be broadcasting tilt sensor data on channel 1 while another hub broadcasts measurements on channel 2.
To prepare the hub to send and receive data with these channels, you initialize the hub class as follows:
hub = ThisHub(broadcast_channel=1) hub = ThisHub(observe_channels=[1, 2]) Sending and receiving data
You can send data like this:
data = sensor.tilt()
Send it out to anyone listening.
hub.ble.broadcast(data) On another hub, you can receive it as follows:
data = hub.ble.observe(1)
Check if there was any data yet:
if data is not None: # There was, so let's print it. pitch, roll = data print(pitch, roll) When sending data, your values will be automatically encoded into a format suitable for broadcasting. You can send integers (1, -5, …), floats (1.0 -5.3, …), booleans (True, False), strings ("example"), bytes (b"\x00\x02") or tuples thereof.
Running the example programsPermalink The following examples shows the broadcasting feature in action to provide wireless bidirectional communication.
One hub sends tilt sensor data to control a driving vehicle. The vehicle sends back a distance measurement to show a warning light for nearby obstacles:
Run this program on the sender
You can use any hub with any type of sensor. For example, you could use the built-in tilt sensor of the Technic Hub, Prime Hub, Essential Hub, or the Inventor Hub. Alternatively, you could build your own remote control that uses motors as input dials.
- Assistant
- User
- Assistant
Per implementare la comunicazione tra due hubs LEGO per trasmettere un colore da un hub a un altro usando Pybricks, dobbiamo scrivere due programmi distinti: uno per leggere il colore con un Color Sensor e l'altro per ricevere questo colore e cambiarne la luce di un pulsante LED dell'hub.
- User
Traceback (most recent call last): File "LetturaColore.py", line 17, in TypeError: must be True, False, int, float, str or bytes
- Assistant