This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot 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 to use.
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
orpython <file>
, with the file's path. - The latest version of Python is available here.
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.
Sample Client Program
Below is a sample client program to send messages and update variable:
The first step is to create a new plaintext file. This can be done in IDLE, if it is available, or in the operating system's plaintext editor. Name the file SendBroadcast.py (not necessarily, but it's good practice to name Python files with a .py
at the end). If using TextEdit, press ⌘ Cmd+⇧ Shift+T to convert the document to a plaintext document; a sign of this being successful is the formatting bar disappearing.
Start by importing all of the necessary python modules and initiating Tkinker:
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(1)
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(1)
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 "{}"'.format(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
when I receive [beat v]
. Running the Python program and typing in "beat" should cause Scratch to react. - 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.