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

Send messages between a hub and a bluetooth application. #8

Open
4 of 8 tasks
gpdaniels opened this issue Nov 20, 2020 · 5 comments
Open
4 of 8 tasks

Send messages between a hub and a bluetooth application. #8

gpdaniels opened this issue Nov 20, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request project - controller For issues about the BLE controller

Comments

@gpdaniels
Copy link
Owner

gpdaniels commented Nov 20, 2020

Desktop app:

  • GUI
  • BT connection
  • BLE connection

Android app:

  • GUI
  • BT connection
  • BLE connection
  • Permissions
  • Running in the background
@gpdaniels gpdaniels added the project - controller For issues about the BLE controller label Nov 20, 2020
@gpdaniels gpdaniels self-assigned this Nov 20, 2020
@gpdaniels gpdaniels added the enhancement New feature or request label Nov 20, 2020
@gpdaniels gpdaniels changed the title Send a message to a hub from a BLE application. Send messages between a hub and a bluetooth application. Dec 21, 2020
@postpersonality
Copy link

postpersonality commented Jan 8, 2021

Hi! I've investigated this a bit. I have a small dump of a short bluetooth session between Android Robot Inventor and the hub. So far I see it's working via BT, not BLE since it uses RFCOMM. RFCOMM is a part of BT stack.

Here is what I found so far.

Android -> Hub request messages

  • {"i":"....","m":"get_hub_info","p":{}}
  • {"i":"....","m":"program_modechange","p":{"mode":"play"}}
  • {"i":"....","m":"program_terminate","p":{}}
  • {"i":"....","m":"reset_program_time","p":{}}
  • {"i":"....","m":"scratch.display_animation","p":{"frames":["00900:06960:99099:06960:00900","00900:06960:99099:06960:00900","00900:06960:99099:06960:00900","00900:06960:99099:06960:00900"],"async":false,"delay":500,"fade":5,"loop":false}}
  • {"i":"....","m":"scratch.display_clear","p":{}}
  • {"i":"....","m":"scratch.display_rotate_orientation","p":{"orientation":"4"}}
  • {"i":"....","m":"scratch.motor_go_to_relative_position","p":{"port":"A","position":0,"speed":35,"stall":true,"stop":1}}
  • {"i":"....","m":"scratch.motor_pwm","p":{"port":"A","power":50,"stall":true}}
  • {"i":"....","m":"scratch.motor_pwm","p":{"port":"A","power":-50,"stall":true}}
  • {"i":"....","m":"scratch.motor_run_timed","p":{"port":"A","time":1000,"speed":50,"stall":true,"stop":1}}
  • {"i":"....","m":"scratch.motor_set_position","p":{"port":"A","offset":0}}
  • {"i":"....","m":"scratch.motor_set_position","p":{"port":"A","offset":73}}
  • {"i":"....","m":"scratch.motor_start","p":{"port":"A","speed":80,"stall":true}}
  • {"i":"....","m":"scratch.motor_start","p":{"port":"A","speed":-80,"stall":true}}
  • {"i":"....","m":"scratch.motor_stop","p":{"port":"A","stop":1}}
  • {"i":"....","m":"scratch.play_sound","p":{"path":"/extra_files/Hydraulics Up","volume":10,"freq":16000,"wait":false}}
  • {"i":"....","m":"scratch.reset_yaw","p":{}}
  • {"i":"....","m":"scratch.ultrasonic_light_up","p":{"port":"F","lights":[0,0,0,0]}}
  • {"i":"....","m":"scratch.ultrasonic_light_up","p":{"port":"F","lights":[100,100,100,100]}}
  • {"i":"....","m":"trigger_current_state","p":{}}

Description of the request message

  • i — Random message ID (consists of 4 chars from range: 0-9, a-z, A-Z, -, _) this is needed for identification of response message from the hub
  • m — Command to be executed. This is mapped to the methods inside commands/* of the filesystem of the hub. The methods begin with handle_....
  • p — Object with parameters specific to a command

Hub -> Android status messages

  • {"m":0,"p":[
    • [75, [0, 0, 179, 0]], // Port A (state of a motor)
    • [0, []], // Port B
    • [0, []], // Port C
    • [0, []], // Port D
    • [0, []], // Port E
    • [0, []], // Port F
    • [-8, 0, 1021], // compass
    • [6, 10, -5], // acceleration
    • [0, 0, 0], // z rot, long side incline, short side incline
    • "",
    • 9932]}
  • {"m":2,"p":[8.323, 100, false]} // Battery status message, sent once a second
  • {"m":4,"p":"down"} — event when hub gets flipped to a side
  • {"m":4,"p":"doubletapped"}

@pizzakid25
Copy link

pizzakid25 commented Feb 9, 2021

I found a few additional commands I thought might be useful:

  • {"i":"....","m":"program_execute","p":{"slotid": 1}} - Starts program saved to slot 1.
  • {"i":"....","m":"program_terminate","p":{}} - Stops currently running program.
  • {"i":"....","m":"get_storage_status","p":{}} - Returns current storage and slot information.
  • {"i":"....","m":"move_project","p":{"old_slotid": 1, "new_slotid": 2}} - Moves program from slot 1 to slot 2.
  • {"i":"....","m":"remove_project","p":{"slotid": 1}} - Deletes program from slot 1.

@brabebhin
Copy link

Hi. Does sending these commands work with the factory firmware or do I have to flush something custom?

I have a research project using these 51515 hubs as targets, and I have been looking for a way to control them through a desktop application without the need of LEGO app.

@pizzakid25
Copy link

pizzakid25 commented Jun 22, 2021

@brabebhin These commands work with the default firmware over Bluetooth or USB. Most commands should work for both 51515 and Spike hubs, but there might be a few that are system-specific since the firmware varies slightly.

@brabebhin
Copy link

Cool thank you for the information. One more question: i know in older lego sets the serial message had to be prefixed with the length of the message, does this apply here as well?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request project - controller For issues about the BLE controller
Projects
None yet
Development

No branches or pull requests

4 participants