Httpclient: make output behave like a INT

Hi all,

I have broken my head around this (hopefully simple) program.

I have a website that outputs a number depending on what information is given on the URL.

The website only states a number, nothing else.

I want to subtract from that number, and therefore i need the number as a int in my sketch.

I use the SimpleHttpExample sketch.

Here is the code:

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

// This example downloads the URL "http://arduino.cc/"

// Name of the server we want to connect to
const char kHostname[] = "arduino.cc";
// Path to download (this is the bit after the hostname in the URL
// that you want to download
const char kPath[] = "/";

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

// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 1000;

void setup()
{
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

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

void loop()
{
  int err =0;
  
  EthernetClient c;
  HttpClient http(c);
  
  err = http.get(kHostname, kPath);
  if (err == 0)
  {
    Serial.println("startedRequest ok");

    err = http.responseStatusCode();
    if (err >= 0)
    {
      Serial.print("Got status code: ");
      Serial.println(err);

      // Usually you'd check that the response code is 200 or a
      // similar "success" code (200-299) before carrying on,
      // but we'll print out whatever response we get

      err = http.skipResponseHeaders();
      if (err >= 0)
      {
        int bodyLen = http.contentLength();
        Serial.print("Content length is: ");
        Serial.println(bodyLen);
        Serial.println();
        Serial.println("Body returned follows:");
      
        // Now we've got to the body, so we can print it out
        unsigned long timeoutStart = millis();
        char c;
        // Whilst we haven't timed out & haven't reached the end of the body
        while ( (http.connected() || http.available()) &&
               ((millis() - timeoutStart) < kNetworkTimeout) )
        {
            if (http.available())
            {
                c = http.read();
                // Print out this character
                Serial.print(c);
               
                bodyLen--;
                // We read something, reset the timeout counter
                timeoutStart = millis();
            }
            else
            {
                // We haven't got any data, so let's pause to allow some to
                // arrive
                delay(kNetworkDelay);
            }
        }
      }
      else
      {
        Serial.print("Failed to skip response headers: ");
        Serial.println(err);
      }
    }
    else
    {    
      Serial.print("Getting response failed: ");
      Serial.println(err);
    }
  }
  else
  {
    Serial.print("Connect failed: ");
    Serial.println(err);
  }
  http.stop();

  // And just stop, now that we've tried a download
  while(1);
}

So my initial approach was to use the following:

char collectChars=collectChars + C;
unsigned int myNewInt=atoi(collectChars);

Any ideas ?

I have a website that outputs a number depending on what information is given on the URL.

No, you don't. The server outputs text. It is up to you to collect the text in an array, and then convert the array to an int.

The website only states a number, nothing else.

I really find that difficult to believe. Your script may only output a number AS TEXT, but the server may be adding additional information before the script output.

      err = http.skipResponseHeaders();

If the server didn't add headers, you wouldn't need to call this function. 8)

I use the SimpleHttpExample sketch.

That you got from?

                // Print out this character
                Serial.print(c);

So, what does this actually print?

So my initial approach was to use the following:

complete nonsense. When you declare a local variable, it is not given an initial value. Adding anything to whatever useless crap was stored in that memory location last time is pointless.

Adding characters doesn't make a lot of sense, either. The + operator is NOT a concatenation operator. Even if it was, a char can hold ONE character. Concatenating one character to one character, and storing the result in a one character variable is not going to work.

atoi() takes a NULL terminated ARRAY of chars, not a single char.

Robin2 has a tutorial on collecting Serial data, and parsing the resulting data. The process is exactly the same when collecting client data.

I dont understand how to implement Robin2’s tutorial to my sketch ?

Is it better to map out the ASCII characters from 0 to 9 in order to write it to a arduino int ?

I dont understand how to implement Robin2's tutorial to my sketch ?

Is that a statement or a question? Looks like a statement to me. Statements do NOT end with ?

Is it better to map out the ASCII characters from 0 to 9 in order to write it to a arduino int ?

If you know what you are doing, that will work.