Skill Level: Beginner


PrepareGetting your device ready:Mount the Ethernet / Wi-Fi Shield on the Arduino Uno device.Connect the Arduino Uno to the USB of your computer.Connect an Ethernet cable to the Ethernet / Wi-Fi Shield.Install sketch on desktop / laptopDownload Paho mqtt library, for Ardunio, from this link in the sketch a. To load it into the Arduino […]


Hardware Requirements:


  1. Prepare

    Getting your device ready:

    1. Mount the Ethernet / Wi-Fi Shield on the Arduino Uno device.
    2. Connect the Arduino Uno to the USB of your computer.
    3. Connect an Ethernet cable to the Ethernet / Wi-Fi Shield.
    4. Install sketch on desktop / laptop
    5. Download Paho mqtt library, for Ardunio, from this link in the sketch
      a. To load it into the Arduino IDE, the zip file should be downloaded intact, without unzipping.
      b. Then in the IDE: Select Sketch -> Import Library -> Add Library and select the zip file
    6. Connect the USB cable to the Arduino Uno and other end to desktop / laptop which has the sketch installed on it.
    7. The samples folder of this repository (https://github.com/ibm-messaging/iot-arduino) contains 2 folders, each containining 1 flow –
      1. Quickstart flow
      2. Registered flow
    8. Compile the 2 sketch codes (corresponding to the flows)
    9. Depending upon the requirement, push one of the flows to the Arduino device
    10. Reset the Ethernet / Wifi shield
    11. Wait for about 10 seconds and the flow starts working


    Getting the Bluemix services ready:

    This IoT Recipe makes use of the Create Toolchain button to help deploy the necessary services on to Bluemix. Click on the Create Toolchain button provided below, provide a custom name to your application and choose to click on Create button, to quickly deploy the Watson IoT Platform – Node-RED Bluemix starter app. Post successful deployment, you shall have both the above mentioned services, up & running on Bluemix along with a Cloudant NoSQL DB at your disposal.


    Note: If you are a User using the United Kingdom Region in your Bluemix environment, then please make use of the steps mentioned in the IoT Recipe Deploy Internet of Things Platform Starter service on Bluemix to deploy the setup. Alternatively, you can also try using the Deploy to Bluemix button, to deploy the setup under your United Kingdom Region, provided your Jazzhub account is validated. Users of US South Region can ignore this step.


  2. Connecting to the IBM Watson IoT Platform Quickstart Service

    The IBM Watson IoT Platform Quickstart Service does not require you to have signed up to the IBM Watson IoT Platform

    1. Use the following command to download the installer from GitHub:
      curl -LO https://github.com/ibm-messaging/iot-arduino.git

      (Source code is available here).

    2. Use the Sketch program, installed in the preparation stage, to open the samples code samples/quickstart/quickstart.ino
    3. View the lower part of the Sketch pad window to check that the COM connection is shown as active.
    4. Compile and deploy the sample code to the Arduino Uno device.
  3. Visualizing your data with the IBM Watson IoT Platform

    1. By default, the Sketch code assigns MAC address of aabbccde02. You must modify this address to a unique number. Check for the variable “byte [] mac” in the sketch code, this needs to be modified.
    2. Enter the unique code here and click Go.
  4. Register your Device In Watson IoT Platform (WIoTP)

    To explore and make use of full capabilities of Watson IoT Platform, we need to setup and register your device(s) in it. This section shows how you can setup the same.

    Open the Bluemix Dashboard and click on the Application service that you have currently deployed (using the Create Toolchain button), if you have already moved away from it. You should see the Watson IoT Platform listed under the Binded Services. Click on the WIoTP service and choose the click on Launch button to launch the WIoTP Dashboard.

    Carry out the steps present in this recipe to register your device(s) in IBM Watson Internet of Things Platform.

    At this step, we have successfully created the Watson IoT service and registered your device(s) in it.

  5. Do you want to skip Secure connections for now?

    By default, the Watson IoT Platform accepts only Secure connections. Unless the sketch program is configured, the Arduino device, by default initiates Insecure connection to Watson IoT Platform. In such cases, you might experience connectivity issues while initiating the connection between the device / gateway & WIoTP.

    However, to facilitate all connections ( both Secure and Insecure)  to WIoTP, the Platform provides a security configuration, that helps you to take a well informed decision to allow Insecure connections, under your supervision.

    Please skip this section if you Do Not Want to allow Insecure connections to WIoTP. However, should you choose to allow Insecure connections to WIoTP from individual devices or devices connected to Platform through a gateway, then, please work through the following steps to configure the Security settings to make TLS Connections optional:

    1. While you are in the Watson IoT Platform Dashboard area, click on the Security tab on the left hand side pane.


    2. Under the Policies section, choose the Connection Security option and click on the Configure button


    3. Take a note of the options listed against the Default Security Level drop down menu


    4. Choose to select TLS Optional as your choice from the listed options in the drop down menu


    5. Optionally, you can add custom connection security to apply different security level to specific device from your list of registered devices, by having them added under Custom Connection Security


    6. Click on Save provided on the top-right corner of the screen to save the Security settings
  6. Connecting to the IBM Watson IoT Platform Registered Service

    1. During the device registration process, you will receive file configuration information with the following information.
      • Organization ID
      • Device Type
      • Device ID
      • Authentication Method
      • Authentication Token
    2. Copy this information to the clipboard to use in the configuration file.
    3. Use the device credentials obtained during registration to modify the fields in the Sketch code. An example configuration is shown in the README.md file from the GitHub download.
    4. Save the configuration file by pressing CTRL-S in the Sketch program.

    Note: All the properties in the configuration file are mandatory. Currently, only token based authentication is supported.

  7. Visualize the Arduino Uno events in Watson IoT Platform dashboard

    With the new boards and cards capability in the Watson Internet of Things platform, you can build your own Custom dashboard without writing any code. You can use the boards as the landing page of interest and then make use of the cards within them to:

    • Create visualization charts for the real time data from your devices
    • Create Gauges for visualizing physical quantities like Vehicle Speed, Temperature, pressure
    • Create Donuts charts, bar charts to display the current value of the data points
    • See the Data and storage consumption of your devices
    • List of registered devices and etc..

    Refer to Configuring Boards & Cards in the new Watson IoT Dashboard recipe to view the Arduino Uno events in Watson IoT Platform using the boards and cards.

  8. Access the Node-RED application to receive events from the device

    With events now being sent to Watson IoT, the next step is to access the application in Bluemix to receive the events.

    Access the Node-RED application in Bluemix

    1. You have deployed the Node-RED service on to the Bluemix by clicking on the Create Toolchain button in Pre-requisites section. Access your Node-RED application by clicking on View App link and then choosing the option Go to you Node-RED flow editor, to access the editor.
    2. Parallelly, you can also access the Node-RED editor directly by accessing the following URL

    Create a simple flow to start receiving the device events

    • Open the Node-RED flow editor,
    • Add an ibmiot input node and configure it to use Bluemix Service authentication. Set the Device Type field to match the type you used to register your device with. Leave the Device Id and Event fields set to all.receive-events-in-node-red_1
    • Attach the debug node to the IBM IoT node as shown below to output the datapoints in the debug panel.receive-events-in-node-red
    • Click Deploy to deploy the flow in Node-RED and observe that the readings are printed in the console.

    In this step, we have successfully configured the IBM IoT App In Node to receive the Arduino Uno events from the Watson IoT Platform.

  9. Send commands to your Arduino Uno device

    When the Arduino Uno Sample is running in registered mode, it supports the receipt of commands sent by an Application.

    This sample supports the blink Command. When the user sends this command, the Arduino Uno PIN13 is configured to blink for 2 seconds.

    • Drag-n-Drop an inject node and configure the payload as follows,arduino-trigger-blink
    • As shown the above payload instructs the sample to wait for 3 seconds and reboot the device.
    • Drag and drop ibmiot out node from the output pane and configure it to use Bluemix Service authentication. Set the Device Type, Device Id fields appropriately. Set the Output type as Device Command and connect it to the reboot trigger node as shown below,send-arduino-command
    • The flow should look like the following,
    • Click deploy to deploy the flow.
    • Click on the Trigger blink node to send the blink command to Arduino Uno device.

    In this step, we saw how to send a command to the device to control it using a Node-RED application.

  10. Where to go from here?

    As a next step, go through the following recipes that will help you to create an end to end IoT solution,

28 comments on"Connect an Arduino Uno device to the IBM Watson IoT Platform"

  1. The very first step threw me off. Where would we enter this command?
    curl -LO https://github.com/ibm-messaging/iot-arduino.git
    I’m using an arduino uno and an ethernet shield connected to a windows 7 laptop. I don’t know where to enter that command.

  2. KiranShashi January 16, 2016

    I’m at Step 8 above, and I got an error that IP stack.h is not found, when I copied the quickstart.ino to my Sketch and tried to Verify.
    ( I have an Arduino Uno and an Ethernet Shield )
    ===X==== Cut and Paste from Sketch Compile Error ===X====
    Arduino: 1.6.7 (Mac OS X), Board: “NodeMCU 0.9 (ESP-12 Module), 80 MHz, Serial, 115200, 4M (3M SPIFFS)”

    /Users//iot-arduino/samples/quickstart/quickstart.ino:3:21: fatal error: IPStack.h: No such file or directory
    compilation terminated.
    exit status 1
    Error compiling.

    This report would have more information with
    “Show verbose output during compilation”
    enabled in File > Preferences.
    ========X=== Done Cut and Paste ==X===

  3. Hi! Can you tell me how I`d have to change this code to use a external temp. sensor like DHT11? Thanks

    • Ph.Gregoire March 27, 2016

      Not sure if you have solve this question already?
      In any case, to use DHT11, you would need to:
      * Add DHT_sensor_library to your sketch/project
      * #include in your sketch’s code
      * have an instance of the DHT object, declared above setup, e.g.: DHT dht(DHTPIN, DHT11);
      * inside of setup(), call dht.begin();
      * inside of loop(), call float h = dht.readHumidity(); and float t = dht.readTemperature(); , then publish this to MQTT

  4. Hi
    Im trying to get this to work on my arduino mega and I manage to get the quickstart example working fine but having trouble with the registered version.
    It seems like the device cant connect to the IoT platform.
    Here is my editing to the credentials in the code
    #define PUBLISH_TOPIC “iot-2/evt/status/fmt/json”
    #define SUBSCRIBE_TOPIC “iot-2/cmd/+/fmt/json”
    #define AUTHMETHOD “use-token-auth”

    int ledPin = 13;

    #define CLIENT_ID “d:nkq1vt:Arduinomega:0000bbccde00”
    #define MS_PROXY “nkq1vt.messaging.internetofthings.ibmcloud.com”
    #define AUTHTOKEN “4ctkEDkgv4(Y8FCF-B”
    // Update these with values suitable for your network.
    byte mac[] = { 0x00, 0x00, 0xBB, 0xCC, 0xDE, 0x00 };

    Have I missed something?

    • Recipes@WatsonIoT November 29, 2016

      Thank you for contacting us, Looks like the configurations are proper, Can you answer the following queries to debug further?
      1. Is this { 0x00, 0x00, 0xBB, 0xCC, 0xDE, 0x00 } the MAC address of your Arduino?
      2. Is the AUTHTOKEN obtained during the registration of the device and it is not the API Key Token? For more information look at this recipe – https://developer.ibm.com/recipes/tutorials/how-to-register-devices-in-ibm-iot-foundation/
      3. Can you paste the output of your program? also the error code?

      • Hi Thank you for your quick reply!
        1. This is just a random mac address I picked which works fine when used in the quickstart sketch .
        2. Im not sure how to upload a screenshot to here(to show the exact credentials) but the AUTHTOKEN used is the authentication token obtained whenI registered the device.in the IoT platform device section, it is not the API key token from the apps section.
        3. This is what I get in the serial monitor
        Connecting using Registered mode with clientid : d:nkq1vt:Arduinomega:0000bbccde00 to MQTT Broker : nkq1vt.messaging.internetofthings.ibmcloud.com on topic : iot-2/evt/status/fmt/json
        In the IoT platform all I see is a triangle with ! saying Disconnected.

        I get this line once every time I restart the device.

        • Recipes@WatsonIoT November 29, 2016

          Hi, I tried using the following credentials from my Node-RED client and got not authorized error .. looks like some issue the details.

          ## Device Registration detail
          Organization-ID = nkq1vt
          Device-Type = Arduinomega
          Device-ID = 0000bbccde00
          Authentication-Method = token
          Authentication-Token = 4ctkEDkgv4(Y8FCF-B

          Please send the screenshot to sathipal@in.ibm.com? or add sathipal@in.ibm.com as a member into your organization? so that i can take a look.

          • OK Solved!
            In the readme file it says
            Modify the CLIENT_ID, in the sketch code, by providing the values in the following format “d:uguhsp:iotsample-arduino:00aabbccde03”, by replacing “aabbccde03” with the MAC Address and “uguhsp” with the organization and “iotsample-arduino” with the device type that you entered when creating the device.

            It should be replacing “aabbccde03” with the device ID rather the MAC address.

          • Thanks for your AWESOME help!

  5. Recipes@WatsonIoT November 29, 2016

    Cool, Thank you @PilpelHarif. We ll look at correcting the readme.

  6. Hi

    I have tried the recipe and have some issues.
    I am using the script for a registered device and can’t seem to connect to Publish.

    I have used a WiFi client and can connect to the MQTT service “ydyqtv.messaging.internetofthings.ibmcloud.com”.

    These are the connection details I am using;

    #define MQTT_MAX_PACKET_SIZE 100
    #define SIZE 100
    #define MQTT_PORT 1883

    #define PUBLISH_TOPIC “iot-2/evt/status/fmt/json”
    #define SUBSCRIBE_TOPIC “iot-2/cmd/+/fmt/json”
    #define AUTHMETHOD “use-token-auth”

    #define CLIENT_ID “d:ydyqtv:demo1:1”
    #define MS_PROXY “ydyqtv.messaging.internetofthings.ibmcloud.com”
    #define AUTHTOKEN “11111111”

    As far as I an tell I am not successfully connecting and get stuck at this line of code

    while ((rc = client.connect(options)) != 0)

    Can you please help ?


    • Recipes@WatsonIoT February 01, 2017

      Looking at your given code snippet, you should have device type as “demo1” , device id as “1” and the authtoken as “11111111” registered with Watson IoT Platform. I want to get this confirmed from your side, have you done these registrations with Watson IoT Platform before running the code? And also, your registered device id’s hexdecimal values should be provided as contents of mac variable in the sketch:

      #define CLIENT_ID “d:uguhsp:iotsample-arduino:00aabbccde03”

      // Update these with values suitable for your network.
      byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };

      Can you please confirm?

  7. hi
    in step 1: prepare
    in point 4 it is given “Install sketch on desktop / laptop”
    which sketch is to be installed?

    • Recipes@WatsonIoT April 27, 2017

      Its the Arduino IDE that needs be installed on your desktop/laptop. We will correct the line as its confusing.

  8. hello
    I have downloaded paho library from the link and added in arduino
    then downloaded sample code provided by git hub
    but i got error in the code of quickstart like # include
    please help me in sorting out this

  9. #include
    error status 1

  10. YatinHKamble April 17, 2017

    Could you please guide on following error.—-

    Arduino: 1.8.2 (Windows 7), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

    C:\Users\IBM_ADMIN\Desktop\Internet of Things & Augmented Reality Emerging Technologies\IoT collabera\device-arduino-master\device-arduino-master\samples\quickstart\quickstart.ino:3:21: fatal error: IPStack.h: No such file or directory



    compilation terminated.

    exit status 1
    Error compiling for board Arduino/Genuino Mega or Mega 2560.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

  11. ganesh@19 May 23, 2017

    in the quickstart flow, nothing is being plotted in real time.
    It says “we are waiting for the device to send us a message”.
    Do i have to modify the code or something to see the real time visualizations?

  12. WaelYoussef77 August 13, 2017

    Hello Team,

    I am facing this error when i am trying to compile the registered.ino, it gives “redefinition : for almost everything ,Can you please help?

    registered_yun:17: error: redefinition of ‘int ledPin’
    int ledPin = 13;

    • Amit M Mangalvedkar February 11, 2018

      @WaelYoussef77, looks like the same code has been pasted twice. Could you please double-check if the entries have been typed twice?

Join The Discussion