Skip to content
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

JavaScript client, as done with LEGO Education SPIKE website #3

Open
Utopiah opened this issue May 6, 2024 · 4 comments
Open

JavaScript client, as done with LEGO Education SPIKE website #3

Utopiah opened this issue May 6, 2024 · 4 comments

Comments

@Utopiah
Copy link

Utopiah commented May 6, 2024

On https://spike.legoeducation.com/essential/project one can connect to the Hub via WebBluetooth. Consequently there is already a JavaScript client implementation. Is it documented somewhere?

@Utopiah
Copy link
Author

Utopiah commented May 6, 2024

@SteffenLEGO
Copy link

SteffenLEGO commented Jun 3, 2024

We don't have any public javascript documentation, but there is really nothing SPIKE specific to consider when connecting.
We use two characteristics, one for sending data to the hub and one for receiving.

Sending: txChar.writeValueWithoutResponse(data)
Receiving: rxChar.addEventListener('characteristicvaluechanged', ev => console.log(ev))

Note that the documentation is from the perspective of the hub, so my txChar here uses the characteristic that in the documentation is called rx, and vice versa.

Remember to start notifications in order to start receiving data.
rxChar.startNotifications();

If you need more documentation, I think the best ressource is Googles Web Bluetooth Samples

Your link into our application indicates that you have selected SPIKE Essential as your product.
This documentation is for SPIKE Prime, the SPIKE Essential actually uses the LEGO Wireless Protocol for bluetooth connections

@Utopiah
Copy link
Author

Utopiah commented Jun 5, 2024

Thank you for the clarification. I believe it would be interesting to have a most basic example on e.g Glitch that facilitates discoverability. It could be the shortest page allowing to

  • connect to the Hub,
  • list what is connected to it,
  • if there is a servo, make it do 1 turn in a clockwise direction.

The rest would be left to the user but I find that such remixable examples on Glitch helps a lot to foster a community. Having the first steps to experiment then modified is a lot more engaging to begin than generic documentation.

PS: I actually meant to link to https://spike.legoeducation.com/prime/project but good to learn how Essential also works for the future.

cc @adamwern

@SteffenLEGO
Copy link

That is a really good point, thank you!

I'll look into the possibility of setting up a little more examples.
It does feel like there is something fairly fundamental missing from the documentation that you would have to inspect the traffic from the app to figure out.

There are no handles in the protocol to do anything with the hardware. Running motors, turning on lights.
Instead what you have to do is transfer a micropython program (either as text or as bytecode) using the filetransfer handles in the protocol.
I think there might be a hard dependency on the name program.py or program.mpy (depending on if you are sending python text or bytecode)
Then you need to send a ProgramFlowRequest to start the program.

Using the python experience in the Spike app should give you a pretty good idea of how to make the hardware do things, although the app module that can be used in the app to play sounds, plot graph data and stuff like that is obviously not present, you'd have to build things like that yourself using the TunnelMessage which allows you to send arbitrary data back and forth between the running program and whatever app you create.
There are som apis for for sending and receiving tunnel messages in the program, that I suppose should be part of the documentation here.

Essential and the Lego Wireless Protocol works really differently, in that it cannot run programs, it can just run individual commands, so for that you can send a message to make a motor run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants