Overview

Skill Level: Any Skill Level

Ingredients

  • Arduino Nano 3.0
  • Ethernet Shield ENC28J60 for Nano
  • Arduino Software (IDE)

Step-by-step

  1. Milestones

    1. Get yourself registered at Bluemix(I will not go deeper on this as it was already discussed in other recipes here)
    2. Create your organization (keep organization details for further usage)
    3. Add a device into your organization (keep device details for further usage)
    4. Put yourself as a member of the organization
    5. Connect your arduino mini to the ethernet shield mini (you can use a proto-board to make it stable and safe)
    6. Connect ethernet cable to hub and usb cable to your computer
    7. Open arduino software and import provided code
    8. Change code to adapt it to your organization and device’s details
    9. Upload code and keep your eyes on your internetofthings dashboard (you should see device communication)
  2. Arduino Code (Library and Sketch used)

    • Libraries required (PubSubClient and UIPEthernet)
      URLs: https://github.com/knolleary/pubsubclient and https://github.com/ntruchsess/arduino_uip
      note: download libraries and put into your arduino library’s directory. Reopen arduino software after that to load libraries.
    • Sketch used (sketch_IoT_IBM.ino).
      note: edit code below, locate brackets and put correct info. e.g. <Organization_ID_HERE> -> tcr696 (in my case)

      /*

      IBM Usefull URLs:
      https://developer.ibm.com/recipes
      https://quickstart.internetofthings.ibmcloud.com
      https://<Organization_ID_HERE>.internetofthings.ibmcloud.com/dashboard

      */

      #include <UIPEthernet.h>
      #include <PubSubClient.h>

      // Update these with values suitable for your network.
      byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
      byte ip[] = { 192, 168, 1, 157 }; //put your device’s local ip here
      EthernetClient ethClient;

      //char servername[]=”<Organization_ID_HERE>.messaging.internetofthings.ibmcloud.com”;
      char servername[]=”<PING_ABOVE_URL_AND_PUT_IP_HERE>”;
      PubSubClient client(servername, 1883, 0, ethClient);

      void setup()
      {
      Serial.begin(9600);
      Serial.println(“Arduino MQTT v.1.2”);
      Ethernet.begin(mac, ip);
      }

      void loop()
      {
      //client.loop();
      char clientStr[33];
      String clientName = String(“d:<Organization_ID_HERE>:<Device_Type_HERE>:<Device_ID_HERE>”);
      clientName.toCharArray(clientStr,33);
      char token[] = “<Authentication_Token_HERE>”;
      while (!client.connected()) {
      Serial.println(“Reconnecting client … “);
      client.connect(clientStr, “use-token-auth”, token);
      }

      String data = “{“d”: {“TEST”: “”;
      data+=random(10);
      data+=””} }”;

      char jsonStr[33];
      data.toCharArray(jsonStr,33);
      char topicStr[33];
      String topicName = String(“iot-2/evt/status/fmt/json”);
      topicName.toCharArray(topicStr,33);
      Serial.print(“attempt to send “);
      Serial.print(jsonStr);
      Serial.print(” to “);
      Serial.println(topicStr);
      if (client.publish(topicStr,jsonStr))
      Serial.println(“successfully sent”);
      else
      Serial.println(“unsuccessfully sent”);
      Serial.println(“Disconnecting client … “);
      client.disconnect();
      delay(5000);
      }

  3. Some pictures for a better understanding

    The Organization created (in my case)

    The device created

    Some device details available

    Member configuration

    My device view

Join The Discussion