From Test-Scratch-Wiki

SandCastleIcon.png This page has links to outside of the Scratch website or Wikipedia. Remember to stay safe when using the internet as we can't guarantee the safety of other sites.

Here are some instructions for how to communicate with Scratch using Remote Sensor Connections from a Python program. It includes a minimal GUI window that prompts the user for a message to broadcast to Scratch. If you have Python 3.x, see this article.

There are also a few Python libraries you can use to interact with Scratch, which may be easier.

Install Python

  • Mac OS X comes with Python built in. You can run it from the command line using the terminal program via the command python or python <file>, with the file's path.
  • You can get the latest version of Python from here.
  • You can get a free Python development environment from Active State

Enable Remote Sensor Connections

  • In your Scratch project, right click on one of the sensor tiles and click "enable remote sensor connections". You must do this to connect to Python.

Sample client program

Sample client program to send messages and update variable:

Start off by creating a new Plain Text File in TextEdit for Mac, or Notepad for Windows. Also, if you are using the IDLE editor for Python, make a new window in that. Name the file SendBroadcast.py (it can be named anything you want if you have .py at the end). If you are using TextEdit, press Shift-Command-T to convert the document to plain text; you should see the formatting bar at the top disappear.

Let's start by importing everything:

Note Note: This tutorial only works with Python 2.x. Later versions do not have the modules.
   from array import array
   import socket
   import time
   import sys
   
   from Tkinter import Tk
   from tkSimpleDialog import askstring
   root = Tk()
   root.withdraw()

Next, let's add some text input to choose what IP to connect to:

   PORT = 42001
   HOST = askstring('Scratch Connector', 'IP:')
   if not HOST:
       sys.exit()
   
   print("connecting...")
   scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   scratchSock.connect((HOST, PORT))
   print("connected")


Now, we create the function that actually sends the broadcast to Scratch:

   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)

The last part is creating user input to show what broadcast to send to Scratch:

   while True:
       msg = askstring('Scratch Connector', 'Send Broadcast:')
       if msg:
           sendScratchCommand('broadcast "' + msg + '"')

The entire program:

   from array import array
   import socket
   import time
   import sys
   
   from Tkinter import Tk
   from tkSimpleDialog import askstring
   root = Tk()
   root.withdraw()
   
   PORT = 42001
   HOST = askstring('Scratch Connector', 'IP:')
   if not HOST:
       sys.exit()
   
   print("Connecting...")
   scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   scratchSock.connect((HOST, PORT))
   print("Connected!")
   
   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)
   
   while True:
       msg = askstring('Scratch Connector', 'Send Broadcast:')
       if msg:
           sendScratchCommand('broadcast "' + msg + '"')
  • If it works you should see Python say that it has connected, and then it will print the messages it is sending
  • Now, in Scratch create a script that starts with the "when I receive beat." If you restart the Python program, Scratch should respond.
  • You can also see the value of the note variable being changed by Python; it appears in the "sensor value" block's menu
  • If you send a broadcast from Scratch, you will see it print out on the Python Console.
  • If you create or update a global variable in Scratch, you will see that on the Python Console.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.