Filter results by

ARTIK Advanced MQTT Topics

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 Photon by subscribing to its messages
  3. 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 Particle Photon 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 Particle Photon module, which measures voltage at the sensor and publishes MQTT messages containing a corresponding value. Once again, the ARTIK module acts as message broker to retransmit the messages to any interested subscribers (in our example, the Node-RED application).

  1. Wire the photoresistor and its power resistor as shown here.

  2. Flash the sample code you find here. It builds on the existing code by configuring connections for the photoresistor and its power resistor, and then just adds these two simple code lines to the main loop() to publish the 'brightness' value:

    1
    2
     analogvalue = String(analogRead(photoresistor));
     client.publish("brightness", analogvalue );
  3. 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'.

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

  5. 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 < 300) 
        {msg.payload = 'RED';
        } else
        if (msg.payload < 450)
        {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 LED on the Photon module responds 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).

Note that the subscriber and publisher tasks could just as easily reside on two separate Photon 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.

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 module and MQTT seamlessly manage all of the messaging handling intricacy in the middle.

Last updated on: