condition if not work

:confused: :confused:

Hello guys

I'm doing a project home automation.
I am using the following condition, but does not. Because?

code:

if(readString.indexOf("AlarmeOn")>=0 && digitalRead(9)==HIGH)
{

digitalWrite(alarme, HIGH);

}

Either your read string does not contain "AlarmeOn" or pin 9 is LOW, Other than that it is difficult to say without seeing the content of the string and your full code.

Hi,
Check your spelling of your variables, uppercase and lower case is important.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom...... :slight_smile:

When you have problems with if statements not doing what is expected it is helpful to print the conditions involved in the test so that you can see their value(s). To do this is is helpful to put the values into variables. When you print the values print a label so that you know what you are looking at and start/end markers such as < and > so that you can see where the value of the variable starts and ends.

Also, the string class is well known for causing memory problems on the Arduino platform. You should avoid it and use char arrays instead.

the piece of code that works is not within ****

#include <SPI.h>
#include <String.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; // Endereço Mac
byte ip[] = { 192, 168, 1, 2 }; // Endereço de Ip da sua Rede
byte gateway[] = {192,168,1,1};
byte subnet[] = {255,255,255,0};
EthernetServer server(8090); // Porta de serviço

int alarme=2;// Pino onde deve ser ligado o led de teste
int janela_coz=9;

String readString = String(30); // string para buscar dados de endereço
boolean jan_coz = false; // Variável para o status do led 
boolean activo=false;

void setup(){
  // Inicia o Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  // Define o pino como saída
  pinMode(alarme, OUTPUT);
  // Inicia a comunicação Serial
  Serial.begin(9600); 
}

void loop(){
  // Criar uma conexão de cliente
  EthernetClient client = server.available();
  
    
  
  if (client) {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        // ler caractere por caractere vindo do HTTP
        if (readString.length() < 30)
        {
          // armazena os caracteres para string
          readString += (c);
        }
        
        //se o pedido HTTP terminou
        if (c == '\n')
          {
          // vamos verificar se o LED deve ser ligado
          // Se a string possui o texto L=Ligar
          //****************************************************************************
           if(readString.indexOf("Ligar_Alarme")>0 && digitalRead(janela_coz)==HIGH)
          {
            digitalWrite(alarme,HIGH);
            jan_coz = true;
         
          }
//***********************************************************************************
          // Se a string possui o texto L=Desligar
          if(readString.indexOf("Desli_Alarme")>=0)
          {
            // O Led vai ser desligado
            digitalWrite(alarme, LOW);
            jan_coz = false;
          }

        // dados HTML de saída começando com cabeçalho padrão
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
        
        client.print("<font size='20'>");
        if (jan_coz) {
          client.print("AlarmeLigado");
        } else {
          client.print("AlarmeDesligado");
        }
        
        //limpa string para a próxima leitura
        readString="";
        
        // parar cliente
        client.stop();
        }
      }
    }
  }
}

What do you see if you print readString.indexOf("Ligar_Alarme") and digitalRead(janela_coz) before the if statement ?

How do you know that the code is even reaching that section ? Suppose that c never equals '\n' what would happen ? Where is the serial input coming from ?

if janela_coz == HIGH and then readString.indexOf ("Ligar_Alarme")> = 0 the condition is made. If not contrary.

#include <SPI.h>
#include <String.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; // Endereço Mac
byte ip[] = { 192, 168, 1, 2 }; // Endereço de Ip da sua Rede
byte gateway[] = {192,168,1,1};
byte subnet[] = {255,255,255,0};
EthernetServer server(8090); // Porta de serviço

int alarme=2;// Pino onde deve ser ligado o led de teste
int janela_coz=9;

String readString = String(30); // string para buscar dados de endereço
boolean jan_coz = false; // Variável para o status do led
boolean activo=false;

void setup(){
  // Inicia o Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  // Define o pino como saída
  pinMode(alarme, OUTPUT);
  // Inicia a comunicação Serial
  Serial.begin(9600);
}

void loop(){
  // Criar uma conexão de cliente
  EthernetClient client = server.available();
 
   
 
  if (client) {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        // ler caractere por caractere vindo do HTTP
        if (readString.length() < 30)
        {
          // armazena os caracteres para string
          readString += (c);
        }
       
        //se o pedido HTTP terminou
        if (c == '\n')
          {
          // vamos verificar se o LED deve ser ligado
          // Se a string possui o texto L=Ligar
          //****************************************************************************
 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!       

[b]Serial.println(readString.indexOf("Ligar_Alarme"));


Serial.println(digitalRead(janela_coz));
[/b]

??????????????????????????????????????????????????????????


  if(readString.indexOf("Ligar_Alarme")>0 && digitalRead(janela_coz)==HIGH)
          {
            digitalWrite(alarme,HIGH);
            jan_coz = true;
         
          }
//***********************************************************************************
          // Se a string possui o texto L=Desligar
          if(readString.indexOf("Desli_Alarme")>=0)
          {
            // O Led vai ser desligado
            digitalWrite(alarme, LOW);
            jan_coz = false;
          }

        // dados HTML de saída começando com cabeçalho padrão
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
       
        client.print("<font size='20'>");
        if (jan_coz) {
          client.print("AlarmeLigado");
        } else {
          client.print("AlarmeDesligado");
        }
       
        //limpa string para a próxima leitura
        readString="";
       
        // parar cliente
        client.stop();
        }
      }
    }
  }
}