-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Broadcast example for other devices #800
Comments
This is definitely a possibility. Both sending and receiving. The protocol implemented follows the format used by the hub-to-hub communication blocks in the official MINDSTORMS app. This means each message is broadcasted by setting the advertisement data as:
Receiving should indeed be trivial. For sending you need to be able to set the full advertisement message. This should be possible for an ESP32. |
Thanks for the detailed description of the advertisement data. I will try to use it and will share the example code when I succeed. |
@NStrijbosch I am not able to get transmitted data on my computer. I am able to scan the device but all the libraries I used do not report any manufacturer data. One example with bleak: import asyncio
from bleak import BleakScanner
async def main():
devices = await BleakScanner.discover(timeout=3,return_adv=True)
for d in devices:
_ , adv = devices[d]
print(adv)
asyncio.run(main()) For my technic hub with the custom firmware and code sending pitch and roll on
It doesn't contain manufacturer data at all but I see x97 x03 sequence in the service data. I tried to use different library (https://github.com/abandonware/noble/blob/master/examples/advertisement-discovery.js), but the effect is similar: {
"localName": "legoble",
"serviceData": [
{
"uuid": "2a50",
"data": {
"type": "Buffer",
"data": [
1,
151,
3,
128,
0,
0,
0
]
}
}
]
} I know the sender code works because I have the second hub as a receiver and it gets the messages. Is it possible that the data is not structured properly and cannot be decoded on my computer? Manufacturer data from other devices are visible (I started experiments with ESP32). Have you tried to receive the data on other devices? |
The times I used other devices to receive the data I had access to the raw advertisement data, e.g., using https://docs.micropython.org/en/latest/library/bluetooth.html I did the decoding to obtain the data, topic, index myself. I was of the impression the advertisment data followed the normal conventions. But maybe it is not so conventional to only send manufacturing data... Anyhow, given that you recognize the company identifier there must be something. What is your topic name? |
The topic name is pitch, roll = hub.imu.tilt()
radio.send("tilt", (pitch,roll)) Full code is here: https://gist.github.com/pbochynski/c8336c885251c77ed9fbf8e75cd4defc I tried your advice and I send raw data:
Anyway, whatever I send, whatever the topic is the received looks the same:
The only difference is that now when I send static data in the loop (always the same value 1234) the advertisement data doesn't have service_uuids array. It looks like my message and topic are not popping up through the libraries on my MacBook. |
Clear. Indeed somewhere in the decoding of the advertising data is going wrong. I am actually surprised that 'legoble' is there, it is not something that is explicitly advertised. Moreover, I don't recognize the crc32 hash in any of the data. I think the only solution would be to search for a way to read the raw advertising data before the decoding in service data etc. Just some experience I have with ble advertising with apple products: I have been in conversation with someone who has been able to send and receive data in this format from an ESP32. So it could be worth trying if you can read the advertising data from a pybricks hub on your ESP32. |
I tried on ESP32 with this BLE scanner code: The result is a little bit better:
But the message should be longer as the sender code is like this: data = bytes("abcdefghijklmn","ascii")
print(data)
while True:
radio.send_bytes("tilt", data)
wait(1000) The topic name encoded would be So I did another experiment and when I send shorter message: data = bytes("abc","ascii")
radio.send_bytes("tilt", data) the scanner reports:
Look! Also 9 characters missing (3 from string and 6 from message meta data).
|
I managed to send the data with ESP32 code in the format @NStrijbosch described. I am also able to decode that data on my computer (all data - not truncated). But what is important broadcast implementation for pybrics can read my signal from ESP! I will share the code soon - it requires a little bit of refactoring before publishing. |
I have a prototype for micropython module for ESP32 that implements Broadcast (send and receive). |
Quite possibly, yes. We're still finding out the best ways to add projects to the site, though. To save yourself some time, perhaps you could start off posting your proposed content as an issue instead of a pull request. Once we know where everything will go, you can always just use the same markdown content for your pull request. |
To be honest, PR is easier than maintaining the code in the issue. I need 2-3 python code files plus a markdown description, maybe some images also. From my side, a PR is not a problem, and it is also not a problem to refactor/move it somewhere else later. |
@pbochynski I'd be very interested in perusing your code—were you able to make it public? |
We now have a library and tutorial for this here: https://pybricks.com/project/micropython-ble-communication/ Thanks for opening this issue! |
With broadcast implementation, you opened a lot more possibilities than described in the initial comment on this issue. The publish-subscribe model is much better for integrating many hubs than a direct connection. I tried already simple scenario of sharing sensor data between hubs, and now I have a few other ideas, but wanted first to double check with you if it makes sense and how hybrids project can support it.
Broadcast.send
on my PC using the bleak library you introduced in pc-communication example. I now analyze the code in the PR to decode the data, but as both BLE and micropython code are new to me it will take some time ;). Maybe you already have some code snippet that you can share? Something like python/bleak implementation of Broadcastreceive
.What do you think?
The text was updated successfully, but these errors were encountered: