A visual block-based programming environment for controlling robotics platforms via Bluetooth Web API. Built on Google's Blockly framework, this application provides an intuitive drag-and-drop interface for programming robots with real-time execution and debugging capabilities.
- Overview
- Features
- Architecture
- Getting Started
- Usage
- Block Categories
- Bluetooth Communication
- Code Generation
- Development
- Project Structure
- Contributors
- License
Blocks.Mangdang is a web-based visual programming environment designed for educational robotics. It enables users to program robots (specifically the "Santa-Bot" platform) using intuitive drag-and-drop blocks instead of traditional text-based coding. The application communicates with robots via Bluetooth Low Energy (BLE) and supports dynamic tool discovery through the Model Context Protocol (MCP).
- Blockly (v12.2.0) - Google's visual programming library
- Web Bluetooth API - Browser-based BLE communication
- JavaScript Interpreter - Acorn-based safe code execution
- Prism.js - Syntax highlighting for code preview
- Model Context Protocol (MCP) - Dynamic tool discovery and management
- π§© Drag-and-Drop Interface - Intuitive block-based programming
- π¦ Categorized Blocks - Organized toolbox with conditionals, loops, functions, variables, and operations
- π¨ Custom Theme - MangDang Santa-themed color palette
- π Workspace Minimap - Bird's-eye view of large programs
- πΎ Save/Load Projects - Local storage with import/export capabilities
βΆοΈ Real-time Execution - Run programs directly on connected robots- π Block Highlighting - Visual feedback during execution
- π Variables Inspector - Live variable monitoring during program execution
- π Stop Control - Immediate program termination
- π Debug Console - Comprehensive Bluetooth communication logging
- π‘ Web Bluetooth API - Browser-native BLE connectivity
- π Auto-Reconnection - Seamless reconnection to previously paired devices
- π οΈ Dynamic Tool Discovery - MCP-based runtime tool loading
- π Communication Log - Full message history with filtering
- π Python Export - Generate Python code from blocks
- π Copy to Clipboard - Easy code sharing
- π€ Syntax Highlighting - Pretty-printed code with line numbers
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Browser Frontend β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Blockly β β Execution β β Bluetooth β β
β β Workspace ββββ Controller ββββ Controller β β
β ββββββββ¬ββββββββ βββββββββ¬βββββββ βββββββββ¬βββββββ β
β β β β β
β β β β β
β ββββββββΌβββββββ ββββββββββΌβββββββββ ββββββββΌβββββββ β
β β Custom β β JS Interpreter β β Web BLE β β
β β Blocks β β (Acorn-based) β β API β β
β βββββββββββββββ βββββββββββββββββββ ββββββββ¬βββββββ β
β β β
ββββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββ
β
βββββββΌββββββββ
β Santa-Bot β
β Robot β
β (BLE) β
βββββββββββββββ
- blocklySetup.js - Workspace initialization and configuration
- customBlocks.js - Custom block definitions and behaviors
- codeGenerator.js - Python and JavaScript code generation
- executionController.js - Program execution and stepping logic
- bluetoothController.js - BLE device management and MCP communication
- interpreter.js - Safe JavaScript execution environment
- variablesPaneController.js - Live variable monitoring
- modalController.js - UI modal management
- toastController.js - Notification system
- Modern web browser with Web Bluetooth API support (Chrome 56+, Edge 79+, Opera 43+)
- Node.js and npm (for dependency installation and build tools)
- A compatible robot with BLE support (e.g., Santa-Bot)
- OpenSSL (for generating HTTPS certificates)
Quick Setup (3 steps):
-
Clone and install dependencies
git clone https://github.com/mangdangroboticsclub/scratch.git cd scratch npm install -
Start a local server
# Python 3 python -m http.server 8000 # OR Node.js npx http-server -p 8000
-
Open in browser
Navigate to
http://localhost:8000
β Works with Bluetooth:
localhostis treated as a secure context, enabling full Web Bluetooth functionality.
β οΈ Network limitation: External devices (http://192.168.x.x) require HTTPS - see advanced setup below.
For testing across devices or production deployment, HTTPS certificates are required:
-
Generate SSL certificates
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 \ -keyout blockly-v3+3-key.pem -out blockly-v3+3.pem
Set Common Name to
localhostwhen prompted -
Start HTTPS server
# Node.js npm install -g https-server https-server -S -C blockly-v3+3.pem -K blockly-v3+3-key.pem -p 8000 # OR Python 3 python -m http.server 8000 --bind localhost \ --certfile blockly-v3+3.pem --keyfile blockly-v3+3-key.pem
-
Access via HTTPS
Navigate to
https://localhost:8000(accept self-signed certificate warning)
Modifying block definitions? Regenerate the toolbox:
node scripts/_generateTools.jsScans scripts/tools/ and compiles all block JSONs into list.json
Web Bluetooth API is required and supported on:
- β Chrome 56+ (Desktop & Android)
- β Edge 79+
- β Opera 43+
- β Firefox (not supported)
- β Safari (not supported)
Important: Web Bluetooth API only works in secure contexts (HTTPS or localhost). The application must be served over HTTPS to enable Bluetooth connectivity. See installation instructions above for certificate generation.
- Click the Bluetooth button in the top-left corner
- Select your "Santa-Bot" device from the browser dialog
- Wait for the connection to establish
- MCP tools will automatically load into the dropdown blocks
- Drag blocks from the categorized toolbox on the left
- Start with a "Start" block - Required to begin program execution
- Connect blocks vertically to build your program sequence
- End with a "Stop" block - Signals program completion
- Use the Variables button to monitor variable values
- Click the Run Program button (visible when connected)
- Watch blocks highlight as they execute
- Monitor variables in the Variables pane
- Use Stop Program to halt execution
- Click Save/Load dropdown in the top bar
- Save now - Auto-save to browser local storage
- Export save - Download project as JSON file
- Import save - Load previously exported project
- Clear workspace - Start fresh (with confirmation)
- Click Show as Code button
- View generated Python code with syntax highlighting
- Use Copy Code to copy to clipboard
- Use Copy Santa Code for robot-specific format
- Start - Program entry point (only one allowed)
- Stop - Program termination (only one allowed)
- Send [text] to Santa - Send text messages to the robot to say out loud
- Make Santa [command] - Execute dynamic MCP commands with parameters
- Set variable - Create or update variables
- Get variable - Read variable values
- Integer - Numeric literal
- Text - String literal
- Boolean - True/False values
- If-Else - Conditional branching
- Comparison - Equality and relational operators (=, β , <, >, β€, β₯)
- For loop - Count-controlled iteration
- While loop - Condition-controlled iteration
- Arithmetic - Math operations (+, -, Γ, Γ·)
- Logic - Boolean operations (AND, OR, NOT)
- Lists - Array creation and manipulation
- Text - String operations (join, length)
- Random - Random number generation
- Define function - Create reusable procedures
- Define function with return - Create functions that return values
- Function calls - Invoke defined functions
The application uses Bluetooth Low Energy (BLE) with a custom GATT service:
- Service UUID:
12345678-1234-5678-1234-56789abcdef0(Robot Control Service) - Characteristic UUID:
12345678-1234-5678-1234-56789abcdef1(Data Exchange)
Messages are JSON-encoded and support chunking for large payloads:
{
"type": "command" | "query" | "response",
"data": { ... },
"chunk": { "index": 0, "total": 1, "id": "unique-id" } // Optional
}Upon connection, the application queries available tools:
- Sends
list_toolscommand to robot - Receives tool definitions with schemas
- Dynamically populates "Make Santa" dropdown blocks
- Generates parameter inputs based on tool schemas
- Disconnected - No active connection
- Connecting - Attempting to establish connection
- Connected - Active BLE connection with tool discovery complete
- Reconnecting - Attempting to restore previous connection
Converts blocks to executable Python code:
# Program starts here
send_message('Wake Up')
# Set variable example
my_variable = 42
# Conditional example
if my_variable > 10:
execute_santa_command("move.forward", {"speed": 50})
# Program ends here
execute_santa_command("self.system.quit", {})For in-browser execution with the JS interpreter:
// Program starts here
send_msg('Wake Up');
// Variable tracking
my_variable = 42;
setVar('blockId=my_variable');
// Conditional with highlighting
highlightBlock('block_id_123');
if (my_variable > 10) {
highlightBlock('block_id_456');
cmd_dropdown("move.forward", {"speed": 50});
}- Define the block in
scripts/customBlocks.js:
Blockly.Blocks['my_custom_block'] = {
init: function() {
this.appendValueInput("INPUT")
.appendField("My Block");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(230);
}
};- Add Python generator in
scripts/codeGenerator.js:
pythonGenerator.forBlock['my_custom_block'] = function(block, generator) {
const input = generator.valueToCode(block, 'INPUT', pythonGenerator.ORDER_ATOMIC);
return `my_function(${input})\n`;
};- Add JavaScript generator for execution:
javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {
const input = generator.valueToCode(block, 'INPUT', generator.ORDER_ATOMIC);
return `myFunction(${input});\n`;
};- Create block definition JSON in
scripts/tools/operations/my_block.json:
{
"type": "my_custom_block",
"kind": "block"
}- Regenerate toolbox by running the build script:
node scripts/_generateTools.jsThis will automatically scan the scripts/tools/ directory and update scripts/tools/list.json with your new block, organizing it by folder structure.
Custom styles are organized in the styles/ directory:
main.css- Core layout and workspace stylespalette.css- Color theme definitionstoolbox.css- Toolbox customizationmodal.css- Modal dialog stylesbluetoothModal.css- Bluetooth debug UIvariablesPane.css- Variables inspector styling
Edit CSS variables in styles/palette.css:
:root {
--primary: #1a1a2e;
--secondary: #16213e;
--accent: #0f3460;
--highlight: #e94560;
--background: #f8f9fa;
/* Block category colors */
--category-conditional: #5b9bd5;
--category-loop: #70ad47;
--category-function: #ffc000;
/* ... */
}Blocks.Mangdang/
βββ index.html # Main application entry point
βββ package.json # npm dependencies
βββ LICENSE # Apache 2.0 License
βββ README.md # This file
βββ blockly-v3+3.pem # HTTPS certificate (generated)
βββ blockly-v3+3-key.pem # HTTPS private key (generated)
β
βββ scripts/ # JavaScript modules
β βββ _generateTools.js # Build script to compile block definitions
β βββ acorn_interpreter.js # JS interpreter (Acorn-based)
β βββ blocklySetup.js # Workspace initialization
β βββ bluetoothController.js # BLE device management
β βββ codeGenerator.js # Python/JS code generation
β βββ customBlocks.js # Custom block definitions
β βββ customCategory.js # Dynamic category management
β βββ debug.js # Debug utilities
β βββ executionController.js # Program execution logic
β βββ functionController.js # Function block handling
β βββ interpreter.js # Execution environment setup
β βββ minimapController.js # Workspace minimap
β βββ modalController.js # Modal dialog management
β βββ saveDropdownController.js # Save/load functionality
β βββ showCodeModal.js # Code preview modal
β βββ simpleRunButtonController.js # Run button logic
β βββ toastController.js # Notification system
β βββ toolboxController.js # Toolbox management
β βββ toolboxResizer.js # Toolbox resizing
β βββ topBarController.js # Top bar UI
β βββ topBarWidthController.js # Responsive top bar
β βββ variablesPaneController.js # Variable inspector
β β
β βββ tools/ # Block definitions
β βββ list.json # Generated toolbox structure (auto-built)
β βββ conditionals/ # If-else, comparison blocks
β βββ functions/ # Function definition blocks
β βββ loops/ # For, while loop blocks
β βββ mainloop/ # Start, stop blocks
β βββ operations/ # Math, logic, list operations
β βββ santa/ # Robot command blocks
β βββ variables/ # Variable blocks
β
βββ styles/ # CSS stylesheets
β βββ main.css # Core layout
β βββ palette.css # Color theme
β βββ bluetoothModal.css # BLE debug UI
β βββ codeModal.css # Code preview
β βββ dropdown.css # Dropdown menus
β βββ minimap.css # Workspace minimap
β βββ modal.css # Modal dialogs
β βββ resizer.css # Resizable panels
β βββ runDropdown.css # Run options
β βββ saveDropdown.css # Save menu
β βββ toast.css # Notifications
β βββ toolbox.css # Block toolbox
β βββ topButtons.css # Top bar buttons
β βββ variablesPane.css # Variable inspector
β
βββ svg/ # Vector graphics
βββ Background.svg # Santa background
βββ Bluetooth.svg # Bluetooth icon
βββ Snowflake.svg # Decorative snowflake
- Follow existing code style and conventions
- Test Bluetooth connectivity thoroughly (requires HTTPS)
- Ensure blocks work in both code generation modes (Python & JavaScript)
- Update documentation for new features
- Add block definitions to appropriate category folders
- Run
node scripts/_generateTools.jsafter adding/modifying block JSON files - Always test on another device with HTTPS server when working with Bluetooth features
Haris - Lead Developer & Designer
- Complete application architecture and implementation
- Bluetooth Web API integration with MCP protocol
- Custom block system and code generation (Python & JavaScript)
- Cloud deployment and production hosting setup
- Execution controller with real-time debugging
- Comprehensive project documentation
Mandy - Design Contributor
- SVG graphics and visual assets (Background, Snowflake, icons)
- Color palette and theme design
- UI/UX design language with Figma
- Visual design system and branding
- MangDang Robotics Club - Robot platform and testing support
- Open Source Community - Blockly, Web Bluetooth API, and related technologies
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Copyright 2025 MangDang Technology Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
- Google Blockly - Visual programming framework
- Web Bluetooth Community Group - BLE API specification
- MangDang Robotics Club - Robot platform
- Acorn - JavaScript parser for safe code execution
- Prism.js - Syntax highlighting
For issues, questions, or contributions:
- GitHub Issues: Report a bug
- Repository: MangDang Robotics Club - Scratch
Made with β€οΈ for robotics education