Sending commands from Azure IoT Central to Raspberry PI with Sense HAT in node-RED.

In the previous post of this series you have seen how send telemetry from a Raspberry PI with a Sense HAT to Azure IoT Central using my Node-RED connector, you can see the code on my Github repo. Well, now it’s time to see how to send commands to the Sense HAT. In our case we would like to turn on the LEDs (in green color) and turn them off.

First we have to modify our interface in Azure IoT Central to add two commands turnLedOn and turnLedOff, remember that, as in the previous post for telemetry, names are important. Here how my new interface looks like after the change:

After you updated the interface you have to publish with a different name from the one you used before.

You can download the new interface updated with commands from here.

Now go in your device section and press “Migrate” to change the interface with the new one (in my case from v2 to v3) of your Raspberry Device.

Back in Raspberry PI Node-RED environment you have to build something like this:

You can download the flow from here, but look on how it’s built in the following sections.

Azure IoT Central node is configured as in the previous post, but you have to add the name of the two commands created in the interface above:

Now the most important part lives in the JavaScript node where you have to define the two functions that will respond to the commands and make them available to the flow context, the Node-RED connector for Azure IoT Central will do the rest for you.

Here the sample code used to turn on and off LEDs.


// Create a commands with the same name 'turnLedOn' and 'turnLedOff'
// in the Azure IoT Central node
function turnLedOn(request, response) {
  node.log('Received asynchronous call to turn on LED');
  msg.payload = "*,*,green";
  response.send(200, (err) => {
    node.send(msg);
    if (err) {
      node.error('Unable to send method response: ' + err.toString());
    }
  });
}

function turnLedOff(request, response) {
  node.log('Received asynchronous call to turn off LED');
  msg.payload = "*,*,off";
  response.send(200, (err) => {
    node.send(msg);
    if (err) {
      node.error('Unable to send method response: ' + err.toString());
    }
  });
}

// Function handlers
flow.set('turnLedOn',turnLedOn);
flow.set('turnLedOff',turnLedOff);

In the code above just note that to change the color of the LEDs we simply pass a string “*,*,green” to the Sense HAT node. The full syntax it’s here for the output Sense HAT node.

Now, from the interface of Azure IoT Central, go to your device, and a new menu for commands is displayed with the two method, as in the following image.

If you press Run for turnLedOn you should see all the LEDs of the HAT turn to green! And of course if you press turnLedOff all the LEDs turn off. See the video below of the result on my device.

Summary
In this second post you have seen how to use the Node-RED Connector for Azure IoT Central to send commands from Azure IoT Central to the Raspberry PI with a Sense HAT. Let me know if you need more samples!

Happy coding!

Published by Pietro

Passionate Developer, family man, working at Microsoft, the creator of a beautiful Kids' app and a community contributor.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: