jeflem / spremote

Remotely control your LEGO SPIKE Prime machines
GNU General Public License v3.0
2 stars 0 forks source link

Bluetooth ? #1

Open Matejvvo opened 4 weeks ago

Matejvvo commented 4 weeks ago

Hi, I am trying to figure out how to control spike wirelessly for example from a phone. I found this project and it is awesome for this. However I haven't made it work with ble? Is this something you tried or perhaps are trying to incorporate. Thanks for the answer.

jeflem commented 4 weeks ago

Hi @Matejvvo,

in principle spremote also works with Bluetooth. You have to establish a bluetooth connection via your operating system. Then you create a serial Bluetooth device and everything works in exactly the same way as with USB (spremote requires a serial connection without knowing whether it is USB or Bluetooth). See my blog post Host-hub communication for LEGO Spike Prime on Linux for some more details.

The problem: starting with the LEGO firmware published in September 2023 (the one spremote has been developed for), Bluetooth connection to the hub fails for unknown reasons. Tested this extensively on Linux, MacOS, Windows without any success. It works in the LEGO App, but not without the app.

A workaround would be to use the old firmware (it also has a much more powerful Python API). Then Bluetooth works, but spremote needs lots of tweaking. The central Hub.cmd method should work without modifications. But the sensor and motor classes have to be rewritten.

Beste regards,

Jens

Matejvvo commented 3 weeks ago

Hi thanks for the reply.

Do you mean it was unreliable with bluetooth or with bluetooth LE?

I tried to connect directly via the official LEGO documentation https://lego.github.io/spike-prime-docs/ over their protocol and that works fine. I then wanted to access the repl, but that to my knowledge disables the LEGO communication, so this can't even work if I am right.

What I am trying to make is a remote control that would work with the official newest spike prime software, since we use these in our school and other firmware like pybricks isn't an option. My next steps in this research is probably to find way I could control motors directly through the official communication. Or perhaps establish the bluetooth connection some other way inside the hub.

jeflem commented 3 weeks ago

With the old firmware (the one with the hub module in the hub's Python install, I think it was version 2, now it's 3) I had to press the hub's bluetooth button and my OS (Linux) connected to the hub without Spike App. Then I was able to create a serial bluetooth device via this connection. This connection gave me access to the hub's Python interpreter without doing anything on the hub side (except for interrupting the program running there)

With current firmware my OS isn't able to connect via bluetooth anymore. Sometimes it connects, but after same seconds (in rare cases after 1 or 2 minutes) it disconnects without any reason before I'm able to create the serial device and run my code. Some day I'll dig into this, but at the moment I'm satisfied with USB (need it for power supply anyway). Don't now very much about bluetooth. Even can't tell the difference between BLE and usual bluetooth...

Didn't know the link you posted. It's new (2024) and looks quite interesting. If you make any progress with bluetooth remote control on current firmware, please let me know.

Matejvvo commented 3 weeks ago

I think the version 2 was standard bluetooth that can create a serial device. Now version 3 has only BLE and I had to use https://github.com/Jakeler/ble-serial this tool to create serial device. But I couldn't get to the repl. I will keep digging and I'll update this thread if I find anything new.

Just to share some details about this tool https://github.com/Jakeler/ble-serial. You can follow the readme and use characteristics from https://lego.github.io/spike-prime-docs. Here they are with the correct options. Remember to keep it all lowercase. This creates the serial device.

ble-scan
ble-serial -d <id_from_the_scan>
-w 0000fd02-0001-1000-8000-00805f9b34fb 
-r 0000fd02-0002-1000-8000-00805f9b34fb 
-s 0000fd02-0000-1000-8000-00805f9b34fb
jeflem commented 3 weeks ago

Very interesting! Did some quick tests with ble-serial. Connecting to the hub works without any problems. The difficult part is that the hub does not provide a terminal via bluetooth but only understands a set of messages for uploading firmware aso. (see https://lego.github.io/spike-prime-docs).

Tried to stop the hub OS via USB (send byte 3, i.e. Ctrl+C) to get access to the REPL. But then bluetooth communication is gone, too. Thus, bluetooth communication is implemented in the Python program run by the interpreter by default. It's not build directly into the firmware (in contrast to USB communication).

One (not very elegant) way for remotely controlling a hub via bluetooth could be as follows:

Didn't test this, but will give it a try next weeks/months.