LEGO / spike-prime-docs

Reference documentation for the LEGO® Education SPIKE™ Prime communication protocol.
https://lego.github.io/spike-prime-docs
Other
5 stars 0 forks source link

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

Open Utopiah opened 2 months ago

Utopiah commented 2 months ago

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 commented 2 months ago

A 3rd party implementation seems to be available at https://github.com/tuftsceeo/SPIKE-Web-Interface/blob/master/server/examples/modules/ServiceDock_SPIKE.js

SteffenLEGO commented 1 month ago

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 commented 1 month ago

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

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 commented 4 weeks ago

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.