Tutorial: Controlling an Artik board using FRED and Node-RED

with No Comments

This tutorial shows you how to use FRED — a cloud based Node-RED — to connect to the Samsung Artik hardware modules for remote control and visualization. We make use of the node-red-contrib-artik and node-red-contrib-fred nodes on the Artik board and on the FRED cloud-hosted Node-RED service.

untitled-diagram-1

As you can see, this tutorial will demonstrate how we can write a Node-RED flow running on the cloud service (FRED) that communicates directly to a Node-RED flow on a Artik module, which then reads and writes to the Artik I/O pins using the new Node-RED nodes for Artik we recently released. We’ll use these new nodes to turn on and off a LED, to query the status of a switch, to read analog voltage value and to control a motor – all from the cloud!

The FRED nodes make it easy to connect devices to FRED – once setup and running, you can wire up a flow on any device that ends with a FRED node, and messages are then sent automatically to a FRED input node in the cloud.  With these nodes you can build flows that span devices and cloud.

You can find information on how to install the FRED node on your Artik board in Tutorial: Connecting device Node-RED to cloud Node-RED. This tutorial will focus on the setup and usage of the Artik nodes on your Artik hardware modules.

The sample flows can be found in: https://github.com/SenseTecnic/tutorial-samples/tree/master/Artik-nodes-sample

If you are not sure how to import a sample flow, please refer to: http://developers.sensetecnic.com/article/how-to-import-a-node-red-flow/

If you’re new to Node RED you might find our Introduction to Node RED tutorial very helpful.

Create a FRED Account

To If you don’t already have an account on FRED (Cloud Node-RED) set one up now. It’s free for a demo account, which can be used for these tutorials and any other experiements you may have with Node-RED. Register for a free account at http://fred.sensetecnic.com.

After registering, make sure to activate your account via your email. You will not be able to login until you validate your account.

About the Samsung Artik modules

The Samsung Artik modules are integrated systems designed for the Internet of Things. They are designed to power a full range of IoT applications quickly with the capability to integrate with Artik Cloud or any other cloud service. Artik modules can also be scaled from development to production easily. The current release models range from the Artik 1 board, a low power IoT sensor board through to the Artik 520, Artik 710 and Artik 1020 modules which have more processing power and capabilities. More information can be found on the Samsung Atrik developer website.

 


About Sense Tecnic: Sense Tecnic Systems Inc have been building IoT applications and services since 2010. We provide FRED, cloud hosted Node-RED as a service to the community. We also offer a commercial version to our customers, as well as professional services. Learn more.


 

Overview of steps in the tutorial

This tutorial will talk you through the following steps

  1. If you don’t already have a free FRED account, set one up
  2. Ensure you have Node-RED running on your Artik module (Artik520/710/1020) and install the new nodes that interface with the Artik hardware
  3. Test the new Artik nodes under Node-RED by wiring up and LED and turning it on/off using a simple Node-RED flow
  4. Connect the Artik module to the Cloud using the FRED nodes and the FRED service, control the same LEDs from a Node-Red flow in the cloud
  5. Read the GPIO pins, gather interrupts and send their status to the cloud
  6. Remotely read the status of the ADC pin and graph the results in the cloud
  7. Connect a motor to the Artik board and control it from your Node-RED flow in the cloud

 

Installing Artik Nodes

First, ensure Node-RED is installed on your Artik board. For more information on how to do this see http://nodered.org/.

Once you have Node-RED installed on your Artik modules, simply go to the home directory of your Artik board, and cd into the Node-RED configuration directory.  By default this is 

1
~/.node-red

 There, run the following command in the terminal:


1
npm install node-red-contrib-artik

Start your Node-RED on the Artik module like normal, and you should see the Artik nodes listed in the nodes container.

screen-shot-2016-09-27-at-11-40-41-am

As you can see, we have provided 4 new nodes to interface to the Artik 520/710//1020 modules. The artik in and artik out nodes read and write the GPIO pins on the Artik modules. The artik adc node access the ADC pins and the artik pwm node allows control of the PWM pins.

Note: the Node-RED Artik nodes we have developed are fully tested on Artik 520, 710 and Artik 1020 modules but can’t be used on the Artik 1 module.

Using the Artik Nodes

In this section we will show you how to use the artik nodes with FRED to remotely controls your hardware from the FRED server.

Artik Out Node

Artik Out node is a node that sets a GPIO pin in the OUT direction and you can set the output state to HIGH or LOW.  We will first show you how to setup the Artik Out node on your device.

Assume we have a LED on a breadboard that is connected to an Artik 1020 module, and we would like to remote control the LED over the cloud. Here is how the LED is connected to the Artik 1020 module:

artik-out-led

As you can see this is a rather simple circuit. Now we will run Node-RED on Artik 1020, and drag the Artik Out node onto the canvas. By double clicking on the node, we can config the Artik Out node as following:

In the Name field, you can name the node however you like to assist you organizing the flows. In the Platform field, we will select Artik 1020 as the one we are using for this sample. In the Pin field, we will select Pin 13. Details of pin mapping can be found on the official Artik document. Then, in the State field, we can set the output state to HIGH or LOW. You can also set the initial state by checking the checkbox for “Set initial state?“. You will be prompted to set the initial state to HIGH or LOW, which will be the state of GPIO pin right after you deploy the flows. Notice the Artik Out node is set to active high, so the HIGH state in the config tab will lead to a HIGH GPIO output and visa versa.

Let’s try out the settings on local device first. Have an inject node in front of the Artik Out node to activate the node action, and the Artik Out node configured to the following:

What this does is, when you deploy the flow, the LED will be on; when you hit the inject node to trigger the action of the Artik out node, it will turn off the LED. Try it!

Controlling the same LED from the CLOUD

Once you have the node working on local device, we will try something more exciting.  Let’s drag a FRED in node onto the canvas and connect to the Artik Out node as shown in the following:

screen-shot-2016-09-27-at-12-43-01-pm

Now in the FRED in node config tab, we will create a new Endpoint called “artikOut”, pointing to the username as “demotutorial” ( replace this with your actual username! ). If this is your first time using the FRED in/out nodes to connect the FRED cloud to a device then details on how to set up FRED nodes can be found in this tutorial.

screen-shot-2016-09-27-at-12-43-45-pm

Now, we login to the FRED cloud server on https://fred.sensetecnic.com/ (In our tutorial, the username is demotutorial). Set up a fred out node with the same endpoint, with two function nodes in front of the fred out node.

screen-shot-2016-09-27-at-12-49-33-pm

In this flow, each of the two function nodes will return a msg object to set the state of the GPIO pin to either HIGH or LOW. Simply enter this piece of code in the HIGH node:

msg.payload = {
    state:1
}
return msg;

and this in the LOW node:

msg.payload = {
    state:0
}
return msg;

Once you have these flows setup and deployed, you should be able to see that when you inject the HIGH msg on your FRED instance, the LED on breadboard is turned on; when you inject the LOW msg on your FRED instance, the LED on breadboard is turned off. We’ve set up the remote control on the LED!

Artik In Node

In this section we will show you how to use the Artik nodes with FRED to remotely read the status of a GPIO pin, and also send an interrupt from the hardware to the cloud server.

First, let’s try the Artik In node on local device. We will set up the circuit board like this:

artik-10-in

As you can see, we will be using a circuit that is similar to the example from the Artik Out node, but add in a push button. Whenever the pushbutton is pressed, the current will flow through the circuit and the LED will be turned on.

Now, drag the Artik In node onto the canvas of Node-RED, have an inject node in front of the Artik in node, and a debug node after the Artik in as shown below.

screen-shot-2016-09-27-at-2-13-46-pm

Double click on the Artik In node and we will set the configs.

Again, since we are using Artik 1020 for this sample, Artik 1020 is selected in the Platform field, and pin 12 would be the pin number that we are using in the Pin field. If you are using Artik 520, you would select Artik 520 in the Platform field and have the same pin number in the Pin field.

If you check on the tick-box for “Enable interrupt?“, interruption mode will be turned on and you will see output message whenever a change in the state is detected. Edge detection options include “rising”, “falling” and “both”. You can also set the debounce delay index in the Debounce delay field, where the time duration is in milliseconds.

Once you deploy the flow, you should see the output message in the debug tab when you pressed the button as:

{"state": 0, "interrupt": true}

This message indicates that this GPIO pin reads a LOW and it is triggered by an interrupt. If you release the button, you should see another output msg as

{"state": 1, "interrupt": true}

showing that this GPIO pin reads a HIGH, and it is triggered by interrupt because we set to edge detect to both the rising and falling edges.

We can also manually read the GPIO status by clicking the inject node, in this case, it will return something like:

{"state": 1, "interrupt": false}

Now the Artik in node is set up on local device, we are ready to move on! Let’s set up a flow like this on a canvas of the device:

screen-shot-2016-09-27-at-2-38-43-pm

In this flow, we start with a FRED In node that is set to listening to the endpoint “artikInSend”. Followed by a json parsing node, the flow connects to the Artik In node in which we keep the configuration same as earlier. We have a function node right after the Artik In node where we delete the

1
msg._session

in the output msg from the Artik In node. The reason for doing this is, when a message is received by the fred in node on the Artik module via endpoint “artikInSend”, a 

1
msg._session

 was created for specifying the connection session; if this 

1
msg._session

is not deleted, the fred out node with endpoint “artikInReceive” might consider it as a duplicated msg, and thus not send the message back to the FRED cloud server.

Here is the code inside the function node:

delete msg._session;
return msg;

At the end, we set up a FRED Out node that sends the msg back to the FRED server via endpoint “artikInReceive”.

Now, let’s go back to the FRED cloud server at https://fred.sensetecnic.com/, and create a flow like this:

screen-shot-2016-09-27-at-2-45-52-pm

This simple flow does two things:

  1. When the inject node is clicked, a request is sent to the Artik board via endpoint “artikInSend”
  2. it listen to all output from the endpoint “artikInReceive”.

Once the FRED nodes are successfully connected, you can access the GPIO pin status on an Artik board remotely and also receive an interruption msg from the Artik board as you press the pushbutton!

Artik ADC Node

In this section we will show you how to use the Artik nodes with FRED to remotely access an ADC pin and visualize the data onto a dashboard.

There are two ADC pins on each Artik 520 and Artik 1020. Be aware that the locations for these GPIO pins are different on Artik 520 and Artik 1020 modules. For more details on the pin mapping and ADC reading range, please refer to the Artik documents.

Here is a setup of our sample circuit. We are using an analog proximity sensor where it will return an analog voltage output indicating the distance between an object and the sensor. A sample analog proximity sensor can be found in this link.

artik-adc

In this sample, we have the proximity sensor connected to the 5V power supply and the ground pin. We also have the analog voltage output connected to the ADC0 pin on the Artik module. Depends on the maximum voltage output of the sensor you are using, you might need to use external resistor divider as the ADC pin operates only from 0 to 1.8v on Artik 1020.

Since the Artik ADC node is very straight forward, we will move directly to the remote access part. Similar to the Artik In node, here is how we are going to set up the flow on the Artik module:

screen-shot-2016-09-28-at-12-19-31-pm

As you can see, we inserted a FRED in node that listens to the FRED cloud server via endpoint “artikADCSend”, and have a FRED out node that sends back responds via endpoint “artikADCReceive”. The function node “delete session” is the same as previous. Double click on the Artik ADC node, and we can set the configuration like this:

As you can see, you only need to set the ADC pin number and the platform then you are good to use. In this example, we have ADC0 connected to the analog voltage output of the sensor, so we entered 0 in the Pin field. And obviously Artik 1020 is selected as we are using an Artik 1020 module. The unit of the output value from this Artik ADC node will be in mV.

Now on the FRED cloud server side, we will set up the flow like this:

screen-shot-2016-09-27-at-4-04-09-pm

As you can see, we have an inject node that triggers action on the Artik module by using a FRED out node, via the endpoint “artikADCSend”. We also have a FRED in node that listens to the responds from Artik module via endpoint “artikADCReceive”. In this example, since we also want to be able to visualize the data flow, we added in a UI chart node at the end where it will record the data from the Artik ADC node.

Let’s set the inject node to inject in a time interval. For example, we would like to poll data from the sensor every 5 seconds, we will set the time interval in the config tab of the inject node:

screen-shot-2016-09-27-at-4-17-53-pm

when we hit deploy, you would be able to see the data from the Artik ADC node shown on a chart:

screen-shot-2016-09-27-at-4-17-35-pm

For more information on using the UI chart node, please refer to Tutorial: A Node-RED dashboard using node-red-contrib-ui.

Artik PWM Node

In this section we will show you how to use the Artik nodes with FRED to remotely control a PWM pin.

Let’s connect the Artik module to a micro servo like this:

artik-pwm

In this simple set up, we only need to control the PWM output from a PWM pin. Let’s try it out on local first.

Let’s drag an Artik PWM node on the canvas, with an inject node connected in front so we can trigger action on the PWM node. Double click the Artik PWM node and have the settings as above.

In this example, we are using PWM0, so we put down 0 in the Pin field. We check the tick-box to set initial state of the PWM node, and enter these values for duty cycle and period in nanosecond.

After you deploy the flow, you should see the servo spinning right away. If you hit the inject node, you should see the servo spins in the opposite direction.

Now we are ready to move onto using the FRED cloud server. Let’s change the local flow to this:

screen-shot-2016-09-27-at-10-02-21-pm

As you can see, we are using a FRED in node that listens to the endpoint “artikPWMSend”. In the Artik PWM node, specify the PWM pin you would like to use, and set the initial state if you want to.

Now, on the FRED cloud server, let’s set up a flow like this:

screen-shot-2016-09-27-at-9-59-28-pm

As you can see, we have a FRED Out node that sends the message object to Artik module via endpoint “artikPWMSend”. We have three function nodes that return different parameters to the Artik Module.

The code for the “on” function node is:

var newMsg = {
    payload:{
       state:1,
       period:1000000000,
       dutyCycle:500000000
    }
}
return newMsg;

The code for the “off” function node is:

var newMsg = {
    payload:{
       state:0
    }
}
return newMsg;

The code for the “other way” function node is:

var newMsg = {
    payload:{
       state:1,
       period: 1000000000,
       dutyCycle: 1000
    }
}
return newMsg;

If you would like to know the meaning of these values for period and duty cycle, you can look up how PWM works.

Once you deploy the nodes, you will be able to remotely control the servo to spin in either direction and stop via our FRED cloud server.

Now you’ve got the basics of these Artik nodes sorted out, play around with the nodes and see what you can do with them on your Artik modules!


About Sense Tecnic: Sense Tecnic Systems Inc have been building IoT applications and services since 2010. We provide FRED, cloud hosted Node-RED as a service to the community. We also offer a commercial version to our customers, as well as professional services. Learn more.