{"id":4974,"date":"2023-08-05T14:43:41","date_gmt":"2023-08-05T14:43:41","guid":{"rendered":"https:\/\/dalelane.co.uk\/blog\/?p=4974"},"modified":"2026-04-02T17:25:12","modified_gmt":"2026-04-02T17:25:12","slug":"mqtt-extension-for-scratch","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=4974","title":{"rendered":"MQTT extension for Scratch"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/images.dalelane.co.uk\/2023-08-05-mqttscratch\/00-screenshot.png\" alt=\"screenshot\" style=\"border: thin black solid\"\/><\/p>\n<p><strong>Extensions in Scratch let you add additional blocks to the palette. I&#8217;ve written about <a href=\"https:\/\/dalelane.co.uk\/blog\/?p=4846\">how to create extensions<\/a> before, but in this post I want to share my latest extension which adds MQTT support.<\/strong><\/p>\n<p>I don&#8217;t have a particular Scratch project in mind for this yet, but publishing and subscribing to an MQTT broker from a Scratch project would allow multiple web browsers each running Scratch to communicate with each other. I&#8217;m sure there are some fun things this could be used for.<\/p>\n<p><!--more--><\/p>\n<h3>Connecting to an MQTT broker from Scratch<\/h3>\n<p><iframe loading=\"lazy\" width=\"450\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/lF_ifnpqjg0\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen=\"\"><\/iframe><br \/>\n<small><a href=\"https:\/\/youtu.be\/lF_ifnpqjg0\">youtu.be\/lF_ifnpqjg0<\/a><\/small><\/p>\n<p>To connect to an MQTT broker:<\/p>\n<ul>\n<li>Add the new MQTT extension<\/li>\n<li>Choose which broker you want to connect to<\/li>\n<li>Click on <strong>Go to editor<\/strong><\/li>\n<\/ul>\n<h3>MQTT blocks in Scratch<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/images.dalelane.co.uk\/2023-08-05-mqttscratch\/03-blocks.png\" alt=\"screenshot\" style=\"border: thin black solid\"\/><\/p>\n<p>The extension adds three new blocks:<\/p>\n<ul>\n<li><code style=\"font-weight: bold\">publish<\/code> &#8211; publishes a message to an MQTT topic<\/li>\n<li><code style=\"font-weight: bold\">new message<\/code> &#8211; fires when a new message is received on an MQTT topic<\/li>\n<li><code style=\"font-weight: bold\">message<\/code> &#8211; contains the most recent message from an MQTT topic<\/li>\n<\/ul>\n<h3>Demonstrating the MQTT blocks in Scratch<\/h3>\n<p>Here is a simple project that shows the new blocks in action.<\/p>\n<p>I have two sprites:<\/p>\n<h4>sender<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/images.dalelane.co.uk\/2023-08-05-mqttscratch\/01-sender.png\" alt=\"screenshot\" style=\"border: thin black solid\"\/><br \/>\nThis will publish an MQTT message when you click on the sprite<\/p>\n<h4>postman<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/images.dalelane.co.uk\/2023-08-05-mqttscratch\/02-receiver.png\" alt=\"screenshot\" style=\"border: thin black solid\"\/><br \/>\nThis will display MQTT messages when they are received<\/p>\n<p>Giving the project a try&#8230;<\/p>\n<p><iframe loading=\"lazy\" width=\"450\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/cx61bitTHvE\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen=\"\"><\/iframe><br \/>\n<small><a href=\"https:\/\/youtu.be\/cx61bitTHvE\">youtu.be\/cx61bitTHvE<\/a><\/small><\/p>\n<h3>Communicating between Scratch browsers using MQTT<\/h3>\n<p>Sending and receiving a message in one browser isn&#8217;t very interesting, as Scratch already has blocks to send and receive events.<\/p>\n<p>Where this gets interesting is in being able to communicate between multiple browsers, running on different machines.<\/p>\n<p><iframe loading=\"lazy\" width=\"450\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/694cmmO6m10\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen=\"\"><\/iframe><br \/>\n<small><a href=\"https:\/\/youtu.be\/694cmmO6m10\">youtu.be\/694cmmO6m10<\/a><\/small><\/p>\n<h3>A warning<\/h3>\n<p>All of the MQTT brokers I&#8217;m using in the extension are public. This means that every message that is published is available for anyone in the world to see. As such, this isn&#8217;t appropriate for publishing anything private or sensitive.<\/p>\n<h3>Ideas?<\/h3>\n<p>I&#8217;ve added this to the version of Scratch that I run at <strong><a href=\"https:\/\/scratch.machinelearningforkids.co.uk\">scratch.machinelearningforkids.co.uk<\/a><\/strong>.<\/p>\n<p>What could this be used for? What sort of games and projects could be enabled by allowing multiple student projects to communicate with each other?<\/p>\n<p>If you give anything like that a try, please <a href=\"https:\/\/dalelane.co.uk\">let me know<\/a> how you get on!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extensions in Scratch let you add additional blocks to the palette. I&#8217;ve written about how to create extensions before, but in this post I want to share my latest extension which adds MQTT support. I don&#8217;t have a particular Scratch project in mind for this yet, but publishing and subscribing to an MQTT broker from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4975,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[235,536],"class_list":["post-4974","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-mqtt","tag-scratch"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4974"}],"version-history":[{"count":2,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4974\/revisions"}],"predecessor-version":[{"id":5951,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4974\/revisions\/5951"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/media\/4975"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}