Search array and convert to integers?

Hello,
Im trying to convert data being posted from a server to an ethernet board into integer values.

This is what I get in the serial monitor…

Ready
Client request: POST data:
firste:-1seconde:-1en:-1
Sending response
Returning Information to server...done
Client request: POST data:
firste:-1seconde:-1en:-1a=255&b=111&c=C
Sending response
Returning Information to server...done

“a=255&b=111&c=C” is what i am attempting to convert into two interger variables 255 and 111,

the code on the board is…

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 64

byte mac[] = {0x1c, 0x26, 0x0c, 0x98, 0x22, 0x85 };  
byte ip[]  = {192, 168, 1 , 20 };             
byte gateway[] = {192, 168, 1, 1 };  
byte subnet[]  = {255, 255, 255, 0 };

EthernetServer server(80);

String inString = String(maxLength);
char luxbuf[6];

const int photocellPin = A0;
int photocellReading;          
const float Res=10.0;

const int Pin9 = 9; 
const int Pin8 = 8;
const int Pin7 = 7;

int lux = 0;
int val;
int lightlux = 0;
int lightvol = 0;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  
  pinMode(Pin9, OUTPUT);
  pinMode(Pin8, OUTPUT);
  pinMode(Pin7, OUTPUT);
  
  pinMode(photocellPin, INPUT);
  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  int bufLength;
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int a,b;
    char *pch;
    
    Serial.print("Client request: ");
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        
        
        if (c == '\n' && currentLineIsBlank) {
          
        if(tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }
          
          
          // retrieve the POST data
          Serial.print("POST data: ");
          Serial.print(tBuf);
          
          inString = String(tBuf);
          
          int firste = inString.indexOf('=');
          Serial.print("firste:");
          Serial.print(firste);
          int seconde = inString.indexOf('=', firste);
          Serial.print("seconde:");
          Serial.print(seconde);
          int en = inString.indexOf("C", firste);
          Serial.print("en:");
          Serial.print(en);
          if (firste > -1) {
          bufLength = (seconde-5);
            if(bufLength > 6){  
              bufLength = 6;
            }
          inString.substring((firste+1), (seconde-1)).toCharArray(luxbuf, bufLength);
          lightlux = atoi(luxbuf);
          Serial.print("lightlux:");
          Serial.print(lightlux);
          lightvol = 51*(lightlux/1333);
          Serial.print("lightvol:");
          Serial.print(lightvol);
          
          bufLength = (en - (seconde + 1));
            if(bufLength > 6){  
              bufLength = 6;
            }
          inString.substring((seconde+1), (en-1)).toCharArray(luxbuf, bufLength);
          val = atoi(luxbuf);
          Serial.print("val:");
          Serial.print(val);
          }
        
        
          while(client.available()) Serial.write(client.read());
          Serial.println();
          
          Serial.println("Sending response");
          Serial.print("Returning Information to server...");
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          photocellReading = analogRead(photocellPin);
          float Vout=photocellReading*0.0048828125;
          lux=500/(Res*((5-Vout)/Vout));
          
          client.print("Lux Value is : ");
          client.print(lux);
          client.print(" Current Voltage is: ");
          client.print(lightvol);
          client.print(" END");
          client.println("
");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    if (val != 0) {
    Serial.print("adjusting lights...");
    while (lightlux < lux){
      lightvol = lightvol + .1;
      adjustlight();
    } 
      while (lightlux > lux){
      lightvol = lightvol - .1;
      adjustlight();
    }
    }
    Serial.println("done");
  }
}

void adjustlight() {
            Serial.print("adjust light function starting with val: ");
            Serial.print(val);
            if (val == '000') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
            if (val == '111') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
            if (val == '100') { analogWrite(Pin9, lightvol);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
            if (val == '010') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
            if (val == '001') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, lightvol);}
            if (val == '110') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
            if (val == '011') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
            photocellReading = analogRead(photocellPin);  
            float Vout=photocellReading*0.0048828125;
            lux=500/(Res*((5-Vout)/Vout));
}

ive tried a few different things, like moving inString to where the buffer takes in charachters, but everyone returns int firste = -1

I dont care how the values are converted, just want to do it.

Any and all help is appreciated.

Instead of using the String class, you can use strtok() to split the string at given points.

  char * pch;
  pch = strtok (tBuf,"="); //Seperate the string, getting the first token.
  int numbers[10] = {0};
  int index = 0;
  while (pch != NULL)
  {
    //Find each = sign in turn.
    numbers[index] = atoi(pch); //Note pch will be a string like: "255&b=". Atoi() will stop converting when it reaches a non number character, i.e. the &.
    index++;
    if(index == 10){
      break; //Avoid overflowing the buffer.
    }
    pch = strtok (NULL, "="); //Get the next segment of the string.
  }

So just to check numbers[0] would = 255 and not something like numbers[0] = {2,5,5};

And also, forgot this important part...

later on the script checks the second value (in this case 111) to turn some lights on or off, should I change the checks for those from

if (val == '000') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
 if (val == '111') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}

to

if (val = 0) { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
 if (val = 111) { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}

Yeah, it would be an integer.

The atoi() function converts a string like "255", to an integer. You may find that numbers[0] is always zero, and the case of that string, numbers[1] = 255, you would have to check and see, I cant remember off the top of my head.

Ok, So I just need to change the second part to. Thank you for the help.

Yes to the second part.

'000' Doesn't actually mean what you think it does. It converts 000 into a character, which i think is actually the number 3158064

R.E. the first number being 0, it depends on what else is in the string. For example everything before the first '=' is taken as a string and converted. If this doesn't contain useful information, you get 0, which could leave you with lots of random 0's about the place. You may want to perform a check to make sure the strtok string is a valid number.

Example:

  char * pch;
  pch = strtok (tBuf,"="); //Seperate the string, getting the first token.
  int numbers[10] = {0};
  int index = 0;
  while (pch != NULL)
  {
    //Find each = sign in turn.
    char cset[] = "1234567890"; //valid numbers. strspn looks at each character in the string. If a char is part of this set, it adds one to a variable and moves to the next. If it doesn't belong to this set, then it stops and returns the value of its variable. If it is 0, then atoi() would be given nonsense, so we skip it.
    if(strspn (pch,cset)){ //filter out any strings which are not valid to avoid atoi() being given a non numeric string
      numbers[index] = atoi(pch); //Note pch will be a string like: "255&b=". Atoi() will stop converting when it reaches a non number character, i.e. the &.
      index++;
    }
    if(index == 10){
      break; //Avoid overflowing the buffer.
    }
    pch = strtok (NULL, "="); //Get the next segment of the string.
  }

IM getting some random zeros still… Ive tried changing which numbers to use, but they all still have a value of zero.

What I get in the serial monitor…

Ready
Client request: POST data:
Numbers 0:
0Numbers 1:
0Numbers 2:
0

Sending response
Returning Information to server...adjusting lights...adjust light function starting with val:
0with lightvol:
0with luxlight:
0with lux:
2adjust light function starting with val:
11with lightvol:
0with luxlight:
0with lux:
2adjust light function starting with val:
11with lightvol:
0with luxlight:
0with lux:
2                                 "continues in loop in serial monitor"
2done
Client request: POST data:
Numbers 0:
0Numbers 1:
0Numbers 2:
0a=255&b=111&c=C

Sending response
Returning Information to server...adjusting lights...adjust light function starting with val:
0with lightvol:
0with luxlight:
0with lux:
2adjust light function starting with val:
11with lightvol:
0with luxlight:
0with lux:
2done

The code on the board…

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 64

byte mac[] = {0x1c, 0x26, 0x0c, 0x98, 0x22, 0x85 };  
byte ip[]  = {192, 168, 1 , 20 };             
byte gateway[] = {192, 168, 1, 1 };  
byte subnet[]  = {255, 255, 255, 0 };

EthernetServer server(80);

String inString = String(maxLength);
char luxbuf[6];

const int photocellPin = A0;
int photocellReading;          
const float Res=10.0;

const int Pin9 = 9; 
const int Pin8 = 8;
const int Pin7 = 7;

int lux = 0;
int val = 22;
int lightlux = 0;
int lightvol = 0;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  
  pinMode(Pin9, OUTPUT);
  pinMode(Pin8, OUTPUT);
  pinMode(Pin7, OUTPUT);
  
  pinMode(photocellPin, INPUT);
  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  int bufLength;
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int a,b;
    char *pch;
    
    Serial.print("Client request: ");
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        
        
        if (c == '\n' && currentLineIsBlank) {
          
        if(tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }
          
          
          // retrieve the POST data
          Serial.print("POST data: ");
          Serial.print(tBuf);
          
          char * pch;
          pch = strtok (tBuf,"="); //Seperate the string, getting the first token.
          int numbers[10] = {0};
          int index = 0;
          while (pch != NULL)
          {
          //Find each = sign in turn.
           char cset[] = "1234567890"; //valid numbers. strspn looks at each character in the string. If a char is part of this set, it adds one to a variable and moves to the next. If it doesn't belong to this set, then it stops and returns the value of its variable. If it is 0, then atoi() would be given nonsense, so we skip it.
             if(strspn (pch,cset)){ //filter out any strings which are not valid to avoid atoi() being given a non numeric string
               numbers[index] = atoi(pch); //Note pch will be a string like: "255&b=". Atoi() will stop converting when it reaches a non number character, i.e. the &.
               index++;
             }
            if(index == 10){
              break; //Avoid overflowing the buffer.
            }
          pch = strtok (NULL, "="); //Get the next segment of the string.
          }
          lightlux = numbers[0];
          Serial.println("Numbers 0: ");
          Serial.print(numbers[0]);
          Serial.println("Numbers 1: ");
          Serial.print(numbers[1]);
          Serial.println("Numbers 2: ");
          Serial.print(numbers[2]);
          
          lightvol = 51*(lightlux/1333);
          val = numbers[1];
        
          while(client.available()) Serial.write(client.read());
          Serial.println();
          
          Serial.println("Sending response");
          Serial.print("Returning Information to server...");
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          photocellReading = analogRead(photocellPin);
          float Vout=photocellReading*0.0048828125;
          lux=500/(Res*((5-Vout)/Vout));
          
          client.print("Lux Value is : ");
          client.print(lux);
          client.print(" Current Voltage is: ");
          client.print(lightvol);
          client.print(" END");
          client.println("
");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    if (val != 22) {
    Serial.print("adjusting lights...");
    int cnt = 0;
    while (lightlux < lux  && lightvol <= 5){
      lightvol = lightvol + .1;
      adjustlight();
      cnt = cnt + 1;
      if (cnt == 20) {
        break;
      }
    } 
    cnt = 0;
      while (lightlux > lux && lightvol >= 0 ){
      lightvol = lightvol - .1;
      adjustlight();
      cnt = cnt + 1;
      if (cnt == 20) {
        break;
      }
    }
    }
    Serial.println("done");
  }
}

void adjustlight() {
            Serial.println("adjust light function starting with val: ");
            Serial.print(val);
            Serial.println("with lightvol: ");
            Serial.print(lightvol);
            Serial.println("with luxlight: ");
            Serial.print(lightlux);
            Serial.println("with lux: ");
            Serial.print(lux);
            if (val = 0) { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
            if (val = 111) { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
            if (val = 100) { analogWrite(Pin9, lightvol);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
            if (val = 10) { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
            if (val = 1) { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, lightvol);}
            if (val = 110) { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
            if (val = 11) { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
            photocellReading = analogRead(photocellPin);  
            float Vout=photocellReading*0.0048828125;
            lux=500/(Res*((5-Vout)/Vout));
}

You aren’t actually adding any data to tBuff, which means there is nothing there to process (hence all 0’s).

I’m not sure I quite follow the example that I presume you have based your code on, but I think this is correct:

        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          
          Serial.println();
          //You have to actually read the data *before* processing it
          while(client.available()){
            if(tCount < 63){
              tBuf[tCount++] = client.read(); //add to the next space in tCount
            } else {
              client.read(); //too many to fit in tCount, so just read and ignore the rest
            }
          }
          tBuf[tCount] = 0; //Add the null
          tCount = 0;
          // retrieve the POST data
          Serial.print("POST data: ");
          Serial.println(tBuf);
        
          char * pch;
          pch = strtok (tBuf,"="); //Seperate the string, getting the first token.
          int numbers[10] = {0};
          int index = 0;
          while (pch != NULL) {
            //Find each = sign in turn.
            char cset[] = "1234567890"; //valid numbers. strspn looks at each character in the string. If a char is part of this set, it adds one to a variable and moves to the next. If it doesn't belong to this set, then it stops and returns the value of its variable. If it is 0, then atoi() would be given nonsense, so we skip it.
            if(strspn (pch,cset)){ //filter out any strings which are not valid to avoid atoi() being given a non numeric string
              numbers[index] = atoi(pch); //Note pch will be a string like: "255&b=". Atoi() will stop converting when it reaches a non number character, i.e. the &.
              index++;
            }
            if(index == 10){
              break; //Avoid overflowing the buffer.
            }
            pch = strtok (NULL, "="); //Get the next segment of the string.
          }
          lightlux = numbers[0];
          Serial.print("Numbers 0: ");
          Serial.println(numbers[0]);
          Serial.print("Numbers 1: ");
          Serial.println(numbers[1]);
          Serial.print("Numbers 2: ");
          Serial.println(numbers[2]);
            
          lightvol = 51*(lightlux/1333);
          val = numbers[1];
        
          
          Serial.println("Sending response"); //This is part of your original code
...
String inString = String(maxLength);

This will create a String object containing the text "64". How is that useful?

All of the stuff you are doing with the String instance can be done with the tChar array, saving countless headaches when the String class abuses all of your memory.