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, ARTIK 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 edge node devices to messages published to the broker.

Prerequisites

  • ARTIK 5, 7, or 10 development board
  • One or two Particle Photon modules
  • Wi-Fi® access point

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 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 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 Node-RED (node connection development tool) and run it as a background process.

    node-red &

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

    mosquitto &

  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 Particle Photon modules as our edge devices. Follow the Particle docs for registering Photons.

  1. Write the MQTT subscriber app for the Photons in the Particle Build IDE. You can find sample code here.

  2. Make changes as needed, including the correct IP address at byte server[] .

  3. Click Verify to check your code.

  4. Click Save to keep your changes.

  5. Click Flash to send the code to the Particle Photons.

If all goes well, you will see the LED color change to red on the Photon modules.

Troubleshooting Tip. Not working? Remember that all parties must use the same localhost network IP address.
– On each Photon module – use the Setup procedure of the Particle app
– In your Particle build code – byte server[] = { x, x, x, x }
– On the ARTIK board – in the wpa_supplicant.conf file
– For the URL of the Node-RED page – xxx.xxx.xxx.xxx:1880
– For the MQTT output node server address – xxx.xxx.xxx.xxx:1883

Now that the Photons are flashed and subscribing to the broker, they will receive the color message and change their LEDs to red. Try changing the color on the Inject node to GREEN or BLUE and notice that the Photons 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: