Revision as of 04:46, 14 February 2019 by Jedibrine (talk | contribs) (There is a Python article existant, linked to that and removed unneccessary styling)

SandCastleIcon.png This page has links to websites or programs not trusted by Scratch or hosted by Wikipedia. Remember to stay safe while using the Internet, as we can't guarantee the safety of other websites.

This is a tutorial on how to communicate with Scratch using Remote Sensor Connections through a Python program. It includes a minimal GUI window that prompts the user for a message to broadcast to Scratch. If the version in use is Python 3.x, see this article.

There are also a few Python libraries that can be used to interact with Scratch, which may be simpler.

Install Python

  • Mac OS X comes with Python built in. It can be run from the command line using the terminal program via the command python or python <file>, with the file's path.
  • The latest version of Python is available here.
  • A free Python development environment is available from Active State

Enabling Remote Sensor Connections

  • In a Scratch project, right click on one of the sensor tiles and click "enable remote sensor connections". This is necessary for the connection to be made.

Creating 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. If the IDLE editor for Python is being used, open it. Name the file SendBroadcast.py (it can be named anything, however, it must have ".py" at the end). For use in TextEdit, press Shift-Command-T to convert the document to plain text; the formatting bar at the top will disappear.

Start by importing all of the necessary python modules and initiating Tkinker:

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, 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")

Then, create the function that 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)

Lastly, create a user input to show what broadcast would be sent 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 + '"')
  • Python should log that a successful connection has been made, it will then print the messages it is sending
  • In Scratch, create a script that starts with the "when I receive beat." If you run the Python program, Scratch should respond.
  • The value of the note variable being changed by Python appears in the "sensor value" block's menu
  • Sending a broadcast from Scratch will be logged in the Python Console.
  • Creating or updating a global variable in Scratch will be logged in the Python Console.