If statement is not executing

Hi everyone,

I am currently working on a project that receives data from a web server. I want to then put the results into a float array on my Arduino Mega.

I am able to receive data from the server; however, I was not able to put the results into the array. For some reason, the " if serverAccounts[x] == ','" statement never executes.

What am I doing wrong???

Thank you in advance :slight_smile:

p.s. this is only a snippet of the code, but I am sure that the problem is caused by this part of the file.

void getData(){
    esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

if( esp.find("OK")) {

Serial.println("TCP connection ready");

} delay(50);

String postRequest =

"POST " + uri2 + " HTTP/1.0\r\n" +

"Host: " + server + "\r\n" +

"Accept: *" + "/" + "*\r\n" +

"Content-Length: " + data.length() + "\r\n" +

"Content-Type: application/x-www-form-urlencoded\r\n" +

"\r\n" + data;

String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

delay(500);

if(esp.find(">")) { Serial.println("Sending.."); lcd.clear(); lcd.print("Connecting..."); esp.print(postRequest);

if( esp.find("SEND OK")) { Serial.println("Packet sent");

while (esp.available()) {

String tmpResp = esp.readString();
size_t startOfData = tmpResp.indexOf("Results=");
size_t endOfData = tmpResp.indexOf("CLOSED");
serverAccounts = tmpResp.substring(startOfData+8, endOfData);
Serial.println(tmpResp);
Serial.println(serverAccounts);
Serial.println(serverAccounts.substring(2,5));

}

for (int x=0; x<= serverAccounts.length();x++){

  if (serverAccounts[x] == ","){
    
    size_t endOfNumber = serverAccounts.indexOf(",", x+1);
    String section = serverAccounts.substring(x+2, endOfNumber);
    Serial.print("Section: ");
    Serial.println(section);
    float value = section.toFloat();
    Serial.println(value);
    accounts2[index] = value;
    index+=1;
  }
}


// close the connection
lcd.clear();
lcd.print("Success!");
esp.println("AT+CIPCLOSE");

}

Use single quotes, not double.

what is esp.print?

As Wildbill pointed out, the statement:

   if (serverAccounts[x] == ","){

is checking one element (I assume it's in a char array) against the string which has two chars (the ',' plus the null termination character '\0'). Try:

   if (serverAccounts[x] == ','){        // Note single quote marks