0
votes

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?

2

2 Answers

0
votes

For your test, you should do force line="001" with the quotes to be a string, not an int. This is why it prints 1 and not 001

For the usage of line.substring(0) == "1", line.substring(0) will return "001" not "1". The correct way to do would be line.substring(0,1) == "1" to only read the first character.

0
votes

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.