{"id":984,"date":"2017-06-05T13:07:23","date_gmt":"2017-06-05T11:07:23","guid":{"rendered":"http:\/\/heppg.de\/ikg\/wordpress\/?p=984"},"modified":"2018-03-21T22:44:42","modified_gmt":"2018-03-21T21:44:42","slug":"scratch2-connected-to-scratchclient","status":"publish","type":"post","link":"https:\/\/heppg.de\/ikg\/wordpress\/?p=984","title":{"rendered":"scratch2 connected to scratchClient"},"content":{"rendered":"<h2><strong>Did you ever want to control GPIO from scratch2 ? Or other sensors connected to a raspberry pi ?<\/strong><\/h2>\n<p>The last days I worked on connecting scratch2 with GPIO on raspberry and more.<br \/>\nTo be precise, this is done by either using scratchx or since june 2017 with the standalone version of scratch2 for raspbian.<\/p>\n<ul>\n<li>use scratch2 version named scratchX which supports &#8216;scratch extensions&#8217;. This version is identical to scratch2 with few limitations. Or use the standalone version on raspberry.<\/li>\n<li>extending scratchClient to talk with scratch2 and providing sensor values and events, but also receive values from scratch2 and events.<\/li>\n<li>scratchClient using adapters to connect peripheral devices as usual. For example to GPIO.<\/li>\n<li>The blocks in scratchX are defined from the information available in the config file used by scratchClient.<\/li>\n<\/ul>\n<p>This new version of scratchClient is still supporting scratch1.4 and there are no changes in the config files needed.<\/p>\n<p>One of the limitations in scratchX are that sharing projects is not possible.<\/p>\n<p>The extension blocks in scratchX are defined using the definitions from the config xml file from scratchClient. So there is no programming effort needed to build the extension file.<\/p>\n<p>Current implementation allows to send\/receive values and events from scratchX to\/from scratchClient. This is similiar to the features available in scratch1.4 with the broadcast system.<\/p>\n<h3>Problems, Inconsistencies with scratch 1.4 broadcast system<\/h3>\n<p>In legacy 1.4 scratch, there are some predefined blocks for motors and sensor names for scratchBoard. This needed many explanations for unexperienced users to avoid confusion.<\/p>\n<div id=\"attachment_1016\" style=\"width: 189px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/block_motor.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1016\" class=\"wp-image-1016 size-full\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/block_motor.png\" alt=\"\" width=\"179\" height=\"208\" \/><\/a><p id=\"caption-attachment-1016\" class=\"wp-caption-text\">scratch 1.4 motor blocks in sensing palette<\/p><\/div>\n<div id=\"attachment_1017\" style=\"width: 211px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/block_sensor_values.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1017\" class=\"wp-image-1017 size-full\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/block_sensor_values.png\" alt=\"\" width=\"201\" height=\"185\" \/><\/a><p id=\"caption-attachment-1017\" class=\"wp-caption-text\">scratch 1.4 default sensor names<\/p><\/div>\n<p>It was also difficult to explain that values from scratchClient are received as sensor values, but values to be sent out need to be defined as variables &#8216;for all sprites&#8217;.<\/p>\n<p>In addition to this, the extra step &#8216;enable remote sensor connections&#8217; is hidden down in a context menu of a block on lower edge of the window.<\/p>\n<h3>Advantages of the scratch2, scratchX block based interfacing<\/h3>\n<p>In scratchX, all the capabilities of the external system are exposed as blocks. This avoids all the mentioned problems from legacy scratch 1.4.<\/p>\n<h3>Modifications in scratchClient<\/h3>\n<p>scratchClient in its initial design is scratch1.4 centric. The start and stop logic is scratch 1.4 oriented and needed another structure.<\/p>\n<p>During this work, the internal web app was lifted to use tornado framework. Which has build in websocket support and is easier to use as cherrypy. As scratchX is online only, the connection to a local data provider needs to bypass the &#8216;same server policy&#8217; and this was quite simple to achieve with tornado.<br \/>\nMonitoring and simulation of events is still possible by using the internal web server. Some bugs have been removed in the web pages on this way.<\/p>\n<h2>Setup procedure<\/h2>\n<p>As the experimantal scratchClient is merged into scratchclient, the installation is same as for scratchClient.<\/p>\n<p>Start scratchClient with the configuration xml file for your hardware. I recommend using python3 to run scratchClient. Unicode support is the reason for this.<\/p>\n<h3>Context<\/h3>\n<p>scratch2 \/ scratchX does not need to run on same computer as scratchClient. The communication beween scratchX and scratchClient uses websocket and http requests. In my test environment, scratchX runs on windows and scratchClient either on a raspberry or on windows.<\/p>\n<h2>Start scratchX<\/h2>\n<p>Load http:\/\/scratchx.org\/#scratch into the browser.<\/p>\n<h3>Load extension loader<\/h3>\n<p>In &#8216;more blocks&#8217;, &#8216;Load Experimental Extension&#8217;,<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-989\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10-300x240.png\" alt=\"\" width=\"300\" height=\"240\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10-300x240.png 300w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10.png 317w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>press on Load Experimental Extensions and open Extension URL<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-990\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20-300x142.png\" alt=\"\" width=\"300\" height=\"142\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20-300x142.png 300w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20-768x363.png 768w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20-624x295.png 624w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20.png 794w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Paste the following url into the form<\/p>\n<pre>https:\/\/megjlow.github.io\/extensionloader.js<\/pre>\n<p>This url brings the Load Extension Block into the additional block palette.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-991\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30-257x300.png\" alt=\"\" width=\"257\" height=\"300\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30-257x300.png 257w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30.png 276w\" sizes=\"auto, (max-width: 257px) 100vw, 257px\" \/><\/a><\/p>\n<h3>Connect to scratchClient<\/h3>\n<p>The Load Extension Block is now used to load the scratchClient configuration. Best practice is to prepare a small script in stage which loads the scratchClient config.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_40.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-992\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_40-300x67.png\" alt=\"\" width=\"300\" height=\"67\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_40-300x67.png 300w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_40.png 619w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Add a wait block before the &#8216;broadcast [blueFlag]&#8217; with 5 seconds. This gives the extension the needed time to load and to initialize.<\/p>\n<p>The program should use &#8216;blueFlag&#8217; in all places where the green flag hat\u00a0<a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_50.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-993\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_50.png\" alt=\"\" width=\"131\" height=\"56\" \/><\/a> is usually used.<\/p>\n<p>The url to use to load the scratchClient extension is<\/p>\n<pre>http:\/\/localhost:8080\/scratchx\/js\/extension.js<\/pre>\n<p>Copy paste this url into the &#8216;load extension block&#8217;. If scratchClient is running on a different computer, change &#8216;localhost&#8217; to either the other computer&#8217;s IP-address which is something like 192.168.2.203 or perhaps a more meaningful name if the network configuration allows.<\/p>\n<h3>Validation<\/h3>\n<p>See the &#8216;green&#8217; dots to the right of the extension name. If these turn red, connection is lost. Possibly scratchClient has been stopped, can&#8217;t be reached or other problems. In my tests the connection was stable.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_validation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-994\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_validation-174x300.png\" alt=\"\" width=\"174\" height=\"300\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_validation-174x300.png 174w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_validation.png 250w\" sizes=\"auto, (max-width: 174px) 100vw, 174px\" \/><\/a><\/p>\n<p>Extra step for the preview: start legacy scratch and open remote sensor connection.<\/p>\n<h2>Loading a project using scratchClient extension<\/h2>\n<p>A program loaded from file system shows the Load Extension Block, but not the scratchClient extension. The blocks from the scratchClient extension are displayed as red blocks.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_reload.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-995\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_reload-297x300.png\" alt=\"\" width=\"297\" height=\"300\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_reload-297x300.png 297w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_reload.png 490w\" sizes=\"auto, (max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<p>When program is started with green flag and the proposed initial script is executed, the Load Extension Block reads the extension from scratchClient and the program can be used.<\/p>\n<h2>Start scratch2 on raspbian<\/h2>\n<p>In raspbian jessie june-2017, there is a standalone scratch2 version available.<\/p>\n<p>Start from programming\/scratch2.<\/p>\n<p>Press shift key and the file menu. The file menu now displays more options.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1020\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10-1-300x230.png\" alt=\"\" width=\"300\" height=\"230\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10-1-300x230.png 300w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_10-1.png 439w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Select import experimental extension.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1021\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_20-1.png\" alt=\"\" width=\"289\" height=\"152\" \/><\/a><\/p>\n<p>Paste the url into the dialog: http:\/\/localhost:8080\/scratchx\/js\/extension.js<\/p>\n<p>Validate in &#8216;more blocks&#8217;. The example here is for a test adapter and looks different for your configuration file.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1022\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30-1-221x300.png\" alt=\"\" width=\"221\" height=\"300\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30-1-221x300.png 221w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/step_30-1.png 239w\" sizes=\"auto, (max-width: 221px) 100vw, 221px\" \/><\/a><\/p>\n<h2>This scratchClient version is released<\/h2>\n<p>There are no known limitations for this release.<br \/>\nJust start scratchClient and use either scratch1.4 (enable remote sensor connections) or follow the procedure to set up a scratch2\/ scratchX connection.<\/p>\n<p>Some features have been removed. Command line switches to control the gui are removed. As the web app server is needed to connect to scratchX, this does not allow to disable the web browser.<\/p>\n<h2>Example: Controlling two wheel robot pi2go with scratch2<\/h2>\n<p>The tests for the scratchX integration have been performed with a new pi2go-configuration. This includes GPIO, PWM, hardware PWM and ADC chips.<\/p>\n<p>The monitoring\/simulation web page display the many inputs and outputs available for this robot.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2go.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1000 size-large\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2go-607x1024.png\" alt=\"\" width=\"607\" height=\"1024\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2go-607x1024.png 607w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2go-178x300.png 178w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2go-624x1053.png 624w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2go.png 681w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/a><\/p>\n<p>Not all adapters fit on the page.<\/p>\n<p>The blocks build from this config in scratchX are (not all blocks shown).<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1001 size-large\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX-916x1024.png\" alt=\"\" width=\"625\" height=\"699\" srcset=\"https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX-916x1024.png 916w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX-268x300.png 268w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX-768x859.png 768w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX-624x698.png 624w, https:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/06\/pi2goX.png 1023w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>On stage, there are the lights reporters and distance reporter visible.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you ever want to control GPIO from scratch2 ? Or other sensors connected to a raspberry pi ? The last days I worked on connecting scratch2 with GPIO on raspberry and more. To be precise, this is done by either using scratchx or since june 2017 with the standalone version of scratch2 for raspbian. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,10,44,3,14],"tags":[45],"class_list":["post-984","post","type-post","status-publish","format-standard","hentry","category-gpio","category-scratch","category-scratch2","category-scratchclient","category-sensor","tag-scratch2-scratchx-scratchclient"],"_links":{"self":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=984"}],"version-history":[{"count":13,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions\/1026"}],"wp:attachment":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}