|Please expand this article or section. You can help by talk page. (December 2013)if you are an editor. More information might be found in a section of the|
|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 not trusted by Scratch or hosted by 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.|
|The subject of this article or section has changed dramatically and requires updating. Some information or images may not be accurate or relevant to the current version of Scratch, the Scratch website, or the article subject. (January 2022)|
- Install Python 3
- macOS has Python 2 pre-installed.
- The latest release version can be obtained from here.
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.
(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.)