Introduction: Watson IOT and Node-RED Controlled Robotics

The following Instructable is an IOT motor device that is controlled by an accelerometer based Controller through the Watson IOT platform.

Angling the controller adjusts the IOT motor device to match the pitch and angle of the controller. Being IOT based means the controller can move multiple IOT devices across the entire planet with a simple flick of the wrist.

Whilst this Instructable includes the basic detail regarding both the motor device (receiver) and controller, the focus of this step through will be on connecting to the Watson IOT platform and Node-RED.

I have created another instructable which focuses on the hardware (i.e accelerometers, servos, arduinos) and it can be viewed here.

In-depth detail regarding the physical components (Servos, accelerometer, etc..) can be found on my website. These devices are purely designed to provide and receive data so that you can understand how to communicate with the Watson IOT platform.

Why use the IBM Watson IOT platform?

  • To quickly build and deploy IOT services for your devices to connect to
  • Immediate Scalability
  • Connectivity to other IBM Watson services like predictive analytics, cognitive services, data storage and more.

Step 1: Component and Application Overview

The above diagram gives an overview of how the system hangs together in its entirety and the following is a breakdown of each item.

The Controller

The controller is a simple device that sends the X,Y, and Z axis data from an accelerometer to Watson IOT platform using the MQTT protocol.

Components Include:

  • Arduino Mega 2560
  • Ethernet Shield
  • Acceleromete

MQTT

This is the protocol used to communicate directly with the Watson IOT platform. It is a lightweight protocol perfect for fast data transfers on IOT devices.

Devices Publish “Events” (like the axis data this project sends) and Subscribe to “Commands” from applications.

  • Note: Devices can not subscribe or publish directly to other devices unless they are registered as a gateway device. An Application is needed in between two devices.

This “Publish / Subscribe” methodology removes the one to one relationship between devices and creates the ability to scale fast.

The Node-RED Application

You can use any number of applications to receive the Controller data and send commands to the receiver, but in this case I have used Node-RED.

Why did I use Node-RED?

  • I can set it up and deploy within minutes within the IBM Bluemix platform
  • The interface is drag and drop with minimal coding required

The Receiver (IOT motor device)

This purely a Ethernet based Arduino (Freetronics EtherTen) that receives the data passed from the Node-RED application and sets the X and Y Servo’s to the desired position.

Components Include:

  • Freetronics EtherTen Arduino
  • AdaFruit Motor Shield
  • 2 x Servo motors

Step 2: Set Up an IBM Bluemix Account and the Required Services / Applications

The IBM Bluemix platform is the one stop shop for all your cloud based services and applications to get your projects up and running fast.

http://www.ibm.com/bluemix

Once you have access to your Bluemix account we are going to set up:

  • A Watson IOT Service instance
  • A Node-RED Application instance


A. Setup Up a Watson IOT Service

  • Using the Hamburger Menu on the left go to “Service” > “Internet Of Things”
  • At this point you can create a “Project” or just the “Service” – create a “Project” to house all of the pieces of this project.


B. Setup a Node-RED Application

  • Using the Hamburger Menu on the left go to “Apps” > “Cloud Foundry Apps”
  • Click "Create Cloud Foundry App"
  • Go to “Boilerplates”. These are like templates to get you up and running fast.
  • Select “Node-RED Starter”

Step 3: Attach Devices to Your IOT Service

Your IOT devices will require an authentication token to communicate with your IOT services and applications.

  • Go to your newly created Internet Of Things service within your Bluemix account and click on the “Connect Your Devices” or “Launch Devices Dashboard”
  • From the left side menu select “Devices” > Add device.

There are two steps to adding a device – adding the specific device detail, and adding a device type. Device types are saved and are reusable.

Device types help you group your devices.

For both device details and device types, you only need to enter a name. You can add as little or as much info as you like.

I let the system generate the Authentication Token for me.

IMPORTANT

Once the device is added you are presented the following detail:

-------------------------------------------------------------------------------------

Organization ID: kqhpmi

Device Type: Ethernet_Arduino

Device ID: Controller

Authentication Method: token

Authentication Token: &322(23gh4kcL+h

-------------------------------------------------------------------------------------

MAKE A COPY OF THIS INFO – you need to add it to your device and can not retrieve it again once you leave the confirmation page.

You will need to repeat the above for your Receiver Device as well.

Step 4: Generate an API Key for Your Node-RED Application

Just as your devices require an Authentication Token to access the IOT platform, your Applications will require an API Key to connect.

  • From within your “Internet Of Things” Service select from the left menu “Apps” > Generate API Key

IMPORTANT

As per the device authentication you will be presented with:

  • An API Key
  • An Authentication Token.

Keep these in a safe place as you will not be able to retrieve them again once you leave this page.

Step 5: Sending Data From the Controller

The attached Arduino sketch is provided as an example for the authenticating your device and communicating via the MQTT protocol to the Watson IOT service.

I’ll break out the important sections of the code here.

Topics

--------------------------------------------------------------

#define PUBLISH_TOPIC "iot-2/evt/status/fmt/json"

#define SUBSCRIBE_TOPIC "iot-2/cmd/+/fmt/json"

--------------------------------------------------------------

  • Devices communicate via “Topics” – kind of like channels.
  • A device will publish an event to a “Topic” and you would change ‘status’ or ‘json’ in the above format to suit the “channel” you wish to communicate through.
  • A device subscribes to commands to receive instructions from the application.
  • In the above format a ‘+’ is used as a wild card to catch all commands inbound, but you can of course be specific if you wish.t
  • Reminder – devices can not subscribe to another devices topics unless the other device is a registered gateway. Devices subscribe to applications (like our Node-RED application)

Authentication

-----------------------------------------------------------------

#define AUTHMETHOD: "use-token-auth"

#define CLIENT_ID: "d: kqhpmi:Ethernet_Arduino:Controller"

#define MS_PROXY: " kqhpmi.messaging.internetofthings.ibmcloud.com"

#define AUTHTOKEN: "&322(23gh4kcL+h "

-----------------------------------------------------------------

The above authentication is based on the details provided when you attached your device in Step 2. Note below how that information maps to the above.

Once the device is added you are presented the following detail:

-----------------------------------------------------------------

Organization ID: kqhpmi

Device Type: Ethernet_Arduino

Device ID: Controller

Authentication Method: token

Authentication Token: &322(23gh4kcL+h

-----------------------------------------------------------------

The rest of the code is well commented and self explanatory.

  • It has a simple routine to read the Accelerometer data. This could of course be replaced with any data you want to publish.
  • The device subscribes to a command topic to listen for incoming commands from the application (of which there are none for this device)
  • The loop then repeatedly publishes the accelerometer data to the above topic and listens for any incoming commands.

Step 6: The Node-RED Application

The Node-RED application we deployed earlier acts as the intermediary between the Controller device and the Receiver (IOT motor device).

I also do all the calculations that map the accelerometer data to relevant servo data so that the motors move to the appropriate position to mimic the Controller.

Node-RED is mostly a drag and drop application, but I have used Javascript to process the above mapping calculations.

Launch Node-RED editor

  • Leave your “Internet Of Things” service now and go back to your Bluemix account dashboard.
  • Click on your Node-RED foundry App and then click “View App”
  • Click “Go to your Node-RED flow editor.

The interface

  • Your main commands are dragged from the left side of the screen as per Image 1 above
  • Inputs are indicated by their directional arrows and their connecters square is based on the right, whilst Outputs are the opposite visually.
  • Debug nodes are used to provide feedback on screen. This data displays in the right column as per image 1 above
  • Drag a square connector from one node to another to connect them.

Setting Up the application

As per Image 1 above – drag and connect nodes from the left scroller to the work space and connect them as per the diagram

From left to right I have:

  • Device Input (Blue) – to read the incoming data from the controller
    Double click this node and Enter detail as per Image 2 – Enter your device Authentication Token in the API Key field.
  • Function Node (Orange) – to map the data to relevant servo positions.
    Double click this node and enter your javascript here. I have attached the code I have used for this field.
  • Debug Node (Green) – to help debug
    When running, the detail from any attached node will display in the right panel. You can turn each debug node on and off by clicking the square on it’s right.
  • Device Output (Blue) – to send data to the Receiver Device (IOT Motors)
    Double click this node and Enter detail as per Image 3 – Enter your device Authentication Token in the API Key field. Note: The “Output Type” is set to Command and the “Command Type” has been specified as Coords. This will get referenced in the code for the Receiver device.

Step 7: Receiving Data to the Receiver (IOT Motor Device)

The attached Arduino sketch is provided as an example for the authenticating your device and communicating via the MQTT protocol from the Watson IOT service.

The detail is pretty much the same as the Controller device in step 4 apart from the following line where we are listening for commands.

When a command is received we are using the following line to identify the “Coords” topic sent from step 5.

------------------------------------

if(strstr(topic, "/cmd/coords") != NULL) {

-------------------------------------

We then instigate the code to move the servos in the appropriate positions received from the coords topic.

Step 8: You're Ready to Go

So that is pretty much the high level overview of connecting your devices to the IBM Watson IOT services and Bluemix applications.

The following are a few links you may find useful if you want to get a little more advanced with your implementation.

Watson IOT Platform documentation
https://console.ng.bluemix.net/docs/services/IoT/devices/mqtt.html
and
https://media.readthedocs.org/pdf/iotf/latest/iotf...

Watson IOT Tutorial
https://developer.ibm.com/tv/connect-device-watson...

Connecting Devices to the Watson IOT Platform
https://console.ng.bluemix.net/docs/services/IoT/i...

Setting up a device to talk to a Node-RED App.
https://developer.ibm.com/recipes/tutorials/connec...

Sending MQTT commands from Node-RED to a device
https://developer.ibm.com/recipes/tutorials/sendin...

IoT Builders Contest

Participated in the
IoT Builders Contest