Xively HTTP PUT requests

Hi Guys,

I have a problem with the following code. I trying to write HTTP PUT commands to Xively. I aware that they have a Arduino friendly library which I already have used with success however; the library doesn’t support Location data by Waypoints so I can’t display where my device is on the little google map they have on the dashboard.

I thought I might hack the old Pachube example which comes with the IDE and change the API to suit Xively. From about the is point onwards it starts to head down hill…

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

// MAC address for your Ethernet shield
byte mac[] = {0x90, 0xA2D, 0xDA, 0x0D, 0xFD, 0xFA};

// RTC home register address
const int DS3232_I2C_Address=0x68;

// Your Xively key to let you upload data
const char API_key[] = "0qeB68UHfHYClShV4tuGHHqBMWcN0KrTz1SND7JRpmuDWihP";
const unsigned long feedID = 1951066047;
const char streamCount = 1;
const char serverName[] = "www.xively.com";

// Define the strings for our datastream IDs
char sensorId[] = "Temperature";

EthernetClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Starting single datastream upload to Xively...");
  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(15000);
  }
}

void loop() 
{
  int contLength = outPutJSON(Serial);
  if(feed(feedID, contLength) == true)
  {
    Serial.println("Feed up-dated");
    delay(5000);
  }
  else
  {
    Serial.println("Unable to update feed");
    delay(5000);    
  }
}

boolean feed(unsigned long ID, int length)
{
  boolean result=false;
  if(client.connect(serverName, 80) > 0)
  {
    client.print("PUT /v2/feeds/");
    client.print(feedID);
    client.println(".json HTTP/1.0");
    client.print("Content-Length: ");
    client.println(length+2, DEC);
    client.println("Host: api.xively.com");
    client.print("X-ApiKey: ");
    client.println(API_key);
    client.print("Content-Length: ");
    client.println(length+2, DEC);
    client.print("Connection: close");
    client.println();
    outPutJSON(client);
    client.println();
  } 
  else 
  {
    // if you couldn't make a connection:
    Serial.println("Connection failed");
  }
  if(client.connected())
  {
    Serial.println("Connected");
    if(client.find("HTTP/1.0") && client.find("200") == true)
    {
      Serial.println("200 OK");
      result=true;
    }
    else if(client.find("HTTP/1.0") && client.find("400") == true)
    {
      Serial.println("400 Bad Request");
    }
    else if(client.find("HTTP/1.0") && client.find("401") == true)
    {
      Serial.println("401 Not Authorized");
    }
    else if(client.find("HTTP/1.0") && client.find("403") == true)
    {
      Serial.println("403 Forbidden");
    }
    else if(client.find("HTTP/1.0") && client.find("404") == true)
    {
      Serial.println("404 Not Found");
    }
    else if(client.find("HTTP/1.0") && client.find("406") == true)
    {
      Serial.println("406 Not Acceptable");
    }
    else if(client.find("HTTP/1.0") && client.find("422") == true)
    {
      Serial.println("422 Unprocessable Entity");
    }
    else if(client.find("HTTP/1.0") && client.find("500") == true)
    {
      Serial.println("500 Internal Server Error");
    }
    else if(client.find("HTTP/1.0") && client.find("503") == true)
    {
      Serial.println("503 No server error");
    }
    else
    {
      Serial.println("I dont know what is happening?");
    }
  } 
  else
  {
    Serial.println("Disconnected");
  }
  client.stop();
  client.flush();
  return result;
}

int outPutJSON(Stream &stream)
{
  float temperature=getTemperature();
  int count=0;
  count+=stream.println("{");
  count+=stream.println("\"version\":\"1.0.0\",");
  count+=stream.println("\"datastreams\" : [ {");
  count+=stream.println("\"id\" : \"Temperature\",");
  count+=stream.print("\"current_value\" : \"");
  count+=stream.print(temperature,2);
  count+=stream.println("\"");
  count+=stream.println("}");
  count+=stream.println("]");
  count+=stream.println("}");
  return count;
}


float getTemperature()
{
  byte MSB, LSB;
  float degreesC;
  // retrieve data from DS3232  
  Wire.beginTransmission(DS3232_I2C_Address);
  Wire.write(0x11); // set DS3232 register pointer to 00h
  Wire.endTransmission();  
  Wire.requestFrom(DS3232_I2C_Address, 8); // request 7 bytes of data from DS3232 starting from register 00h
  MSB=Wire.read();
  LSB=Wire.read()>>6;
  // send it to the serial monitor
  degreesC = MSB+0.25*LSB;
  return degreesC;
}

I can’t even get a HTTP response from the request :0 Are any of you guys know what I’m doing wrong?

regards

Adam

Wow, first post I've made and no replies... must be a difficult one!

adamatcooranbong: Wow, first post I've made and no replies... must be a difficult one!

18,600 hits to review.

https://www.google.com/search?as_q=xively+arduino&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

Usually this forum is pretty good for getting help, but once in a while you don't get much of a response or one reason or another. For Xively help you might want to try http://stackoverflow.com/questions/tagged/xively