Go Down

Topic: The data from GSM is processed by Arduino in a strange manner and incorrectly (Read 377 times) previous topic - next topic

alex12524

I am using Arduino uno and GSM SIM900A module for a simple electric switch board automation project. i am using IF statements for comparing the data received by GSM module. The GSM module is successfully configured for receiving sms.

When i send sms to my GSM, i am saving it in a int variable and then switching it to find a match through IF statements. The data sent by me is correctly displayed over the serial monitor but every time an incorrect IF statement is executed.

For eg:
If I am sending "1" as sms then the expected result would be Pin 12 high and "TV ON" displayed, but this doesn't happen instead of that I see Pin 8 as high and "LIGHT ON" displayed.

Similarly whatever data i sent it will always display result any thing other than the correct/expected output.

Following is the code I m working on, please suggest what might be the problem and how i can fix it.

Code: [Select]


#include <SoftwareSerial.h>
//use TX to 9 and RX to 11
SoftwareSerial mySerial(9,11);
  int ledTV = 12;
  int ledFAN = 7;
  int ledLIGHT = 8;
 

 

void setup()
{
  mySerial.begin(9600);   // Setting the baud rate of GSM Module 
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(1000);
  pinMode(ledTV,OUTPUT);
  pinMode(ledFAN,OUTPUT);
  pinMode(ledLIGHT,OUTPUT);
 
 
  ReceiveMessage();
}


void loop()
{
 if (mySerial.available()>0)
   Serial.write(mySerial.read());
   storedata();
   
}

 void ReceiveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(2000);
  mySerial.println("GSM SUCCESSFULLY CONFIGURED FOR RECEIVING");
 }
 
void storedata()
{
  while(mySerial.available())
       {
         
         int digit  = mySerial.read();
               
        if(digit=='1')
       { 
         digitalWrite(ledTV,1);    // making Pin 12 high if data received is '1'
         mySerial.println("TV ON"); // printing the same on the Serial monitor
       
      }

         if(digit=='2')
      {
        digitalWrite(ledFAN,1);
        mySerial.println("FAN ON");
         
        }

        if(digit=='3')
        {
          digitalWrite(ledLIGHT,1);
          mySerial.println("LIGHT ON");
          }

          if(digit=='4')
          {
            digitalWrite(ledTV,1);
            digitalWrite(ledFAN,1);
            digitalWrite(ledLIGHT,1);
            mySerial.println("ALL ON");
            }

            if(digit=='5')
            {
            digitalWrite(ledTV,0);
            digitalWrite(ledFAN,0);
            digitalWrite(ledLIGHT,0);
            mySerial.println("ALL OFF");
            }
       }
 }


 


rw950431

Why start a new thread when its the same program as http://forum.arduino.cc/index.php?topic=460481.0 ?


You realise that your loop() function reads and prints one character from the GSM, then discards it, before sending the rest to storedata()?

alex12524

i don't know much of that.

can you tell me  how can i save an integer,and clear serial buffer each time when it executes program

alex12524

Please help me , I want to submit this project

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy