Search code examples
arduinoesp32

String and substring - Arduino, esp32


What am I doing wrong with line = client.readStringUntil('\r'); and if (line.substring(0) == "1");

 // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
  line = client.readStringUntil('\r'); //    String
    Serial.println(line); 
  }

// Rele 1

  if (line.substring(0) == "1") 
 {
    Serial.println("Rele 1 ON");
    digitalWrite(Rele_1, LOW);
    myBr1 = 1;
 }
 else if (line.substring(0) == "0") 
 {
    Serial.println("Rele 1 OFF");
    digitalWrite(Rele_1, HIGH);
    myBr1 = 0;
 }
 else
 {
    Serial.println("Rele 1 OFF the charts - Check what you give me....");
    digitalWrite(Rele_1, HIGH);
    myBr1 = 0;
 }

When I run this code, serial printline gives me: 000 but Relay 1 gives me: Rele 1 OFF the charts - Check what you give me.... If I force line=001; Serial print gives me 1 back, not 001 I have 2 relays now and a bit to start OTA update. Will be adding more relays. What am I mixing up, and how do i correct it?


Solution

  • I learned the diffrence between '0' and "0" and 0 today. By changing:

      if (line.substring(0) == "1") 
     {
    

    To

     if (line.charAt(1) == '1')   // Bryter 1. 
        {
    

    That solved my problem.