Mostly Armless + multiple sensor readings into Scrtach

A couple of colleagues and I have entered an internal competition.  An entry to the competition must include a robot, MQTT and node-red.  Our team entry is called “Mostly Armless” with a slogan of  “Scratching an itchy arm with a dash of red raspberry”. Rather than spoil the fun I’m going to let your imagination run a little and just as important keep our fellow competitors guessing. One thing  you won’t need too many guesses on is our entry will also include the use of Scratch.

The visual aspects together with control of the robot will use Scratch.  Mostly Armless will use techniques discussed in earlier post:  Visual programming and integration with Scratch, Node-Red and MQTT that showed how to enable Scratch to interact with the outside world using Node-Red.  Mostly Armless has multiple sensors whose reading need to be fed into Scratch.  This requires a little more work than the previous entry where just one sensor feed was used.

Scratch polls an external server to get the latest sensor readings.  To get multiple sensor readings a list of sensor name and value pairs separated by a new line must be returned in response to a Scratch poll request.  Here is an example of a function node based on the last blog entry that been enhanced to return both the wind direction and barometric pressure as reading testReport and testReport2:

msg.payload="testReport "+context.global.winddirection+
          "\ntestReport2 "+context.global.barometric;
msg.statusCode=200;

return msg;

As scratch polls the server rather than accepting an event when the a sensor reading changes the latest wind direction and barometric pressure readings are stored in a global variable so that the  poll request can be fulfilled.

The Scratch Extension file must also be updated to include knowledge of the additional sensor:

{
    "extensionName": "Test",
    "extensionPort": 1880,
        "useHTTP": true, 
    "blockSpecs": [
        [" ", "test_Command1", "testCommand1"],
                [" ", "test_Command2 %n %n", "testCommand2", 100, 200],
                ["r", "test_Report", "testReport"],
                ["r", "test_Report2", "testReport2"]
    ]
}

With the extension loaded two report blocks are now available for use in the Scratch program. Below is a very simple Scratch program that continuously displays the wind direction for two seconds and then barometric pressure for two seconds:

CaptureItPlus635195441807968750

More instalments on Mostly Armless as the project takes shape.

One response to “Mostly Armless + multiple sensor readings into Scrtach

  1. Hey man, I love your work with node-red and scratch. It would be awesome to see a post on getting physical sensors inputting data into scratch. I’ve tried doing a similar thing to yourself, except I am controlling an LED (so far) on an arduino with node-red. I can get node-red to give me back the current LED state of HIGH or LOW, but my report block doesn’t send this to scratch. A little elaboration on how you would do this (it uses a get request to retrieve the state) would be awesome, but otherwise keep up the great work 🙂

Leave a reply to Devin Cancel reply