Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

GSM module and arduino interfacing

Status
Not open for further replies.

uttampal86

Newbie level 4
Newbie level 4
Joined
Jun 10, 2008
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,341
Hello,
I hope you guys are having a good time.

My final year project is related to sending a sms to a gsm module which is connected with an arduino to switch on/off any appliance like fan, light, etc.

Problem statement: We have done all the interfacing, the problem arises when the sms reaches to the gsm module, but we are not able to use this string to perform the required action on the arduino module.

I'm attaching two programs here: gsme2 which is a simple program to switch on/off 9 LEDS. Second program prelim4 is the main program.
Can you give some guidance about the code and what may be going wrong?
 

Attachments

  • programs.zip
    3.1 KB · Views: 193

i look at your code and it suffer from the usual problem: problematic string decoding...

codes like this:


Code:
else if(s[2]=='M' && s[3]=='T' && s[4]=='I')    ////+cnmi rsponse is +CMTI: "SM",4

is not how you decode for received strings..

You should store them in a String variable like this

Code:
String rcv_str = "";     //declare empty string 

...
//if a char is received and not equal to carraige return, store to string variable
rcv_str += rx_byte;

...
//else if rx_byte is equal to carriage return, 
if(rcv_str == "+CMTI:")
{
  //do something, like sending AT+CMGR to read the message in the SIM
}
 
Thanks for the reply,

There is some modification to the code and some more information about the work:

We are using Arduino Duemilanove and SIM 900 GSM module (**broken link removed**)

We've tried to work on the similar problem of lightning Leds from port 9-12 when we send an sms #aibicidi, where i = 0 or 1, 0 =off, 1=on.
Eg. #a1b1c1d1 will switch on all the Led's.

When we upload the code and run it through serial monitor and enter the #a1b1c1d1 in the serial monitor, we can see all the led's lighten up. But if we send the sms with having content "#a1b1c1d1", we dont see any function of leds.

It would be great if anyone can give some guidance about the same.

Code:
 char inchar; //Will hold the incoming character from the Serial Port.


 int led1 = 9;
 int led2 = 10;
 int led3 = 11;
 int led4 = 12;

 void setup()
 {
 // prepare the digital output pins
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 //Initialize GSM module serial port for communication.
 
 
 Serial.begin(9600);
 delay(3000); // give time for GSM module to register on network etc.
 Serial.println("AT+CMGF=1"); // set SMS mode to text
 delay(200);
 Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
 delay(200);
 }

 void loop() 
 {
 //If #a1b1c1d1 comes as sms, all led's should light up.
 if(Serial.available() >0)
 {
 inchar=Serial.read(); 
 if (inchar=='#')
   {
   delay(10);
   inchar=Serial.read(); 
   
 //first led
   if (inchar=='a')
     {
   delay(10);
   inchar=Serial.read();
   
 if (inchar=='0')
   {
   digitalWrite(led1, LOW);
   } 
 else if (inchar=='1')
   {
   digitalWrite(led1, HIGH);
   }
 delay(10);

 
 //Second led
 inchar=Serial.read(); 
 
 if (inchar=='b')
   {
   inchar=Serial.read();
 if (inchar=='0')
 {
 digitalWrite(led2, LOW);
 } 
 
 else if (inchar=='1')
 {
 digitalWrite(led2, HIGH);
 }
 delay(10);
 
 // Third led
 inchar=Serial.read(); 
 if (inchar=='c')
 {
 inchar=Serial.read();
 if (inchar=='0')
 {
 digitalWrite(led3, LOW);
 } 
 else if (inchar=='1')
 {
 digitalWrite(led3, HIGH);
 }
 delay(10);
 
 //Fourth led
 
 inchar=Serial.read(); 
 if (inchar=='d')
 {
 delay(10);
 inchar=Serial.read();
 if (inchar=='0')
 {
 digitalWrite(led4, LOW);
 } 
 else if (inchar=='1')
 {
 digitalWrite(led4, HIGH);
 }
 delay(10);
 }
 }
 Serial.println("AT+CMGD=1,4"); // delete all SMS
 }
 }
 }
 }
 }
 

We were able to lighten up the LED with an sms.

Keeping the code below. This code will lighten 9 leds which are kept from port 4 to 12. The sms code is "0#iii#iii#iii, where i =0 or 1 meaning off or on respectively. Ex. 0#111#111#111 will switch on all the leds.

The modification was AT+CNMI=2,2,0,0,0 instead of AT+CNMI=3,3,0,0 from our previous code.

Code:
 char inchar; //Will hold the incoming character from the Serial Port.


 int led1 = 4;
 int led2 = 5;
 int led3 = 6;
 int led4 = 7; 
 int led5 = 8; 
 int led6 = 9; 
 int led7 = 10; 
 int led8 = 11; 
 int led9 = 12; 

 void setup()
 {
 // prepare the digital output pins
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(led5, OUTPUT);
 pinMode(led6, OUTPUT);
 pinMode(led7, OUTPUT);
 pinMode(led8, OUTPUT);
 pinMode(led9, OUTPUT);
 
 
 // initially all are off
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
 digitalWrite(led6, LOW);
 digitalWrite(led7, LOW);
 digitalWrite(led8, LOW);
 digitalWrite(led9, LOW);
 
 
 
 
 //Initialize GSM module serial port for communication.
 Serial.begin(9600);
 delay(3000); // give time for GSM module to register on network etc.
 Serial.println("AT+CMGF=1\r"); // set SMS mode to text
 delay(200);
 Serial.println("AT+CSMS=1\r"); 
 delay(200);
 
// Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
 Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt 

 delay(200);
 }

 void loop() 
 {
 //If a character comes in from the Serialular module...

 if(Serial.available() >0)
 {  
//   digitalWrite(13, HIGH);
   // the following code is to check if input command is cmg then glow LED 13
 
    // inchar=Serial.read(); 
    // if (inchar=='C')
    // {
    // delay(10);
    // inchar=Serial.read(); 
    // if (inchar=='M')
    // {
    // delay(10);
    // inchar=Serial.read();
    // if (inchar=='G')
    // {
    // digitalWrite(led1, HIGH);
    // } 
    // else if (inchar=='1')
    // {
    // digitalWrite(led1, HIGH);
    // }
    
    
//our program begins from here

 delay(10);
 inchar=Serial.read(); 
 if (inchar=='0')   // to catch the 1st 0 in our string 0#101#101#101
 {     
       // digitalWrite(13, HIGH);
       // for #101________________
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led1, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led1, HIGH);
             }
             
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 0
             if (inchar=='0')
             {
                 digitalWrite(led2, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led2, HIGH);
             }
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led3, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led3, HIGH);
             }      
       } //end of  1st 'if char==#' loop
       
       
       
       
       //for _____  #101  _______
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led4, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led4, HIGH);
             }
             
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 0
             if (inchar=='0')
             {
                 digitalWrite(led5, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led5, HIGH);
             }
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led6, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led6, HIGH);
             }      
       } //end of 2nd 'if char==#' loop
       
       
       
       
       //for _____ _____ #101
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led7, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led7, HIGH);
             }
             
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 0
             if (inchar=='0')
             {
                 digitalWrite(led8, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led8, HIGH);
             }
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led9, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led9, HIGH);
             }      
       } //end of 3rd 'if char==#' loop
       
       
       
 } // "ckeck 0 in front of string" wala loop close

 //Serial.println("AT+CMGD=1,4"); // delete all SMS
 
 } // serial available condition close
 
 
//   Serial.println("AT+CMGR=1\r");
//   delay(4000);
 } // LOOP function close
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top