1
\$\begingroup\$

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 192.168.0.178)

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

#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 cosmKey[] = "k8G6bvz0f_PCO_TOOK_PART_OF_THIS_AWAY_FOR_THE_USUAL_REASONS_FwQT0g";
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.begin(9600);
  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: ");
    Serial.println(temperature);
    delay(1000);

  }
}

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

void loop(void) {
  delay(5000);

  digitalWrite(3, HIGH);
  delay(100);
  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(temperature);
  Serial.print(" (10min: ");
  Serial.print(mavg10min);
  Serial.print(" | 60min: ");
  Serial.print(mavg60min);
  Serial.print(" | 12h: ");
  Serial.print(mavg12h);
  Serial.print(" | 24h: ");
  Serial.print(mavg24h);
  Serial.print(")");
  Serial.print("\n\r");

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

    if (client.connect(server, 80)) {
      datastreams[0].setFloat(mavg5readings);
      Serial.print("Read sensor value ");
      Serial.println(datastreams[0].getFloat());

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

      Serial.println();
    } else {
      for (int i = 0; i < 10; i++) {
        digitalWrite(3, HIGH);
        delay(100);
        digitalWrite(3, LOW);
        delay(50);
      }
      Serial.println("HUH?");
    }

  }

  for (int i = 0; i < 5; i++) {
    digitalWrite(3, HIGH);
    delay(50);
    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
      ds.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.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

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


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

  ds.reset_search();

  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)) {
\$\endgroup\$
  • \$\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
0
\$\begingroup\$

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

\$\endgroup\$

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.