Home automation

Here is the code for home automation,the IDE compiles it very fine …but when i send a string …it does not…response …

           #include <LiquidCrystal.h>

                    #include <Servo.h>

                            
                  LiquidCrystal lcd( 12, 11, 5, 4, 3, 2);
                  
                  Servo            SERVO ;
                   void setup()
                  {
                    Serial.begin(9600);
                    analogReference(EXTERNAL);
                    lcd.begin(16, 2);
                    SERVO.attach(10);
                    Serial.println("AT\r"); //To check if GSM module is working
                    delay(2000);
                    Serial.println("AT+CMGF=1\r"); // set the SMS mode to text
                    delay(1500); 
                    pinMode(6,OUTPUT);
                    pinMode(7,OUTPUT);
                    pinMode(8,OUTPUT);
                    pinMode(9,OUTPUT);
                    pinMode(13,OUTPUT);
                    
                    
                  }
                  
                  void loop()
                  {
                   int TEMP_read = analogRead(1);                                                                              
                   float temp = ((4.68 * TEMP_read * 100.0) / 1024);
                   lcd.clear();
                   lcd.setCursor(0,0);
                   lcd.print("TempC:");
                   lcd.println(temp);
                   
                   int GAS_read = analogRead(2);
                   
                   if(GAS_read >= 600)
                     {
                     lcd.setCursor(0,1);
                     lcd.print("Gas Detected");
                     delay(500);
                     }
                   if(GAS_read <= 600)
                     {
                     lcd.setCursor(0,1);
                     lcd.print("Gas Not Detected");
                     delay(500);
                     }
                     
                     if(digitalRead(13) ==  HIGH)
                     {
                       lcd.clear();
                       lcd.setCursor(0,0);
                       lcd.print("Motion Detected");
                       Serial.println("Motion Detected");
                       delay(500);
                     }
                     //Serial.print("AT+CMGR=1\r");
                     //delay(1000);
                     //RUN2();
                     if (Serial.available() > 0)
                     {
                       //RUN();
                       RUN2();
                     }
                 }
                  
                  void RUN2()
                      {
                        
                       char c[8]={Serial.read()};
                        
                      if(c == "LOAD1ON")
                      {
                       LOAD1_ON();
                      }
                      if(c == "LOAD1OFF")
                      {
                       LOAD1_OFF();
                      }
                      if(c == "LOAD2ON")
                      {
                       LOAD2_ON();
                      }
                      if(c == "LOAD2OFF")
                      {
                       LOAD2_OFF();
                      }
                      if(c == "LOAD3ON")
                      {
                       LOAD3_ON();
                      }
                      if(c == "LOAD3OFF")
                      {
                       LOAD3_OFF();
                      }
                      if(c == "LOAD4ON")
                      {
                       LOAD4_ON();
                      }
                      if(c == "LOAD4OFF")
                      {
                       LOAD4_OFF();
                      }
                      return;
                      }
                 //}
                   //subroutines
                   
                   void LOAD1_ON()
                   { 
                      digitalWrite(6,HIGH);
                      Serial.println("LOAD1=ON");
                      return;
                   }
                   void LOAD1_OFF()
                   {
                       
                      digitalWrite(6,LOW);
                      Serial.println("LOAD1=OFF");
                      return;
                   }
                   void LOAD2_ON()
                   {
                      Serial.println("LOAD2=ON"); 
                      digitalWrite(7,HIGH);
                      return;
                   }
                   void LOAD2_OFF()
                   {
                       
                      digitalWrite(7,LOW);
                      Serial.println("LOAD2=OFF");
                      return;
                   }
                   void LOAD3_ON()
                   {
                       
                      digitalWrite(8,HIGH);
                      Serial.println("LOAD3=ON");
                      return;
                   }
                   void LOAD3_OFF()
                   {
                       
                      digitalWrite(8,LOW);
                      Serial.println("LOAD3=OFF");
                      return;
                   }
                   void LOAD4_ON()
                   {
                       digitalWrite(9,HIGH);
                      Serial.println("LOAD4=ON");
                      return;
                   }
                   void LOAD4_OFF()
                   {
                      Serial.println("LOAD4=OFF"); 
                      digitalWrite(9,LOW);
                      return;
                   }
                       char c[8]={Serial.read()};

What’s this nonsense? Serial.read() returns one byte, not an array of bytes. You are only populating the first byte of the array.

                      if(c == "LOAD1ON")

The address of the array is never going to equal the address where a literal is stored. You MUST use strcmp(), when you properly read the serial data (AND NULL terminate the array.

will this work... :~....

 void setup(void) 
  {
   
    Serial.begin(9600);   
  }
   
  void loop(void) 
  {
      char c[10];
      c= Serial.read();
                         
     if(strcmp(c,"ON") == 0)
      {
        Serial.print("compared");
      }
 else if (strcmp(c,"yes") == 0)
     {
      Serial.println(" - Dim");
    } 
}

Read what Paul said about what Serial.read returns and answer your own question.
Have you tried printing c to see what its value is before doing the comparison ?

hi…
I really got confused :~ …in.how to read the string of characters …and compare them…
can any one have a good tutorial to study … :frowning:

UKHeliBob:
Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

i like you are link UKHeliBob
But ...still m not ....comfortable..with my code ...where is the mistake...
can u....suggest the required modification/changes in my code... :expressionless:

OK, let's look at just 2 lines of your code
c= Serial.read();Read a single byte from the serial port
if(strcmp(c,"ON") == 0)Will the single byte previously read ever equal "ON" ?
You need to read the bytes coming in and store each of them somewhere. An array is perfect for that. Then, when the incoming message is complete (how will you know that ?) you can compare the whole message with the command that you want to execute.

The 'Buffering input' on Nick Gammon's site shows you how to read incoming data into an array, how to recognise that the end of the message has been reached and how to add a byte to the array to terminate the string that has been read.

You could do a lot worse than use the example as the basis for your program. His process_data() function prints what has been received but you could put your string comparison and actions if matched in that function.

 char inData[4]; // Allocate some space for the string
          char inChar; // Where to store the character read
          byte index = 0; // Index into array; where to store the character
            void setup(void) 
            {
             
              Serial.begin(9600);   
            }
             
            void loop(void) 
            {
               while(Serial.available() > 0)
              {
               if(index < 3) // One less than the size of the array
                 {
                     inChar = Serial.read(); // Read a character
                     inData[index] = inChar; // Store it
                     index++; // Increment where to write next
                     inData[index] = '\0'; // Null terminate the string
                 }
              }
                if(strcmp(inData,"ON")== 0)
                {
                  Serial.print("compared");
                 
                }
                  else if(strcmp(inData,"YES") == 0)
                {
                  Serial.println("Compared yes ");
                 
                }
                 
                
             }

here what happens when i send ON …the terminal shows COMPARED continously not stop…

Shouldn't really be a surprise, once you have "ON" in your array, there's nothing in your code to change it, so on every iteration of loop, that strcmp will see it and you'll print "compared" again. If you don't want to see it again, set the first character in the array to '\0'. You'll probably want to reset index too.

A very simple way to capture serial port incoming characters into a useful format.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}

wildbill:
Shouldn't really be a surprise, once you have "ON" in your array, there's nothing in your code to change it, so on every iteration of loop, that strcmp will see it and you'll print "compared" again. If you don't want to see it again, set the first character in the array to '\0'. You'll probably want to reset index too.

so what would be the required changes... :cold_sweat:

zoomkat:
A very simple way to capture serial port incoming characters into a useful format.

// zoomkat 7-30-11 serial I/O string test

// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

if (readString.length() >0) {
    Serial.println(readString);

readString="";
  }
}

how to modify the code to compare a predefined string
like......
srtcmp(readString,"ON") == 0)

so what would be the required changes…

if(strcmp(inData,“ON”)== 0)
{
Serial.print(“compared”);
inData[0] = ‘\0’;
index = 0;
}

And use Tools + Auto Format. There is no reason for you functions to start half way across the page.

how to modify the code to compare a predefined string
like......
srtcmp(readString,"ON") == 0)

You can use the indexof function to detect the presence of a string/substring like in the code below.

          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }

PaulS:

so what would be the required changes…

if(strcmp(inData,“ON”)== 0)
{
Serial.print(“compared”);
inData[0] = ‘\0’;
index = 0;
}

And use Tools + Auto Format. There is no reason for you functions to start half way across the page.

PaulS…the code is working…very fine… by making the changes…loved u Bro…:*

PaulS:

so what would be the required changes…

if(strcmp(inData,“ON”)== 0)
{
Serial.print(“compared”);
inData[0] = ‘\0’;
index = 0;
}

And use Tools + Auto Format. There is no reason for you functions to start half way across the page.

pauls…in the code…when i send or inputs and wrong chatacter
like…
i send O by mistake then …send ON ( the correct message)
now the program dont respond… :drooling_face: :frowning:

in the code....

That you didn't post...

PaulS:

in the code…

That you didn’t post…

ohh sorry…
here it is…

          char inData[4]; // Allocate some space for the string
          char inChar; // Where to store the character read
          byte index = 0; // Index into array; where to store the character
            void setup(void) 
            {
             
              Serial.begin(9600);   
            }
             
            void loop(void) 
            {
               while(Serial.available() > 0)
              {
               if(index < 3) // One less than the size of the array
                 {
                     inChar = Serial.read(); // Read a character
                     inData[index] = inChar; // Store it
                     index++; // Increment where to write next
                     inData[index] = '\0'; // Null terminate the string
                 }
              }
                if(strcmp(inData,"ON")== 0)
                {
                  Serial.println("compared");
                   inData[0] = '\0';
                   index = 0;
                  
                }
                  else if(strcmp(inData,"YES") == 0)
                {
                  Serial.println("Compared yes ");
                   inData[0] = '\0';
                    index = 0;
                  
                }
                 
                
             }

…in the code…when i send or inputs and wrong chatacter
like…
i send O by mistake then …send ON ( the correct message)
now the program dont respond… smiley-fat smiley-sad …

You need to be sending packets, not just random characters. If you send “ON” or “ON” or “ON” (any of which the serial monitor can do automatically), then you would read and store data until the end of packet marker ( or ) arrived.

Only when the end of packet marker arrived would you attempt to use the value.

That would let you deal with a packet containing ON or OFF or OOPS.

As the code is structured now, the buffer fills up until it contains a valid string. Which may be never.