Uploading data to Xively.com via Ethernet Shield constantly returns -401

Hi,

I am having problem sending data to Xively.com. I need your help...

Here's my code:

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

// MAC address for your Ethernet shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x50, 0xAA };

// Your Xively key to let you upload data
char xivelyKey[] = "Auto-generated gravimetertest2 device key for feed 445822759";

// 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[] = "sensor_reading";
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(445822759, datastreams, 1 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  Serial.println("Starting single datastream upload to Xively...");
  Serial.println();

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

void loop() {
  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);
}

Basically, I just used the code provided by Xively as an example. I just changed the MAC address, API Key, Feed ID, and created a "device" in xively.com that will receive the stream of data from my Arduino+Ethernet Shield. I have added a few electronics so I read the analog signal out of analog port 2.

When running the code, I see this message (repeatedly) in the serial monitor:

Read sensor value 283.00
Uploading it to Xively
xivelyclient.put returned -401

What can I do to fix the problem?

Thank you! Would greatly appreciate any help that I can get. :slight_smile:

BR,
Rucus

Hi Rucus, I'm not an expert on this, only started using xively 3 days ago, but I took your sketch and patched in my own API and feed, and it works fine for me. I get return codes of 200, and the data posts fine, see here that's your sketch running on my UNO, although I usually use a Mega2560 board, since I don't want to worry about code and RAM overflows.
https://xively.com/feeds/66573328
I'll leave it running for a while, if no data is seen when you look, scroll the graph backwards a ways. Otherwise, I'm not currently posting my own data.

This suggests there is something wrong with your API key or feed number on the xively site, not the sketch itself.

Also, the one thing I specifically noticed in your sketch that I didn't like was the comma at the end of the following line, but removing it didn't matter,

XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};

EDIT: In my version of your sketch, I had changed "sensor_reading" to "light" to match my setup, so I went back and changed it to "sensor_read" so now it doesn't match my original setup. However, xively simply forked a new display graph, but it still runs ok and still returns a 200 code. So, xively doesn't seem to care what channel name you send.

Hi oric_dan,

Thanks for testing the code.

Can you share how you set-up your Xively to receive the data from your Arduino?

I am also suspecting that there's something wrong with my Xively setup. This is because I was able to run successfully other Ethernet sample sketches like the WebClient.

Thanks,
Rucus

HTTP error 401 is an unauthorized request.

10.4.2 401 Unauthorized

The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Hi

Stupid question from me but i dont know if i have to upload the sketch on the Arduino one board or on the Ethernet shield ?

Jean-Robert

Hi Jean-Robert,
I uploaded the sketch in the Arduino Uno board via USB and Arduino IDE.
Hope this helps.
Thanks!

Hi,
I noticed that everytime I create a channel in Xively, I always encounter an error saying: "There was a problem saving this channel. Try again". It appears above the 'Save Channel' button.
Do you also encounter the same error?

Thanks!

Rucus:
Hi,
I noticed that everytime I create a channel in Xively, I always encounter an error saying: "There was a problem saving this channel. Try again". It appears above the 'Save Channel' button.
Do you also encounter the same error?

Thanks!

Well, mine worked ok, but I've only done it once. When I logon now, I see a screen that says Development Devices, and there are 2 fields, left one has my account name on it and the right one says +Add Device. If I click on the left, it shows my complete setup info.

Does that work for you? I would look there. Does yours say "Activated" on top of the screen. Also, you need to set the API key field to "permissions READ,UPDATE,CREATE,DELETE", so you can upload data. I believe the original defaults to READ only.

You can also add channels from the account screen. Like everything else anymore, it's a lot of endless mucking around, and mainly trying to figure out what everything means.

EDIT: BTW, I loaded your sketch into a UNO, changed it to 5-minute update period, and have run it all night, and it's still working fine, see the "sensor_read" channel,
https://xively.com/feeds/66573328

And like I said, I never even set that channel up, xively just takes the new data uploads and forked a new display channel according to the label on the feed.

Hi oric_dan,
Sorry for my late reply. I tried to upload data again to Xively today but still, no luck. I created a new account just to make sure I got the latest "default settings" from Xively (if there would any be) but I still got the -404 return code. I tried running other Ethernet sketches like the WebClient and it worked fine.

To narrow down the problem by eliminating the possibility that there may be something wrong with my Ethernet shield, I tried their 'Get Started' tutorial. It will only require a phone (using its internal sensors) as device that will feed data to my xively api. When I click the link that Xively sent to my email, I encounter a '400 Bad request' error from my phone's browser.

Now, I am wondering if Xively allow uploading data from where I live. Is there in restrictions they've set as to the country where the data originates? Or, does it work in all countries, for that matter?

Would greatly appreciate your input, and the inputs of other readers as well.

Thanks.

I don't know about posting data to xively from different countries, you should contact them directly about that.

Also, did you set the account permissions to "READ,UPDATE", as I mentioned last time? And make sure the account is deployed and activated. Sometimes there are just too many buttons to push, and too much jargon to deal with with these things. I'm always scratching my head and saying "What the heck does that mean?"

Rucus:
Is there in restrictions they've set as to the country where the data originates?

Surely not. There is stuff coming from everywhere and xively clearly doesn't mind where it comes from. If you live in North Korea, the problem is at your end and you probably already know all about it.

Thanks for your inputs, oric_dan and Nick_Pyner.

@oric_dan, yes, I set the API key to "READ, UPDATE, CREATE, DELETE" (default). I just use the key generated by the system (e.g. Auto-generated test_device device key for feed 17537239).

By the way, I just changed 4 parameters in the example code provided by Xively (DatastreamUpload.ino): [1] MAC Address, [2]Feed ID, [3]API Key, [4]Name of Channel. Are there any other parameter/s that I need to change/modify?

Thanks! :slight_smile:

By the way, I just changed 4 parameters in the example code provided by Xively (DatastreamUpload.ino): [1] MAC Address, [2]Feed ID, [3]API Key, [4]Name of Channel. Are there any other parameter/s that I need to change/modify?

I don't even mess with these things, just use the MAC address which can be any value, and let the router DHCP feature assign the IP address.

As you will remember, I got your original sketch to work right off by simply plugging in my own API key and feed ID, so I think that's where the problem is. Are you sure you're using the correct API key, and not one of those other long random string things, like Product ID, Product Secret, Serial Number, or Activation Code?

Are you sure your ethernet shield is actually getting out to the internet at all? You can run the "File > Examples > Ethernet > WebClient" sketch to test that.

Hi,
Finally got my device to work with Xively last night. My bad really -- I created my own API Key and then copy the 'name' that I created instead of the string of tiny gray-colored letters and numbers below it.
Thanks for all the help! oric_dan, thanks!

Hi Jean-Robert,
Did you install the HttpClient and the Xively libraries in your Arduino IDE?

(http://arduino.cc/en/Guide/Libraries)

Jean-Robert, you should go back to your posts and modify them by wrapping the code using the '#' icon on the edit screen, then they look like this,

... code here ...

Also, you should not be posting your API key online - although you can go back and change it once things are working correctly.

Also, if you look back at the very first post in this thread, Rucus had a working sketch right there, and he only needed to have the proper API key and feed# to be able to upload data to xively - plus a properly setup xively account, of course.

Rucus:
Hi,
Finally got my device to work with Xively last night. My bad really -- I created my own API Key and then copy the 'name' that I created instead of the string of tiny gray-colored letters and numbers below it.
Thanks for all the help! oric_dan, thanks!

Good to see it's working now.

Hi

Thanks for the answer. Next time i will wrap the code.

Well this what i did. I took the rucus's sketch and i changer the API key and the feed number for mine. The problem is when i upload the sketch to my Arduino one board, i have the error message. You can see the error message a few post before in a screen shot !

What yours idea on that problem.

BTW sorry for my English but i am doing my best.

Again thanks for helping and i will follow your recommandations on how to post on the forum.

Jean-Robert

Jean-Robert, I tried both of your sketches, and they both work properly. If you're not getting data posted online, then looks like something is not correct in your home router setup.

See the feeds, at about 1200 hrs today. I changed the sensor-ID to "dust" in the 2nd sketch, so both channels are showing data.
https://xively.com/feeds/195755639

You can change your API key now, ;-).

Jean-Robert, I looked at your error message. Like Rucus said, the first thing that comes to mind is the libraries are not installed properly. They should be in your Arduino sketch directory in a subdirectory named "libraries", and not in the Arduino IDE directory.

Secondly, I find these 3rd party libraries often uncompress 1 subdirectory too deep. The actual library files should be found in .\libraries\Xively\XivelyFeed.cpp, etc.

I'm shutting off the feed to your account now, :-).