Filter results by

MQTT Subscribe and Publish

Having seen how messages can be sent and received through a publish-subscribe broker, we can use Node-RED to investigate the full power of this model.

In this section we delve deeper into Node-RED to:

  1. Utilize its debug node as a way to speed development
  2. Monitor a sensing device connected to the ARTIK 053 module by subscribing to its messages
  3. Add a function to the flow that can take the received messages and act on them.


With equipment set up and software services launched from where we left off in the previous section, we will additionally require:

  • A photoresistor and resistor (details below) for use with one of the ARTIK 053 modules.

Using Debug Capabilities of Node-RED

Along with the Inject input node type, the Debug output node type helps greatly in troubleshooting a flow. For a demonstration, we will start from the canvas as it was left after the previous exercise Sending MQTT Messages.

  1. Add debug capabilities to the existing flow on the Node-RED canvas.

    • Select an Output node: Drag a "debug" output node from the palette in the left pane onto the canvas, to the right of your Inject node.
    • Connection: Connect the nodes by dragging a second wire from the right side of the Inject node to the left side of the debug node.
  2. Run the flow. Click Deploy at the upper right to run this new flow.

  3. Look at the debug output. Click on the debug tab just under the Deploy button.

Now you can see the history of the messages being sent from the Inject node.

Returning Sensor Data from Edge Devices

Here we will connect a photoresistor to the ARTIK 053 module, which measures voltage at the sensor and publishes MQTT messages containing a corresponding value. Once again, the ARTIK 5/7/10 module acts as message broker to retransmit the messages to any interested subscribers (in our example, the Node-RED application).

  1. Follow the ARTIK 053 MQTT Publisher article to
    – Wire in a photoresistor and its power resistor
    – Build and load the example code to the ARTIK 053 module.

  2. Add an MQTT input node. Last time, you used an output node to send messages to the broker; this time, you will want to receive messages instead. So on the Node-RED canvas:

    • Select an MQTT input node.

    • Double-click it; give it the same localhost IP address:port as you used for the MQTT output node, and a Topic of 'brightness'.

  3. Add a debug output (just as you did in the previous exercise) and connect it to the new 'brightness' input node.

  4. Click Deploy.

In the incoming messages under the debug tab, the numeric value now changes according to the light reaching the photoresistor as indicated in the image below. Note that the canvas also shows a Function node, which we will add in the next exercise.

Adding Function Nodes to Act On Data

Now that the messages are known to be arriving, we can write a Node-RED function to process and act on them. To demonstrate the concept, we will create a "sunshine detector" app – it reads an incoming brightness value message, and generates an LED color output message based on chosen brightness ranges. Note that the Particle Build IDE code does not change; we can do all of this within the ARTIK – Node-RED environment.

  1. Build a message processing function node.

    • Select a Function node.
    • Double-click it and insert this simple JavaScript code.

        if (msg.payload < 700) 
        {msg.payload = 'RED';
        } else
        if (msg.payload < 1000)
        {msg.payload = 'GREEN';
        } else
        {msg.payload = 'BLUE';
        return msg;
    • Connect the Function node input to the 'brightness' node output (you can leave in place the other wire that goes to the debug node).
  2. Re-purpose your MQTT color output node (from previous exercises) as output for this function.

    • Delete the wire from the Inject node to the MQTT color node.

    • Connect the MQTT color node to the Function node output instead.

  3. Click Deploy. Now, the LEDs on the ARTIK module respond to the brightness at the connected sensor. Move your hand to block light to the sensor to go from "sunny" (BLUE) to "hazy" (GREEN) to "cloudy" (RED).

You will need to adjust the switch-over values depending on your component resistances as well as the brightness in the room.

Note that the subscriber and publisher tasks could just as easily reside on two separate ARTIK 053 modules, where one publishes the sensor value messages to the broker, and the other subscribes to LED color change messages from the broker.


Node-RED allows you to quickly create and debug an application that receives messages from edge devices, intelligently processes them, and sends back appropriate responses, all with very little programming. You can then embed your Node-RED flow directly within an end product. You can even run Node-RED from the cloud!

The real-world application of these principles is just as easy as what you saw. The task complexity is limited only by how much JavaScript function code you are willing to write. The ARTIK modules and MQTT seamlessly manage all of the messaging handling intricacy in the middle.

Last updated on: