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:

  • Monitor a sensing device connected to the ARTIK 053 module by subscribing to its messages
  • Add a function to the flow that can take the received messages and act on them.

Prerequisites

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.

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.

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.

      1
      2
      3
      4
      5
      6
      7
      8
      9
        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.

Summary

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: