Filter results by

Introduction to Node-RED

Node.js is a run-time interpreter of JavaScript packages and has become popular for development. You will quickly come to rely on it if you have not already! Getting you familiar with node is best done by example.

Node-RED really shows off the versatility of node and is emerging as a leading means of developing IoT applications. It's a form of "drag-and-drop" programming.

In this section, we will:

  1. Check that your system has node installed, and if not, show you how to install it
  2. Install Node-RED as your first node application
  3. Show you how to expand Node-RED for some ARTIK-specific fun!

Prerequisites

  • ARTIK 520, 530, 710, or 1020 development board
  • Wi-Fi® access point

Initial Setup

Your system arrives with the Node.js JavaScript environment installed. Here we will install the Node package management (npm) tool and the Node-RED visual wiring tool. This setup is only needed the first time, before starting your first Node-RED session.

  1. If you have not done so, set up network connectivity so that you can download packages. Refer to Configuring Wi-Fi for details.

  2. Log in through the terminal emulator to your ARTIK device.

  3. Enter the commands noted. The downloads may take some time to complete. Unless you have an older firmware image revision, you'll see that node is already pre-loaded on your system.

    1
    2
    3
    4
    5
    6
     dnf update  
     dnf install node  
     dnf install npm  
     npm -g install node-red
     npm -g install node-red-contrib-artik
     npm -g install node-red-contrib-artik-cloud

The npm lines are for node "packages". You have installed three: Node-RED itself and two "contributed" packages that depend on Node-RED. You'll see below how useful these contributions can be.

Having any problems installing packages with npm? Look here for some helpful hints.

Starting a Node-RED Session

Node-RED is a server running on ARTIK, and a Web browser on your host PC will be its client. Therefore, your computer must be connected to the same Wi-Fi network as the ARTIK Wi-Fi.

  1. Log in to your ARTIK system from the terminal emulator.

  2. Enable your ARTIK board for Wi-Fi as described in Configuring Wi-Fi.

  3. Make a note of the ARTIK Wi-Fi network IP address, which we'll refer to as localhost from now on. Use this command to find the wlan0 address:

    ifconfig -a

  4. Start Node-RED (node connection development tool) and run it as a background process.

    node-red &

  5. Start a browser on your PC hosting the terminal emulator. Point the browser to the localhost Wi-Fi IP address of your ARTIK device, on port 1880.

    Example: http://10.0.0.5:1880

    Your screen should look something like this:

ARTIK is now running Node-RED as a server, and your Node-RED "canvas" is being served as a Web page by ARTIK.

If you scroll down the nodes navigator on the left side, you should see these additional nodes. They were added in the npm contributed installations above. We'll use the first four Sense-Tecnic nodes to interface with ARTIK programmable pins.

Developing Node-RED Applications

Remember the Blink an LED tutorial? We're going to revisit that now, but this time we'll make it a lot easier.

Turn on LED

  1. Set up your LED circuit as described here, according to the module type you have.
    • ARTIK 520 or 1020: Use external pin [11].
    • ARTIK 530 or 710: You've already got an on-board LED, so no setup is needed.
  2. Select an "Inject" Input node:
    • Click on and drag an inject input node from the palette in the left pane to the canvas ("inject" changes to "timestamp").

  3. Set up a node to control a GPIO pin as an output.
    1. Click on and drag an artik out node to the canvas.

    2. Double-click to configure it. In the Edit dialog:

      1. Select ARTIK 5, 7, or 10 as target platform (choose ARTIK 7 if you have an ARTIK 530).
      2. For ARTIK 520 or 1020, choose “Pin 11” as digital output pin.
        For ARTIK 530 or 710, choose either LED.
      3. Click Done.
  4. Connect the nodes by dragging a "wire" from the right side of the inject node to the left side of the output node.

  5. Run the flow by clicking Deploy at the upper right. You should see the message below.

  6. Watching the LED, trigger the action as indicated above.

As you can see, your Node-RED canvas drawing is now in control of the ARTIK GPIO pin. It was just that simple!

Troubleshooting Tip. Something not working? Remember that all parties must use the same localhost network connections.
– On the ARTIK board – Wi-Fi router named in wpa_supplicant.conf
    with IP address read back from the ifconfig wlan0 command
– On your host PC – connected to the same-named Wi-Fi router
– In the URL of the Node-RED page on your host PC –
   xxx.xxx.xxx.xxx:1880

Flash LED

Let's make the flow more sophisticated, by inserting code to turn the LED on and off periodically.

  1. Click on and delete the wire you used to connect the two nodes.

  2. Double-click the inject node (still labeled "timestamp") to configure it.
    1. Set Repeat to 'Interval', and make it repeat the flow every 1 second.
    2. Click Done.
  3. Click on and drag a function node to the canvas.

  4. Double-click the node to configure it.

    1. Copy and paste the code below into the Function space.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      var state = global.get('state')||0;
      if (state === 0) {
          global.set('state',1);
      } else {
          global.set('state',0);
      }
      msg.payload = {};
      msg.payload.state = state;
      return msg;
    2. Click Done.

  5. Wire them all together to look something like this.

  6. Click Deploy to run this new flow.

Now you should see the LED flashing at the rate you selected.


How It Works

In the function node, we use a global variable ‘state’ to track the state of the output pin. We just invert the sense every time, and pass the new state out in the payload message.

The artik out node is designed to accept external input of a msg.payload.state parameter, which if present will override the setting you made manually in the node. To see this and other functionality available in a node, just click on it, and look in the info tab to the right of the canvas.

Read Temperature Sensor

In this example, we are going to use an artik adc node to collect analog sensor data. Leave your previous LED circuit in place.

  1. Build the circuit using the TMP36 sensor as shown, with its center lead going to the analog input, its left lead to +5V, and its right lead to GND.

    • ARTIK 520 and 1020: Wire the leads to +5V, A0, and GND as shown.
    • ARTIK 530 and 710: Wire the leads directly to the Test Header. ADC0 is pin 39.
  2. Drag an artik adc node to the canvas, and double-click to set:
    • Target platform
    • “ADC 0” as the analog pin from which data will be read.
    • Name as "ReadSensor".
  3. Drag a function node to the right of the artik adc node, where we will convert the voltage reading from ADC 0 pin to a temperature. Double-click to set:
    • Name as “ConvertToTemperature”
    • Define the function as shown.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var voltage_raw = msg.payload;
    var voltage = voltage_raw * 2 * 0.439453125;
    	
    //Converting from 10mv per degree with 500mV offset
    var temperatureC = (voltage - 0.5) * 100 ;  
    var temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
    msg.payload = {
        "temperature": temperatureF
    };
    return msg;
  4. Wire up an inject node and a debug node to the beginning and end of the flow.
    • Configure the inject node to trigger data reading every 10 s; name it as “Init”.
    • The debug node shows the msg.payload info on the Node-RED Debug tab; name it as “DebugMsg”.

  5. Click Deploy. You will see the temperature updated on the debug tab every 10 s.

Summary

In this tutorial, you have seen how to bypass much of the coding needed for interacting with wired and wireless interfaces. Node-RED allows you to simply wire up the input and output nodes; you can focus your coding skills on the unique functionality in the middle.

In the next tutorial, we will use this same setup to stream temperature readings to ARTIK Cloud. We will use Node-RED again in that tutorial, and in the MQTT tutorial series to come.

Troubleshooting Tip. When you try to start a subsequent Node-RED session, do you get error messages but no canvas?
– Some of the saved nodes or settings may be corrupt. Copy elsewhere and then delete the files (but not the lib subdirectory) in the hidden /root/.node-red directory.
– Make sure there is no proxy in your network.

Last updated on: