Search code examples
arduinoibm-cloudiotwatson-iot

Cannot connect to ibm bluemix quickstart with the following code


                    #include <SPI.h>
                     #include <Ethernet.h>
                      #include <PubSubClient.h>


         byte mac[] = { 0xde, 0xed, 0xba, 0xfe, 0xfe, 0xed};
          String macstr="deedbafefeed";
         byte ip[]     = {192,168,1,1 };



        char servername[]="quickstart.messaging.internetofthings.ibmcloud.com";
    String clientName = String("d:quickstart:iotsample-arduino:")+macstr;
    String topicName = String("iot-2/evt/ledstat/fmt/json");

    float tempF = 0.0;
    float tempC = 0.0;
         float humidity = 0.0;
     EthernetClient ethClient;

    PubSubClient client(servername, 1883,0,ethClient);

     void setup()
        {

         Ethernet.begin(mac,ip);
         Serial.begin(9600);

        }

        void loop()
           {
        char clientStr[324];
        clientName.toCharArray(clientStr,324);
        char topicStr[26];
        topicName.toCharArray(topicStr,26);
         getData();
        if (!client.connected()) {
        Serial.println(client.connected()); 
           Serial.print("Trying to connect to: ");
          Serial.println(clientStr);
       client.connect(clientStr);
       Serial.println(client.connected());
          Serial.print("attempt to send ");
       Serial.println(buildJson());
         delay(20000);
      } 
      if (client.connected() ) {
       String json = buildJson();
      char jsonStr[200];
         json.toCharArray(jsonStr,200);
       boolean pubresult = client.publish(topicStr,jsonStr);
        Serial.print("attempt to send ");
          Serial.println(jsonStr);
           Serial.print("to ");
           Serial.println(topicStr);
            if (pubresult)
             Serial.println("successfully sent");
       else
             Serial.println("unsuccessfully sent");
            }
     delay(5000);
     }

    String buildJson() {
    String data = "\n{";
       data+="\n";
       data+= "\"d\": {";
     data+="\n";
      data+="\"myName\": \"Arduino DHT11\",";
     data+="\n";
         data+="\"temperature (F)\": ";
       data+=(int)tempF;
       data+= ",";
       data+="\n";
       data+="\"temperature (C)\": ";
         data+=(int)tempC;
         data+= ",";
          data+="\n";
          data+="\"humidity\": ";
          data+=(int)humidity;
         data+="\n";
          data+="}";
           data+="\n";
          data+="}";
         return data;
         }

      void getData() {
      int  chk=0;
          switch (chk)
           {
        case 0: 
          Serial.println("Read OK"); 
            humidity = 90;
                tempF = 12;
           tempC = 56;      
           break;
         case -1: 
            Serial.println("Checksum error"); 
              break;
            case -2: 
               Serial.println("Time out error"); 
             break;
           default: 
             Serial.println("Unknown error"); 
            break;
             }
          }

the mac address when given in upper case also gives the same result. Trying any other mac address doesn't show any thing connected on the quickstart website.

The output of serial monitor and what is shown on the quickstart website is shown below enter image description here


Solution

  • Have you tried recipe on Connecting Arduino Uno to IBM Watson IoT Platform? Where did you get this code from? Looks like the code that you are using is not same as the one given with the recipe. Here is the link to the recipe - https://developer.ibm.com/recipes/tutorials/connect-an-arduino-uno-device-to-the-ibm-internet-of-things-foundation/