This guide outlines the steps to install Scratch 3 on your Raspberry Pi and integrate a custom extension for enhanced functionality. The custom extension you'll be using focuses on MQTT Extension.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
After that, the installer will clone the nvm repository into ~/.nvm/ directory and you should add some lines below at the end of ~/.bashrc file to load nvm.\ run the following command in the terminal.
export NVM_DIR="$HOME/.nvm"
# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# This loads nvm bash_completion
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Note : Close the shell and open a new shell here, otherwise you get command not found.
nvm install node
nvm install --latest-npm
Open a terminal and run the following commands:
mkdir scratch3
cd scratch3
Clone the Scratch GUI and VM repositories:
git clone --filter=tree:0 https://github.com/llk/scratch-gui
git clone --filter=tree:0 https://github.com/llk/scratch-vm
cd scratch-vm
npm install
npm link
cd ../scratch-gui
npm install
npm link scratch-vm
scratch-vm/src/extensions
directory and create a directory (scratch3_mqtt)cd ../scratch-vm/src/extensions
mkdir scratch3_mqtt
Navigate to the new created directory scratch3_mqtt
and download the custom extension index.js file
cd scratch3_mqtt
curl -H "Authorization: token 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" -L "https://raw.githubusercontent.com/einfachIT/scratch3-messaging-extension/master/your-scratch-extension/index.js" -o index.js
Open scratch3
folder in the home/[username]
\
goto scratch-vm/src/extension-support
\
open extension-manager.js
file \
and add Add the following line to the builtinExtensions object like other extensions \
mqtt: () => require('../extensions/scratch3_mqtt'),
after adding the above line save it with ctrl+s
Create a new directory by the name of mqtt
within scratch-gui/src/lib/libraries/extensions
.
cd scratch3/scratch-gui/src/lib/libraries/extensions
mkdir mqtt
cd mqtt
Place your extension's icon images (.png and .svg) inside the newly created directory mqtt
in scratch-gui/src/lib/libraries/extensions
.
curl -H "Authorization: token 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" -L "https://raw.githubusercontent.com/einfachIT/scratch3-messaging-extension/mqtt_extension_v1/mqtt.png" -o mqtt.png
curl -H "Authorization: token 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" -L "https://raw.githubusercontent.com/einfachIT/scratch3-messaging-extension/master/mqtt-small.svg" -o mqtt-small.svg
Goto scratch3
folder in the home/[username]
using file explorer \
open index.jsx
file in `scratch-gui/src/lib/libraries/extensions/'
import your extension's icon URLs and add it to the registeredExtensions array:
add the following lines in the index.jsx file after react where other icons are imported.
import mqttIconURL from "./mqtt/mqtt.png";
import mqttInsetIconURL from "./mqtt/mqtt-small.svg";
add the following lines to the export default array
find export default = [ paste it here ]
{
name: (
<FormattedMessage
defaultMessage="mqtt" // Replace with your extension's name
description="Name for the 'mqtt' extension"
id="gui.extension.mqtt.name"
/>
),
extensionId: "mqtt",
iconURL: mqttIconURL,
insetIconURL: mqttInsetIconURL,
description: (
<FormattedMessage
defaultMessage="mqtt extension"
id="gui.extension.mqtt.description"
/>
),
featured: true,
},
Save the file with CTRL + S
we need to install mosquitto MQTT broker server locally in the (Raspberry Pi) \ run the following command in the terminal and hit Enter
sudo apt-get install -y mosquitto
localhost
in port 8883
After installing Mosquitto and mosquitto-clients, you need to configure it to use in our mqtt extension. The location of the mosquitto.conf
file is:
/etc/mosquitto/mosquitto.conf
Now open mosquitto.conf file with the following command:
sudo nano /etc/mosquitto/mosquitto.conf
Add the following code to the end of mosquitto.conf file
listener 1883
listener 8883
protocol websockets
allow_anonymous true
after adding the above code press CTRL + X
\
and then press Y
key from keyboard
and then press Enter
key to save the changes.
load the configuration file you just created. Insert the following command into the terminal and click Enter:
sudo mosquitto -c /etc/mosquitto/mosquitto.conf
now the broker server will listen to the htpp://localhost:8883/
which is configured in the mqtt extension code.
Navigate to the scratch-gui
and run the following command
cd ../../../../../
npm start
go the browser and run the following URL \
http://localhost:8601/
The MQTT Extension for Scratch 3 allows you to connect, subscribe, and publish messages to an MQTT broker. This guide will help you get started with using the extension in your Scratch projects.
In the Scratch interface, click on the "Extensions" button (a puzzle piece icon at the bottom left).
Your custom MQTT extension should appear in the list of available extensions. Click on it to load the extension into your project.
You will have a blocks in your project
Connect to MQTT Broker:
Subscribe to a Topic:
Drag the "Subscribe to topic [topic]" block into your scripts area. Replace [topic] with the topic you want to subscribe to.
Send a Message:
Drag the "Send message [message] to topic [topic]" block into your scripts area. Replace [message] with the message you want to send and [topic] with the target topic.
New Message Event:
Use the "New Message from [topic]" block as a hat block to trigger scripts when a new message is received on the specified topic.
Get Latest Message:
Drag the "Message from [topic]" block into your scripts area. This reporter block returns the latest message received from the specified topic.
when green flag clicked
connect to MQTT Broker
subscribe to topic [test/topic]
send message [Hello, MQTT!] to topic [test/topic]
when I receive new message from [test/topic]
say (join [New message received: ] (message from [test/topic]))
Ensure Broker Accessibility: Make sure the MQTT broker you are connecting to is accessible from the Scratch environment. The broker address (http:/localhost:8883/) should work. /
Debugging: If you encounter any issues, check the browser console for error messages that can help you troubleshoot the problem.