Skip to Content

How can I connect to Scratch using Python?

« Computer Science Education
2 replies [Last post]
Stephen Thomas
Member

 I checked Communicating to Scratch via Python but the links for sample code and the sample project are broken/non-existant.  I saw the Action Script version which seems to explain things fairly well and I could create a python version, but I am preparing for a Technogly/Robotics/Programming club and if there is already a library it would save me time.

 

Update: I fixed the Communicating to Scratch via Python page and it now has information on how to connect Python to Scratch using the Remote Sensor Protocol

 

Thanks,

Stephen

 

 

Replies
Louis Bertrand
Member
 Things have changed in 2.0. You now need an extension helper app between the Scratch offline editor and the Arduino. Scratch speaks sends formatted queries to the extension app over a network socket (remote or localhost) and the app then communicates with the Arduino.

Example:
The Snap!Mobile - Start Your Physical Computing Engines! by MrYsLab (Instructables)
www.instructables.com/id/The-SnapMobile-Start-Your-Physical-Computing-Engin/

Software extension (requires Python 2.x and a few modules):
MrYsLab/s2a_fm: A Scratch Hardware Extension For Arduino
github.com/MrYsLab/s2a_fm

s2a_fm also requires the Arduino to be programmed with Firmata to process the commands and requests from the extension app.

Caveats:
1) I have not tried this (decided that I don't need it for my course)
2) The Scratch team warns that the extension framework is subject to change.
Stephen Thomas
Member

 I found some code from johnm (John Maloney?) for interfacing using Python and it works great, I was able to connect Scratch to some Python programs, broadcast and receive broadcast messages and variable updates. I was also able to link to Etoys using Scratch connect very cool.

 

Below is the code I used (which hopefully renders properly, not sure of the wiki markup here)

 

Scratch client test program

# sends 10 "beat" broadcasts to Scratch

 

from array import array

import socket

import time

 

HOST = '127.0.0.1'

PORT = 42001

 

def sendScratchCommand(cmd):

n = len(cmd)

a = array('c')

a.append(chr((n >> 24) & 0xFF))

a.append(chr((n >> 16) & 0xFF))

a.append(chr((n >>  8) & 0xFF))

a.append(chr(n & 0xFF))

scratchSock.send(a.tostring() + cmd)

 

print("connecting...")

scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

scratchSock.connect((HOST, PORT))

print("connected")

 

for i in xrange(10):

sendScratchCommand('sensor-update note ' + str(60 + (2 * i)) + ' beats 0.4')

sendScratchCommand('broadcast "beat"')

print("beat!")

time.sleep(0.5)

 

print("closing socket...")

scratchSock.close()

print("done")[/code]

 

 

Scratch client test program

# Receives and prints data sent by Scratch; use control-C to quit

 

from array import array

import socket

import time

 

HOST = '127.0.0.1'

PORT = 42001

 

print("connecting...")

scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

scratchSock.connect((HOST, PORT))

print("connected! waiting for data...")

 

# print incoming data forever

while 1:

time.sleep(0.01)

data = scratchSock.recv(1024)

if not data: break

print(data)[/code][/quote]