0
                    #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;
             }
          }

以大写形式给出的mac地址也给出相同的结果。尝试任何其他 mac 地址都不会显示快速入门网站上连接的任何内容。

串行监视器的输出和快速入门网站上显示的内容如下所示 在 此处输入图像描述

4

1 回答 1

2

您是否尝试过将 Arduino Uno 连接到 IBM Watson IoT Platform 的配方?你从哪里得到这个代码?看起来您使用的代码与配方中给出的代码不同。这是配方的链接 - https://developer.ibm.com/recipes/tutorials/connect-an-arduino-uno-device-to-the-ibm-internet-of-things-foundation/

于 2017-01-10T06:58:45.633 回答