Skip to content

Instantly share code, notes, and snippets.

@bricklife
Created May 21, 2020 07:01
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bricklife/017937aab080c860eee6b732c77dd76c to your computer and use it in GitHub Desktop.
Send command to SPIKE from EV3
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
from pybricks.parameters import Button
from connection import SpikePrimeStreamReader
# Add send_command method into SpikePrimeStreamReader class
def send_command(self, command):
self.sock.send((command + '\r').encode())
SpikePrimeStreamReader.send_command = send_command
# Beep!
ev3 = EV3Brick()
ev3.speaker.beep()
# Create the connection. See README.md to find the address for your SPIKE hub.
spike = SpikePrimeStreamReader('40:BD:32:42:CB:37')
while True:
while not any(ev3.buttons.pressed()):
wait(10)
if Button.LEFT in ev3.buttons.pressed():
spike.send_command('{"i":"AAA","m":"scratch.motor_start","p":{"port":"A","speed":-75,"stall":true}}')
if Button.RIGHT in ev3.buttons.pressed():
spike.send_command('{"i":"AAA","m":"scratch.motor_start","p":{"port":"A","speed":75,"stall":true}}')
if Button.CENTER in ev3.buttons.pressed():
spike.send_command('{"i":"AAA","m":"scratch.motor_stop","p":{"port":"A","stop":1}}')
while any(ev3.buttons.pressed()):
wait(10)
@wheel7
Copy link

wheel7 commented Jun 28, 2020

Thanks for this nice sample! I have been looking for a list of scratch commands, but to no avail. Are the commands like "scratch.motor_start" and the proper parameters documented somewhere?

@bricklife
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment