Uploading to Xively via Ethernet Shield constantly return -3.

I am getting "xivelyclient.put returned -3" whenever it tries to upload the data to Xively. I have also checked my codes, but can't seems to find any error. Please help!

Here are the codes:

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Your Xively key to let you upload data
char xivelyKey[] = "LJrYPIT0vx59hkZCXzkQuywSukEwZJAzJx5jZB3CQAZBxMq1";//API KEY

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

// Define the strings for our datastream IDs
char sensorId[] = "SensorReading";
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT)
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(633350176, datastreams, 1 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

int flexSensorPin = A0; //analog pin 0

void setup(){
Serial.begin(9600); // telling it to begin sensing the signal at 9600 bits of data per second
}

void loop(){

int flexSensorReading = analogRead (flexSensorPin); // declaring what the sensor reading is

delay(250); // Just to slow down the output for easier reading

int flex0to100 = map(flexSensorReading, 150, 300, 100, 0);
//flexSensorReading is assigned to store the raw analog value from the sensor.
//map(value, fromLow, fromHigh, toLow, toHigh). map() function is used to reduce the range as Arduino is able to read analog value up to 1023.

Serial.println(flex0to100);

int sensorValue = analogRead(sensorPin);
datastreams[0].setFloat(sensorValue);

Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());

Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);

Serial.println();
delay(15000);
}

There is no call to Ethernet.begin() in setup.

I don't think the -3 is a Xively code. I recall they are three digit and the one you hope to God you see is 200. The code looks pretty right as far as Xively is concerned.

The code seems confusing

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

// Define the s.........
...............................ivelyclient(client);

int flexSensorPin = A0; //analog pin 0

which implies you are using digital pin 2 for something, and the setup normally carries

while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(10000);
}

It is recommended that you don't publish your API key

Hi SurferTim, thanks for the help. I have tried to add in Ethernet.begin(mac,ip) but now i has an error of -1.

Here are the code:

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip();

// Your Xively key to let you upload data
char xivelyKey[] = "";//API KEY

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
//int sensorPin = 2;

// Define the strings for our datastream IDs
char sensorId[] = "SensorReading";
XivelyDatastream datastreams[] =
{
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT)
};

// Finally, wrap the datastreams into a feed
XivelyFeed feed(633350176, datastreams, 1 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

int flexSensorPin = A0; //analog pin 0
int flexSensorPin1 = A1;

void setup()
{
Ethernet.begin(mac,ip);
Serial.begin(9600); // telling it to begin sensing the signal at 9600 bits of data per second
}

void loop()
{
int flexSensorReading = analogRead (flexSensorPin); // declaring what the sensor reading is
int flexSensorReading1= analogRead (flexSensorPin1);

delay(2050); // Just to slow down the output for easier reading

int flex1 = map(flexSensorReading, 150, 300, 100, 0);
int flex2 = map(flexSensorReading1, 150,300,100, 0);
//flexSensorReading is assigned to store the raw analog value from the sensor.
//map(value, fromLow, fromHigh, toLow, toHigh). map() function is used to reduce the range as Arduino is able to read analog value up to 1023.

Serial.println("The value of flex sensor 1 is: ");
Serial.println(flex1);
Serial.println("The value of flex sensor 2 is: ");
Serial.println(flex2);

//int flexSensorValue = analogRead(flexSensorPin);
//datastreams[0].setFloat(flexSensorValue);

//Serial.print("Read sensor value ");
//Serial.println(datastreams[0].getFloat());

Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);

Serial.println();
delay(15000);
}

You are not setting the ip variable correctly. Try using dhcp to get your network settings.

// change this
  Ethernet.begin(mac,ip);

// to this
  while(Ethernet.begin(mac) == 0) {
    Serial.println("DHCP failed");
    delay(1000);
  }

  Serial.println(Ethernet.localIP());

Hi Nick_Pyner, thanks for the help. May i ask, when you mentioned pin 2, does it also refers to pin A2? I am currently using A0 and A1 to connect two sensor respectively.

Hi SurferTim, thanks for your help! It is able to work now! Thank you. :smiley:

I think the problem might be that I'm not mentioning pin 2, you are,

int sensorPin = 2;

and it's a digital pin. This doesn't sit very well with the adjacent comment. I don't know if you really intend to use it.

I now see you probably don't, as that line has now been commented out. I suspect it doesn't even serve as a comment, and it makes more sense to delete it.

I think

IPAddress ip();

is redundant

The relevant sections of your code is more or less the same as mine - word for word - except that you make no allocation of string buffer size. It is possible that there is sufficient by default. Other than that, I suspect the problem is with the sensors, not Xively.

Below is a standard code. COSM and Xively are essentially the same thing, and my cosm code worked on Xively without modification.

/*
From cosm library example and lifts from a lot of others
particularly from Stanley in Kuala Lumpur.
Use your own DS18B20 addresses, keys etc.
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>

byte InThermo[8]={0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8]={0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char cosmKey[] = "l6a yours here z0g";

int sensorPin = 3;

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";

const int bufferSize = 100;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
CosmDatastream datastreams[] = {
CosmDatastream(sensorId0, strlen(sensorId0), DATASTREAM_FLOAT),
CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
CosmFeed feed(83153, datastreams, 2 /*put your number of feed here - in this case 2*/);

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
Serial.begin(9600);

sensors.setResolution(InThermo, 12);
sensors.setResolution(OutThermo, 12);

Serial.println("Starting multiple datastream upload to Cosm...");
Serial.println();

while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(10000);
}
}

void loop() {

int ret=0;
//get the values from the DS8B20's
sensors.requestTemperatures();
Serial.println("Read sensor value ");

float InTemp = (sensorValue(InThermo));
float OutTemp = (sensorValue(OutThermo));
// float Drain = (sensorValue(DrainThermo));

datastreams[0].setFloat(InTemp);
datastreams[1].setFloat(OutTemp);

Serial.println(datastreams[0]);
Serial.println(datastreams[1]);

Serial.println("Uploading it to Cosm");
ret = cosmclient.put(feed, cosmKey); // SEND FEED TO COSM
Serial.print("cosmclient.put returned ");
Serial.println(ret);

Serial.println();
delay(10000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
float tempC = sensors.getTempC (deviceAddress);
return tempC;
}

Hi Nick_Pyner, thanks for your help! I am able to upload it without any error! ;D

I am now able to upload the data to Xively via direct connetion to the router. However, I would like to upload it via bridge connection. But whenever the ethernet cable is plugged into the laptop, my wifi connection will be disconnected. Is there other ways to upload it by connecting the ethernet cable directly to my laptop? I am using windows 8.

Do a Google search for "internet connection sharing". There were plenty of how-to articles last I checked.