Convert char Web Request to unsigned int for irsend.sendRaw

How do I convert a char variable to an unsigned int?

My Arduino with ethernet shield receives a web request in the form of:

http://192.168.0.9/6000,1150,1250,1150,650,550,650,550,650,1150,650,550,650,550,650,1150,650,1150,600/

and I am able to get:

6000,1150,1250,1150,650,550,650,550,650,1150,650,550,650,550,650,1150,650,1150,600

into a variable and the last thing I need is to pass that data to irsend.sendRaw which requires an unsigned int.

I have tried this example: http://forums.adafruit.com/viewtopic.php?f=25&t=25264 and many more I found online that use atoi or atol but nothing has worked.

Here is the code I am working with:

void loop()
{

  char clientline[BUFSIZE];
  int index = 0;
  // listen for incoming clients
#if defined(ARDUINO) && ARDUINO >= 100
  EthernetClient client = server.available();
#else
  Client client = server.available();
#endif
  if (client) {

    //  reset input buffer
    index = 0;

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //  fill url the buffer
        if(c != '\n' && c != '\r' && c != '*' && index < BUFSIZE){ // Reads until either an eol character is reached or the buffer is full
          clientline[index++] = c;
          continue;
        }  
#if DEBUG
        Serial.print("client available bytes before flush: "); Serial.println(client.available());
        Serial.print("request = "); Serial.println(clientline);
#endif







        // Flush any remaining bytes from the client buffer
        client.flush();

#if DEBUG
        // Should be 0
        Serial.print("client available bytes after flush: "); Serial.println(client.available());
#endif

        //  convert clientline into a proper
        //  string for further processing
        String urlString = String(clientline);
        
        //  extract the operation
        String op = urlString.substring(0,urlString.indexOf(' '));

        //  we're only interested in the first part...
        urlString = urlString.substring(urlString.indexOf('/'), urlString.indexOf(' ', urlString.indexOf('/')));

        //  put what's left of the URL back in client line
#if CASESENSE
        urlString.toUpperCase();
#endif
        urlString.toCharArray(clientline, BUFSIZE);
#define numBufSize 11
        //  get the first two parameters
        char *SignalCommand = strtok(clientline,"/");
        
        char *value = strtok(NULL,"/");

        
      irsend.sendRaw(SignalCommand,68,38);

If somebody can show me how to convert SignalCommand into an unsigned int that irsend.sendRaw will accept that would be greatly appreciated.

I have stuff here about getting incoming serial data and turning it into numbers:

http://www.gammon.com.au/serial

That's serial, not web, but the concept is the same. Scroll down to "How to send and receive numbers" for some tips.

Thanks for the reply Nick but I am missing how that applies. I have since scrapped the code I had and started over from a different example but am in a similar boat. I have my variable command which is a string that could contain something like 600,456,1586,235 and need to convert that to an unsigned int array.

Any other examples or tips would be greatly appreciated.

#include <SPI.h>
#include <Ethernet.h>
#include <IRremote.h>
IRsend irsend;
#include <string.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,9);

EthernetServer server(80);
String command="";
unsigned int signalCommand;
char c;
int i;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {

  EthernetClient client = server.available();
  if (client) {
      while (client.connected()) {
      while (client.available()) {
        client.read();
 
        for(int i=0; i<5; i++) //This removes the unneeded data before the command
            {
               client.read(); 
            }

          while (c != '\n' && c != '/') // This removes everything from the header execept the arguments
          {
             c = client.read();
             command += c;    
          }
          
          command = command.substring(0, command.length() - 1); // Removes the trailing /
         
          Serial.print("command: ");Serial.println(command);  

          //Convert variable command to unsigned int here
          //
          //
          //
          ///////////
          
          irsend.sendRaw(signalCommand,68,38);
  
          delay(1);
          client.stop();
      }    
      delay(1);
      client.stop();
      Serial.println("client disonnected");
    }
    
  }
}

A simple thing would be to take your line of numbers (and commas) and use strtok to break it up at a comma. The use atoi in the result.

I would advise you to ditch the String class and use C strings.

Please note that, at present, the String library has bugs as discussed here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

This is the code I ended up going with if any body stumbles across this in the future.

#include <SPI.h>
#include <Ethernet.h>
#include <IRremote.h>
IRsend irsend;
#include <string.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,9);

EthernetServer server(80);
String command="";
char charBuf[300];
char c;
int i=0;
int index=0;
unsigned int angle[300];
char delimiters[] = ",";
char* valPosition;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
      while (client.connected()) {
      while (client.available()) {
        client.read();
        
        for(int i=0; i<4; i++) //This removes the unneeded data before the command
            {
               client.read(); 
            }
            
          client.read(); // removes the first / in the argument
          
          while ( c != '\n' && c != '/') // This removes everything from the header execept the arguments
          {
             c = client.read();
             charBuf[index++] = c;
             Serial.print(c);  
          }
          
          charBuf[index]=' ';//This removes the second / at the end of the argument

          valPosition = strtok(charBuf, delimiters); //fist trun of strtok
        
          while(valPosition !=NULL){ //converts all numbers between commas into int
            angle[i] = atoi(valPosition);
            valPosition = strtok(NULL, delimiters);
            i++;
          }
       
          irsend.sendRaw(angle,68,38); // sends infred command

          index=0; // resets variables so universal remote buttons can be pushed again
          c=' ';
          i=0;

          delay(1);
          client.stop();
      }    
      delay(1);
      client.stop();
      Serial.println("client disonnected");
    } 
  }
}

Or, using strtol :

http://codepad.org/vcOgqIq0

Edit: should have used strto*u*l..bah