Filter results by

ARTIK as MQTT Message Broker

MQTT has emerged as a standard messaging protocol for IoT. It can be used over TCP/IP networks and is very lightweight. The standard follows a publish-subscribe ("pub/sub") model; here, an ARTIK 5/7/10 module plays a key role on the network as the "message broker".

In this section, we will:

  1. Set up an ARTIK device with Mosquitto to become a simple MQTT broker.
  2. Use the Node-RED development tool to easily send and receive MQTT messages.
  3. Subscribe ARTIK 053 edge node devices to messages published to the broker.

Prerequisites

You'll need the following resources.

  • ARTIK 5, 7, or 10 development board with Linux OS
  • One or two ARTIK 053 modules with Tizen RT OS
  • Wi-Fi® access point

You'll be using the ARTIK IDE to compile the code for ARTIK 053 modules, so you'll need to have a least tried out an initial project before tackling this one.

Initial Setup

Install the Node.js JavaScript environment, the NPM package management tool, and the Node-RED visual wiring tool on your ARTIK board. This setup is only needed the first time, before starting your first session. The procedure assumes you have network connectivity. Refer to Ethernet and Wi-Fi for details.

  1. Log in through the terminal emulator to your ARTIK Linux device.

  2. Enter the commands noted. The downloads may take some time to complete. Depending on module type and firmware image revision, you may find that many of these are already pre-loaded on your system.

    dnf update
    dnf install node
    dnf install mosquitto
    dnf install npm
    npm -g install node-red

Starting a Session

You must use Wi-Fi for your network connections for these procedures to work.

  1. Log in to your ARTIK Linux device (if you are not already logged in).

  2. Make a note of the Wi-Fi network IP address (which we refer to as localhost later on). Enable Wi-Fi first with dhclient wlan0 as noted in Configuring Wi-Fi, then use this command to find the wlan0 address:

    ifconfig wlan0

  3. Start Mosquitto (message broker) and run it as a background process.

    mosquitto &

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

    node-red &

  5. Start a browser on your computer. Point it to the localhost IP address of your ARTIK device that you noted previously, on port 1880.

    Example: http://192.168.1.5:1880

    Note: Your computer must be connected to the same Wi-Fi network as the ARTIK Wi-Fi.

ARTIK is now set up as a broker and is also running Node-RED, so we are ready to set up publish-subscribe messaging.

Sending MQTT Messages

This section sets up a periodic transmission of the string 'RED' to an output node that sends them under the Topic of 'color' to the ARTIK message broker. ARTIK will "publish" this information to any interested "subscribers" (to be set up in the next section).

  1. Create a flow on the Node-RED canvas.

    • Select an Input node: Drag an "inject" input node from the palette in the left pane to the middle of the canvas (it initially shows "timestamp").

    • Select an Output node: Drag an "MQTT" output node to the right of the first node. This node will be sending messages to the broker via MQTT.

    • Connection: Connect the 2 nodes by dragging a "wire" from the right side of the inject node to the left side of the MQTT node.

  2. Configure each node by double-clicking on it.

    • Inject node: Set Payload to string; enter 'RED' as its value. Set Repeat to a 10s interval.

    • MQTT node: Configure the node to send a message by entering the localhost IP address of the broker, at port 1883. Enter a Topic of 'color'.

  3. Run the flow. Click Deploy at the upper right to run this new flow.

Node-RED is now sending 'color' messages to the broker every 10 seconds. We can next set up edge devices and configure them to be subscribers to these messages.

Subscribing Edge Devices

We will use ARTIK 053 modules as our edge devices. Follow this MQTT subscriber tutorial to set up each ARTIK 053 starter kit board as a subscriber. You don't need to do the "extra credit" part at this time.

Now that the ARTIK 053 modules are flashed and subscribing to the broker, they will receive the color message and power their LEDs as commanded. Try changing the color on the Inject node to BLUE or RED and notice that all subscribed modules change their LEDs accordingly.

Summary

In this tutorial, you have seen how messages can be sent and received through a broker in a publish-subscribe fashion. This simple architecture can scale up and out to a variety of scenarios through a number of different of network topologies.

Node-RED is a powerful development tool. We present some more advanced examples of its use in the next article.

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: