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:
- Set up an ARTIK device with Mosquitto to become a simple MQTT broker.
- Use the Node-RED development tool to easily send and receive MQTT messages.
- Subscribe ARTIK 053 edge node devices to messages published to the broker.
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.
Log in through the terminal emulator to your ARTIK Linux device.
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 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.
Log in to your ARTIK Linux device (if you are not already logged in).
Make a note of the Wi-Fi network IP address (which we refer to as
localhostlater on). Enable Wi-Fi first with
dhclient wlan0as noted in Configuring Wi-Fi, then use this command to find the
Start Mosquitto (message broker) and run it as a background process.
Start Node-RED (node connection development tool) and run it as a background process.
Start a browser on your computer. Point it to the
localhostIP address of your ARTIK device that you noted previously, on port 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).
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.
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
localhostIP address of the broker, at port 1883. Enter a Topic of 'color'.
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.
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
– Make sure there is no proxy in your network.