Arduino and Strings..

Hi all,
I need to get some data from my router using an Ethernet Shield.
So i wrote this code:

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

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192,168,1,177 };
byte server = { 192,168,1,1 }; //
Client client(server, 80);

void setup() {
String output = “”;
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println(“connecting…”);
if (client.connect()) {
Serial.println(“connected”);
client.println(“GET /statswan.cmd HTTP/1.0”);
client.println(“Authorization: Basic YWRt**********”);
client.println();
}
else {
Serial.println(“connection failed”);
}
}

void loop()
{
if (client.available()) {
char c = client.read();
output.concat(c);
// Serial.print(c);
}

if (!client.connected()) {
Serial.print(output);
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
for(;:wink:
;
}
}

I wanted to put into “output” the output of my router but when I try to print the variable “output” it shows nothing…
Where’s the problem?
Then I’d like to get the code between the tags

and

but what function should I use?

Thanks and sorry for my bad english!

void setup() {
String output = "";

output is a local variable. It goes out of scope at the end of loop.

   output.concat(c);

You can't concatenate a value onto a variable that has gone out of scope.

Why are you only reading one character from the client?

Why, if no client is connected are you stopping the not-connected client and going into an infinite loop?

You are right! It works…
I’m going into a loop because I’m debugging…
Now I have another problem: at the end of the code I have to multiply the variable “output” (type String) for a number…

I tried using

int number = atoi(output);
int number = int(output);

but they don’t work… so how can I use math with a string?

Thankyou and sorry for my bad english!

The String variable encapsulates a char array. You need to extract that char array, and pass that to atoi.

buffer charArray[10];
output.toCharArray(charArray, 10);
int num = atoi(charArray);

Thank Paul, I'll send you some beer ;) I hope this is my last problem: The code now is

char charArray[10];

void setup() {
....
}
void loop() {
....
....
....
      output.toCharArray(charArray, 10);
      unsigned long num = atoi(charArray); 
      //Serial.println(millis());
      Serial.print("String output: ");
      Serial.print(output);
      Serial.print(" after atoi(): ");
      Serial.println(num);

And in the serial monitor i get

String output: 0 after atoi(): 0 String output: 80 after atoi(): 80 String output: 17747 after atoi(): 17747 String output: 13795 after atoi(): 13795 String output: 30870 after atoi(): 30870 String output: 15141 after atoi(): 15141 String output: 46500 after atoi(): 4294948260 ??? WTF ? String output: 88500 after atoi(): 22964 ???? Again String output: 30000 after atoi(): 30000 String output: 111000 after atoi(): 4294947224 ?? String output: 33000 after atoi(): 4294934760 ?? String output: 46500 after atoi(): 4294948260 ?? String output: 4500 after atoi(): 4500

when the numbers rise the result of atoi() changes..

Thankyou!

Do you have a clue what atoi() does? The a stands for ASCII. It means that the input is an array of characters that look like an integer value, like 3 or -47, not "three" or 29.4.

The i means that the output is an integer. The output should be stored in an int or unsigned int variable, not an unsigned long. "46500" is not an integer. It is a long. If you want to process longs, you need to use a different function.

atol() or strtol() are more suitable functions for converting strings to longs.

The output should be stored in an int or unsigned int variable, not an unsigned long

  1. The output of atoi() may be stored in a long as it will not loose precision. However don’t expect it will convert longs > MAXINT or longs < -MAXINT correctly. as Paul states.
  2. The output of atoi() may not be stored in an unsigned int as atoi() can return negative values, resulting in loss of precision. Same for unsigned long.

Even if you know for sure that there will be no negative numbers be aware that the range of int is not the same as the range of unsigned int.