Arduino Uno and SIM900a Shield - Need Code

Hi, I need a code which receive a key word and compare that word with the word setted and if is the same send me a message with something and if not send me the message Error.

Can someone help me ? I tried to make it but doesnt work :(

Thanks very much.

(deleted)

Here :

#include <SoftwareSerial.h>
#include <string.h>


SoftwareSerial GSM(2, 3);

byte buffer[128]; 
int count = 0; 


void setup()

{

  GSM.begin(19200);

  Serial.begin(19200);

  delay(500);

  Sim900_Inti();

}

void loop()
{
  GSM.listen();

  if (GSM.available()) 
  {
    while (GSM.available()) 
    {
      buffer[count++] = GSM.read(); 
      if (count == 64)break;
    }
    Serial.write(buffer, count);
    Cmd_Read_Act();
    clearBufferArray();
    count = 0; 

  }

  if (Serial.available()) 
    GSM.println(Serial.read());
}

void clearBufferArray() 
{
  for (int i = 0; i < count; i++)
  {
    buffer[i] = NULL;
  }
}

void Sim900_Inti(void)

{
  GSM.println("AT+CMGF=1"); 
  Serial.println("AT+CMGF=1");
  delay(500);
  GSM.println("AT+CNMI=2,2");
  Serial.println("AT CMGF=1");
  delay(500);
}

void Cmd_Read_Act(void)
{
  char buffer2[128];
  for (int i = 0; i < count; i++)
  {
    buffer2[i] = char(buffer[i]);
  }
  if (strstr(buffer2, "Test"))
  {
    Serial.println("Password Authenticated.");
    Serial.println("Sending reply SMS. ");
    SendTextMessage();
  }
}

void SendTextMessage()
{
  GSM.print("AT+CMGF=1\r");
  delay(100);
  GSM.println("AT + CMGS = \"+40752xxxx3\"");
  delay(100);
  GSM.println("TEST");
  delay(100);
  GSM.println((char)26);
  delay(100);
  GSM.println();
  delay(5000);
  int counter = 0;

}

The code you posted does something. You did not say what it does.
You did not say how that differs from what you want.

It’s hard to help you without knowing what the problem is.

  GSM.listen();

The last created instance of SoftwareSerial is already listening. Since you only have one instance, it is not necessary to call listen() on every pass through loop().

  if (GSM.available())
  {
    while (GSM.available())

The if statement is not necessary. The while statement will do nothing if there is no serial data to read.

    {
      buffer[count++] = GSM.read();
      if (count == 64)break;
    }

Why break at 64 when the array can hold twice that number of characters? Why is the array not being NULL terminated, to make it a string?

  if (Serial.available())
    GSM.println(Serial.read());

If you send a string to the Arduino, via the serial port, why are you then sending it to the GSM with a carriage return and line feed after each character?

void Cmd_Read_Act(void)
{
  char buffer2[128];
  for (int i = 0; i < count; i++)
  {
    buffer2[i] = char(buffer[i]);
  }

Why are you copying the data, when you are going to simply through the data away when this function ends?

I try do make the code for what i need , but its not working... The code what i posted is made from some tutorial , i watch them and i try to make what i need, but is not working.

redaot: I try do make the code for what i need , but its not working... The code what i posted is made from some tutorial , i watch them and i try to make what i need, but is not working.

Neither are you. I asked some questions. You whined that the code doesn't work, for some undefined definition of work. This forum does not work that way. You need to answer ALL of the questions asked.

I make another code :

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
 
void setup()
{
  SIM900.begin(19200);
  SIM900power();  
  delay(20000);  
}
 
void SIM900power()
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
}
 
void sendSMS()
{
  SIM900.print("AT+CMGF=1\r");                                                       
  delay(100);
  SIM900.println("AT + CMGS = \"+40752******\"");                                    
  delay(100);
  SIM900.println("TEST");        
  delay(100);
  SIM900.println((char)26);                       
  delay(100); 
  SIM900.println();
  delay(5000);                                     
  SIM900power();                                  
}
 
void loop()
{
  sendSMS();
  do {} while (1);
}

And i try to implement the function wich i speack above; i want to receive a sms just when i send a key word . The code doesnt work… Some help ?

#include <SoftwareSerial.h>
#include <String.h>

byte buffer[64]; 
int count=0; 

SoftwareSerial SIM900(7, 8);

String yourPassword = "TRACK"; 
String password; 
char incoming_char=0;


char senderNumber[20]; 

void setup()
{
  SIM900.begin(19200);
  SIM900power();  
  delay(20000);  
}
  
 
void SIM900power()
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
}
 
void loop()
{
 SIM900.listen();
  if (SIM900.available())                     // If date is comming from from GSM shield)
  {
    while(SIM900.available())               // reading data into char array 
    {
      buffer[count ]=SIM900.read();    // writing data into array
      if(count == 64)break;
     }
    Serial.write(buffer,count);    // if no data transmission ends, write buffer to hardware serial port
  Cmd_Read_Act();       //Read the 'COMMAND' sent to SIM900 through SMS
  clearBufferArray();      // call clearBufferArray function to clear the storaged data from the array                        
  count = 0;    
  
 
  }
}

void clearBufferArray()         // function to clear buffer array                     
{
  for (int i=0; i<count;i )
    { buffer[i]=NULL;}              // clear all index of array with command NULL                  
}

void Cmd_Read_Act(void)     //Function reads the SMS sent to SIM900 shield.                  
{ 
  char buffer2[64];
  for (int i=0; i<count;i )
  { buffer2[i]=char(buffer[i]);}  
    
  if (strstr(buffer2,"TRACK"))    //Comparing password entered with password stored in program  
  {
      Serial.println("Password Authenticated.");
      Serial.println("Sending reply SMS. ");
      sendSMS();            
  }
  
}

void sendSMS()
{
  SIM900.print("AT+CMGF=1\r");                                                        
  delay(100);
  SIM900.println("AT + CMGS = \"+407524*****\"");                                     
  delay(100);
  SIM900.println("Test");       
  delay(100);
  SIM900.println((char)26);                       
  delay(100); 
  SIM900.println();
  delay(5000);                                     
  SIM900power();                                   
}

The code doesnt work...

Nonsense. The code works just fine. You simply expect it to do something other that what it actually does. However, since you couldn't be bothered to explain what is actually does or what you expect it to do, we can NOT help you.

PaulS: Nonsense. The code works just fine. You simply expect it to do something other that what it actually does. However, since you couldn't be bothered to explain what is actually does or what you expect it to do, we can NOT help you.

The code doesnt work after is uploaded .

I want to set a "Password" , when i send the password from another phone to the sim card what is in GSM module, if its the same password i want the module to send me back a message .

Well, now we have half the story. Try again.

So, here is the code for wich i want to make that “login” with a word or when i make a call from a number what is set to send me the message with location.

Now you have entire story :smiley:

#include <SoftwareSerial.h>
#include <TinyGPS.h>

TinyGPS gps;

SoftwareSerial SIM900(7, 8);
SoftwareSerial ss(10, 11);

float LON, LAT;

void setup()
{
  Serial.begin(9600);
  SIM900.begin(19200);
  ss.begin(9600);
  Serial.println("GPS Start");
  delay(20000);

}

void loop()
{
  while (ss.available())
  {
    char c = ss.read();
    if (gps.encode(c)) // Did a new valid sentence come in?
    {
      float flat, flon;
      unsigned long age;
      gps.f_get_position(&flat, &flon, &age);

      LAT = flat;
      LON = flon;

      Serial.print("LAT,LON =");
      Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
      Serial.print(",");
      Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
      Serial.print(" SAT=");
      Serial.println(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());

      delay(1000);

      Serial.println("Send SMS");
      SIM900.print("AT+CMGF=1\r");
      delay(100);
      SIM900.println("AT + CMGS = \"+4000000543\"");
      delay(100);
      SIM900.println("Locatie");
      SIM900.println("");
      SIM900.println("Google Maps URL:");
      SIM900.print("http://www.google.com/maps/place/");
      SIM900.print(LAT == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LAT, 6);
      SIM900.print(",");
      SIM900.print(LON == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LON, 6);
      SIM900.println("");
      SIM900.println("LAT,LON =");
      SIM900.print(LAT == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LAT, 6);
      SIM900.print(",");
      SIM900.print(LON == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LON, 6);
      Serial.print("LAT,LON =");
      Serial.print(LAT == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LAT, 6);
      Serial.print(",");
      Serial.print(LON == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LON, 6);
      delay(100);
      SIM900.println((char)26);
      delay(100);
      SIM900.println();
      delay(5000);
      Serial.println("\nMesaj trimis");
      delay(100000);
    }
  }
}
SoftwareSerial SIM900(7, 8);
SoftwareSerial ss(10, 11);

What is the ss that is connected to these pins?

Why does the SIM900 instance have a good name while the ss instance has a stupid name?

Now you have entire story

Not by a long shot. The code you posted does NOT look for an SMS with the word login in it. The code does SOMETHING. You STILL, despite repeated requests, have NOT told us what the code ACTUALLY does.

Do not bother posting again if you are not going to tell us what the code actually does.

By the way, I think you are flogging a dead horse. You can NOT listen to the SIM900 and a GPS at the same time.

The ss instance is for GPS communication ( TX and RX ).

The code above send me a message with location, and i set a delay to not receive many messages, because their is a loop.

The code for sms login is this :

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <String.h>

TinyGPS gps;

SoftwareSerial SIM900(7, 8);
SoftwareSerial GpsSerial(10, 11);

byte buffer[64];
int count = 0;

float LON, LAT;

String yourPassword = "TRACK";
String password;
char incoming_char = 0;

void setup()
{
  Serial.begin(9600);
  SIM900.begin(19200);
  GpsSerial.begin(9600);
  Serial.println("GPS Start");
  delay(20000);

}

void loop()
{
  SIM900.listen();
  if (SIM900.available())
  {
    while (SIM900.available())              // reading data
    {
      buffer[count ] = SIM900.read();  // writing data
      if (count == 64)break;
    }
    Serial.write(buffer, count); // if no data transmission ends, write buffer



    while (GpsSerial.available())
    {
      char c = GpsSerial.read();
      if (gps.encode(c)) // Did a new valid sentence come in?
      {
        float flat, flon;
        unsigned long age;
        gps.f_get_position(&flat, &flon, &age);

        LAT = flat;
        LON = flon;

        Serial.print("LAT,LON =");
        Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
        Serial.print(",");
        Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
        Serial.print(" SAT=");
        Serial.println(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());

        delay(1000);

        char buffer2[64];
        for (int i = 0; i < count; i )
        {
          buffer2[i] = char(buffer[i]);
        }

        if (strstr(buffer2, "TRACK"))   //Comparing password entered with password stored in program
        {
          Serial.println("Password Authenticated.");
          Serial.println("Sending reply SMS. ");
          Serial.println("Send SMS");
          SIM900.print("AT+CMGF=1\r");
          delay(100);
          SIM900.println("AT + CMGS = \"+400000543\"");
          delay(100);
          SIM900.println("Locatie");
          SIM900.println("");
          SIM900.println("Google Maps URL:");
          SIM900.print("http://www.google.com/maps/place/");
          SIM900.print(LAT == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LAT, 6);
          SIM900.print(",");
          SIM900.print(LON == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LON, 6);
          SIM900.println("");
          SIM900.println("LAT,LON =");
          SIM900.print(LAT == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LAT, 6);
          SIM900.print(",");
          SIM900.print(LON == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LON, 6);
          Serial.print("LAT,LON =");
          Serial.print(LAT == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LAT, 6);
          Serial.print(",");
          Serial.print(LON == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : LON, 6);
          delay(100);
          SIM900.println((char)26);
          delay(100);
          SIM900.println();
          delay(5000);
          Serial.println("\nMesaj trimis");
          delay(100000);
        }

        for (int i = 0; i < count; i )
        {
          buffer[i] = NULL;  // clear
        }
      }
    }
  }
}

The ss instance is for GPS communication

So, why is it NOT called GPS?

  SIM900.listen();
  if (SIM900.available())
  {
    while (SIM900.available())              // reading data
    {
      buffer[count ] = SIM900.read();  // writing data
      if (count == 64)break;
    }
    Serial.write(buffer, count); // if no data transmission ends, write buffer



    while (GpsSerial.available())
    {

While the SIM900 instance is listening, the GpsSerial instance is not. There will never be data in the GpsSerial buffer while the SIM900 instance is listening.

Quit beating a dead horse. Get a Mega with multiple hardware serial ports.

PaulS: Quit beating a dead horse. Get a Mega with multiple hardware serial ports.

Ok... my MEGA is on the way... Thanks very much for your time