Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (December 2013) |
This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (December 2015) |
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 article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |
Here are some instructions on how to use Python 3.x to communicate with Scratch 1.x via Remote Sensor Connections. These instructions will create a minimal program that sends commands.
Install Python
- Install Python 3
- macOS has Python 2 pre-installed.
- The latest release version can be obtained from here.
Sample Program
Make a new project in Scratch 1.4. Find the Sensor Value block in the palette, right click on it, and select "enable remote sensor connections". Then drag out a When I Receive block and create the broadcast "hello".
Open a text editor and create a new file. Save it as communication.py and put this code in it:
import socket
HOST = 'localhost'
PORT = 42001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
def sendCMD(cmd):
sock.send(len(cmd).to_bytes(4, 'big'))
sock.send(bytes(cmd, 'UTF-8'))
sendCMD('broadcast "hello"')
Then run the program with Python. This can be done from IDLE, or from the command line: on Microsoft Windows, type this into Command Prompt, followed by Enter; on macOS or Linux, use the Terminal.
python communication.py
(On macOS and some (GNU/)Linux distributions, python
means Python 2.7, so instead type python3
.) Data can also be sent to Scratch by sending sensor-update
, followed by the name of a variable, followed by its new value. Then use Sensor Value to obtain the value. (Refer to Remote Sensors Protocol for details.)