1

我正在尝试将数据从 Aurdino 上传到 Xively。我可以连接到 Xively,甚至可以在 Xively 请求日志上显示 PUT 请求。但我收到错误 400 Bad Request {"title":"JSON Parser Error","errors":"Feed is empty"}

我认为问题可能在这里

data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" :  \"Reading\",\"current_value\" : \"" + String(reading) + "\"}]}";

任何提示将不胜感激。干杯和感谢

///////////////////////
// Saving to Xively ///
///////////////////////

int savetoxively(String reading, char* feedID) {


//getting the IP address for the xively website.  

ip = 0;
Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
 if (! cc3000.getHostByName(WEBSITE, &ip)) {
  Serial.println(F("Couldn't resolve!"));
 }
 delay(500);
}
cc3000.printIPdotsRev(ip);

// formatting the data for the xively website. 

int length = 0;

String data = "";      //the problem might be here, the serial monitor says the length is 0. should have something ing it. 
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Reading\",\"current_value\" : \"" + String(reading) + "\"}]}";


length = data.length();
  Serial.print("Data length");
  Serial.println(length);
  Serial.println();

  // Print request for debug purposes
  Serial.print("PUT /v2/feeds/");
  Serial.print(feedID);                    //serial monitor shows the correct feedID here
  Serial.println(".json HTTP/1.1");
  Serial.println("Host: api.xively.com");
  Serial.print("X-ApiKey: ");
  Serial.println(API_key);                 //serial monitor shows the correct API key
  Serial.print("Content-Length: ");
  Serial.println(length, DEC);
  Serial.print("Connection: close");
  Serial.println();
  Serial.print("Reading: ");
  Serial.println(reading);                 //serial monitor shows the correct reading
  Serial.println();


// connecting with the xively server

Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);      

if (client.connected()) {
    Serial.println("Connected!");
    Serial.print("feedID: ");
    Serial.println(feedID);
    client.print("PUT /v2/feeds/");
    client.print(feedID);
    client.println(".json HTTP/1.1");      
    client.println("Host: api.xively.com");
    client.print("X-ApiKey: ");
    client.println(API_key);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");
    client.println(length);
    client.print("Connection: close");
    client.println();
    client.print(data);          
    client.println();            
   Serial.println("data sent");    

  }

while (client.connected()) {          //printing connection status
 while (client.available()) {         //HTTP/1.1 400 Bad Request
  char c = client.read();             //Date: Sat, 15 Mar 2014 19:33:38 GMT
  Serial.print(c);                    //Content-Type: application/json; charset=utf-8
                                      //Content-Length: 58
                                      //Connection: close
                                      //X-Request-Id: f48494a26daa2a5f0979dc4460264d233103f0f5
                                      //{"title":"JSON Parser Error","errors":"The feed is empty"}
 }
}

client.close();      // closes the connection with xively

cc3000.disconnect();    //disconnects the Wifi network

return 1;  


}
4

0 回答 0