I've implemented successfully in the past, but all of a sudden stopped working, an integration between Arduino with Ethernet Shield, communicating to cosm.com to put live the value of a temp sensor. I'm sure my code didn't change, but after starting my app up after a few weeks again, instead of successfully doing a "put" command to the cosm API, I get -1 returned. I'm not sure what that means in order to try and fix it.

I've confirmed that my Arduino+Ethernet works, by loading the example WebServer sketch, and I got the desired results when going to the IP (in this case

When I go to the cosm.com API debug page, I'm not seeing my communications hitting their site, so I'm sure somewhere between my Arduino and the cosm API there's something broken. And, yes... my internet is working, and yes the Ethernet card has access to it (I've had it call one of my servers and it logged the hit in apache)

Here's the code for my sketch:


#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <OneWire.h>

// COSM Setup:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 178 };
//IPAddress ip(192,168,0,177);
char sensorId[] = "DS18B20";
CosmDatastream datastreams[] = {
  CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
// Finally, wrap the datastreams into a feed
CosmFeed feed(95874, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);

// 1-Wire Setup:
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
float mavg5readings = -1000;
float mavg10min = -1000;
float mavg60min = -1000;
float mavg12h = -1000;
float mavg24h = -1000;
#define REFRESHRATE = 1000;

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2

void setup(void) {
  Serial.println("Serial Port Reset!");
  pinMode(3, OUTPUT);

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

  // make sure we don't get bullshit from the device when we reboot it
  for (int i = 0; i < 3; i++) {
    digitalWrite(3, HIGH);
    float temperature = getTemp();
    digitalWrite(3, LOW);
    Serial.print("Sync Setup Temperature: ");


IPAddress server(216,52,233,121); // api.cosm.com

void loop(void) {

  digitalWrite(3, HIGH);
  digitalWrite(3, LOW);

  float temperature = getTemp();
  if (mavg10min <= -1000) { // i.e. we haven't set it yet
    mavg10min = temperature;
    mavg60min = temperature;
    mavg12h = temperature;
    mavg24h = temperature;
    mavg5readings = temperature;
  } else {
    mavg10min = (mavg10min*600-mavg10min+temperature)/600;
    mavg60min = (mavg60min*3600-mavg60min+temperature)/3600;
    mavg12h = (mavg12h*43200-mavg12h+temperature)/43200;
    mavg24h = (mavg24h*86400-mavg24h+temperature)/86400;
    mavg5readings = (mavg5readings*5-mavg5readings+temperature)/5;
  Serial.print(" (10min: ");
  Serial.print(" | 60min: ");
  Serial.print(" | 12h: ");
  Serial.print(" | 24h: ");

  if (temperature < 50) { // we had a post of 85 when we restarted it once

    if (client.connect(server, 80)) {
      Serial.print("Read sensor value ");

      Serial.println("Uploading it to Cosm");
      digitalWrite(3, HIGH);
      int ret = cosmclient.put(feed, cosmKey);
      digitalWrite(3, LOW);
      Serial.print("cosmclient.put returned ");

    } else {
      for (int i = 0; i < 10; i++) {
        digitalWrite(3, HIGH);
        digitalWrite(3, LOW);


  for (int i = 0; i < 5; i++) {
    digitalWrite(3, HIGH);
    digitalWrite(3, LOW);
    delay(5000); //just here to slow down the output so it is easier to read

float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      return -1001;

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1002;

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1003;

  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.write(0xBE); // Read Scratchpad

  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();


  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;

  return TemperatureSum;


So, this code section is the first place things goes wroing: int ret = cosmclient.put(feed, cosmKey); When I hit that, I'm supposed to get a 200 or even 404 code back, but instead I'm getting -1.

Then, in my loop where I try to post the result of my temp sensor, I try to connect to the server on port 80, and if that fails I just loop a little for effect on an LED so I can see what's cooking without having to troll the serial port:

if (client.connect(server, 80)) {
  • \$\begingroup\$ By the way, please ignore all the maths that I'm doing for averages, it's just something I'm playing with to see if I can't "smooth out" the graphs ... especially seeing that I'm posting the data every few seconds :P \$\endgroup\$ – Sarel Feb 17 '13 at 19:09
  • \$\begingroup\$ Do you have a Ethernet hub, so you can sniff the network traffic? Some Ethernet switches can also be configured to aggregate and copy all packets to a single port, but I'm unsure what it is called. Probably you've been bitten by a changed firewall or API. \$\endgroup\$ – jippie Feb 17 '13 at 20:09

That's because the -1 indicates there is an issue with the ethernet card in your Arduino, so the request isn't getting out.

I found this helped, it shows me where it barfs. -1 looks like it can't create the socket. You could take a look and see if you have

 if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {

on line 44 of [your arduino dir]\libraries\Ethernet\EthernetClient.cpp

as if you have the original

if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {

then if the server hangs on close wait you get to run out of sockets and can't make any more, and get the -1

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.