Overview

Skill Level: Any Skill Level

Beginner to advanced

Ingredients

  • Bluemix account
  • Raspberry Pi or some other device the can run Node.js

Step-by-step

  1. Setup

    Connect the device to IBM IoT platform. Follow this tutorial: https://developer.ibm.com/recipes/tutorials/raspberry-pi-4/

    Make sure you have your own instance of IoT Platform (registered), not quickstart.

  2. Subscribe for commands

    Subscribing for commands is not the main focus of this tutorial, thus we use a simple and fast method with Node-RED.

    Create a new flow or just add a new tab to the one from this recipe

    Configure the three nodes as follows:

    Watson IoT input:

    Reuse your credentials for IoT. Subscribe to the command named “list”.

    Simple function node:

    Move the data we have in args to the payload object. This makes it consumable by the the exec node.

    Exec node:

    Execute our list command with the Linux command “ls”. We pass the the aguments in payload.

    Push the deploy button and open the debug pane.

  3. Create Bluemix app

    • Create a new cloud foundry app and chose SDK for Node.js.
    • Bind your already existing IoT plarform service

  4. Download and deploy the application

    • Open the “start coding” page of the app we just created.

    • Consider this as step 1 from the start coding page. Instead of the mentioned starter code we use this IoTSendCmd project.
    • Follow all the steps until the app is running
    • Open the app by clicking on its URL.
  5. Send a command

    Open the app.

    • Click connect and watch the logging area. The app connects to the IoT plattform using the credentials
      provided by VCAP_SERVICES. Check the “Environment Variables” menu iten above “Start Coding”.
    • Enter values for all fields. Unlike MQTT subscribe, publishing commands does not support wildcards.
    • If all went well, you should see some output on the debug pane of Node-RED.
  6. The code

    Using MQTT with Javascript (Node.js) is fairly straightforward. Take a closer look at this code: https://github.com/cloud-dach/IoTSendCmd/blob/master/sendcmd.js

    It uses a node module called “mqtt”. This module provides funtions for connecting to a mqtt broker:

    Once connected, the client can send commands by using the publish function.

Join The Discussion