Using Altair SmartCore nodes in Node-RED

First Steps

Introduction

In this tutorial you will learn how to use some nodes of Node-RED, a flow based programming tool for the IoT.

For a deeper explanation of Node-RED, please go to the official documentation.

Installation

The first step is to install node.js and npm.

If you are using Windows download the installer from Node.js website and follow the steps specified in the Node-RED documentation site. If you are using Linux install it with your favourite package manager for example:

sudo apt-get install nodejs & npm

Next, install Node-RED:

npm install -g --unsafe-perm node-red

Finally, install our Altair SmartCore custom nodes. To do so, go to your Node-RED folder and execute:

npm install node-red-contrib-carriots

First Project

Run “node-red” on your console, open your browser and access “localhost:1880”.

Your browser will show something like this:

  • Node-RED workspace

From now on, you can drag and drop the nodes of the left list and use them as you wish. In this simple example we will send a data stream to Altair SmartCore.

List of nodes

To do this you will need 3 nodes. An “inject” node to inject the data that we want to send. A “send stream” node (from Altair SmartCore custom nodes list) that will make a post request to Altair SmartCore API without a single line of code. Finally a “debug node” that will show the result of the request on its debug window. Drag and drop this 3 nodes to your flow.

Project development

First of all we will create a JSON document with the “inject” node. To do this drag and drop the node into the flow area and double click on the dropped module to edit it. On the edition form select the JSON mode and introduce the desired JSON document in the “Payload” field.

  • Inject node

Next we have to configure the Altair SmartCore send stream node. You will find some fields that you have to fill. The “Name” field is used to personalize the name of the node on your flow: if you don’t want to have a node called “Send Stream” and want to have a node called “Altair SmartCore is fantastic!” you can do it filling this field. If you do not know what the other fields mean, please refer to Altair SmartCore documentation.

  • Send Stream node

Then connect the send stream node to the inject node.

You can check if the node can send streams connecting it to a debug node:

  • First project nodes

Finally, you must “Deploy” the Node-RED flow by clicking on the red button on the right of the screen. Once deployed, you can click on the blue button of the inject node to send the configured stream to Altair SmartCore.

The response message will be received on the console of our debug node:

  • Checking the response

Version Conrol of the Configuration Files

Introduction

Now let’s build a Node-RED flow to manage the config files of our devices. Please go to Altair SmartCore documentation to know more about how Altair SmartCore performs hardware management through device files and status streams.

In this simple example we will make 2 requests: one to send the version of the device file we want to check into a status stream, and another to download the file if necessary. To do this we’re going to use the Altair SmartCore “send streams” and “get file” nodes.

The logic of this example is to download a device file from Altair SmartCore only if the version of the checked file is different from the current one.

List of nodes

  • "inject node" (where we will configure a JSON with the filename and version to check).
  • "send streams node" (to post the status stream to Altair SmartCore).
  • 2 x "template node" (where we will get the value of the response).
  • 2 x "debug node" (one for the template and another for the result of the post configfile node).
  • "switch nod"e (to check if we need to download the new device file).
  • "get file node" (where we will download the new file).
  • "file node" (to configure the path where we want to store the downloaded file).

Project development

Let's assume we have in our Altair SmartCore account a device with an associated file called “f42. We want to check if we have the latest version, otherwise we will download it. Let's assume again we have the version “1.0” in our system. So in the “inject” node we will configure a JSON payload and fill it with {“f42”: “1.0”}

  • Inject node configuration

Connect the “inject” node to a “send streams” node and fill it with your Altair SmartCore Apikey and your device id. developer (Remember to change the “Type” field to status).

  • Send stream node configuration

Now drag and drop to the flow screen a template node where we will specify which part of the data received we want to use. In this case, we only want to analyze the response:

  • Template node configuration

Next we’re going to use a “switch” node to only post a new config file only if the version returned in the response is different from 0. Connect the swtich node to the template node and configure it with:

  • Switch node configuration

Then connect the switch node to a “get file” node where you have to fill the edit form fields with your Altair SmartCore data. If you want to get the most recent version of the file leave empty the version field.

  • Get file node configuration

Connect the output of the “get file” node to a new “template” node and configure it like the following image:

  • Final template node configuration

Finally drag and drop an “output file” node. Configure it with the path and the filename where you want to store the file and connect it to the last “template” node that we just added.

  • Output file node

If necessary, drop and connect some debug nodes to check the results that you are getting.

The final result would be something like this:

  • Final workspace

Now Deploy the flow and click on the button of the “inject” node. If there is a device file in Altair SmartCore, associated with the specified device and its version matches the one entered in the status stream, the first “template” node will receive the value “0”. Otherwise I t will receive the value “1”, the file will be downloaded and stored in the path configured in “file” node.

  • The version of the file on the status stream isn't the actual version of the file.
  • The version of the file on the status stream is the most recently version of the file.

Use our Forum or the Support & Contact section if you need help, we love to hear from you.

Enjoy Altair SmartCore and Node-RED!