Code problems (GMS & GPS Modules )

Hi, i use a GSM module (SIM900A) and an GPS module (NEO6MV2) , I put the code on Arduino UNO and when I send the sms with the key word i dont receive any answear from the device. Can someoane tell me why ? Thanks

#include <SoftwareSerial.h>

#include <string.h>

#include <TinyGPS.h>

SoftwareSerial Sim900Serial(2, 3);

byte buffer[128]; // buffer array for data recieve over serial port

int count = 0; // counter for buffer array

SoftwareSerial GPS(4, 5);

TinyGPS gps;

unsigned long fix_age;

long lat, lon;

float LAT, LON;

void gpsdump(TinyGPS &gps);

bool feedgps();

void getGPS();

void SIM900power() // software equivalent of pressing the GSM shield "power" button

{

  digitalWrite(9, HIGH);

  delay(1000);

  Sim900_Inti();

}

void setup()

{

  Sim900Serial.begin(19200); // the SIM900 baud rate

  GPS.begin(9600); // GPS module baud rate

  Serial.begin(19200); // the Serial port of Arduino baud rate.
  
  Serial.println("GPS Start");

  delay(500);

  Sim900_Inti();

}

void loop()

{

  Sim900Serial.listen();

  if (Sim900Serial.available()) // If date is comming from from GSM shield)

  {

    while (Sim900Serial.available()) // reading data into char array

    {

      buffer[count++] = Sim900Serial.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; // set counter of while loop to zero

  }

  if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook

    Sim900Serial.println(Serial.read()); // write it to the GPRS shield

}

void clearBufferArray() // function to clear buffer array

{

  for (int i = 0; i < count; i++)

  {
    buffer[i] = 0; // clear all index of array with command NULL
  }

}

void Sim900_Inti(void)

{

  Sim900Serial.println("AT+CMGF=1"); // Set GSM shield to sms mode

  Serial.println("AT+CMGF=1");

  delay(500);

  Sim900Serial.println("AT+CNMI=2,2");

  Serial.println("AT CMGF=1");

  delay(500);

}

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. ");

    SendTextMessage();

  }

}

void SendTextMessage()

{

  Sim900Serial.print("AT+CMGF=1\r"); //Sending the SMS in text mode

  delay(100);

  Sim900Serial.println("AT + CMGS = \"+407*****476\"");//The predefined phone number

  //Sim900Serial.print("AT+CMGS=");

  //Sim900Serial.println("+60174432599");

  delay(100);

  Sim900Serial.println("Please wait while Module calculates position");//the content of the message

  delay(100);

  Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26

  delay(100);

  Sim900Serial.println();

  delay(5000);

  int counter = 0;

  GPS.listen();

  for (;;)

  {

    long lat, lon;

    unsigned long fix_age, time, date, speed, course;

    unsigned long chars;

    unsigned short sentences, failed_checksum;

    long Latitude, Longitude;

    // retrieves /- lat/long in 100000ths of a degree

    gps.get_position(&lat, &lon, &fix_age);

    getGPS();

    Serial.print("Latitude : ");

    Serial.print(LAT / 1000000, 7);

    Serial.print(" :: Longitude : ");

    Serial.println(LON / 1000000, 7);

    /*if (LAT == 0 && LON == 0)

      {

      continue;

      }

      counter ;

      if (counter<30)

      {

      continue;

      }

    */

    Sim900Serial.print("AT+CMGF=1\r"); //Sending the SMS in text mode

    delay(100);

    Sim900Serial.println("AT + CMGS = \"+407*******76\"");//The predefined phone number

    delay(100);

    Sim900Serial.print("Latitude : ");

    Sim900Serial.print(LAT / 1000000, 7);

    Sim900Serial.print(" :: Longitude : ");

    Sim900Serial.println(LON / 1000000, 7); //the content of the message

    delay(100);

    Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26

    delay(5000);

    Sim900Serial.println();

    counter = 0;

    break;

  }

}

void getGPS()

{

  bool newdata = false;

  unsigned long start = millis();

  while (millis() - start < 1000)

  {

    if (feedgps ())

    {

      newdata = true;

    }

  }

  if (newdata)

  {

    gpsdump(gps);

  }

}

bool feedgps()

{

  while (GPS.available())

  {

    if (gps.encode(GPS.read()))

      return true;

  } return 0;

}

void gpsdump(TinyGPS &gps)

{

  gps.get_position(&lat, &lon);

  LAT = lat;

  LON = lon;

  {

    feedgps();

  }

}
    Serial.println("Password Authenticated.");

    Serial.println("Sending reply SMS. ");

Do you see these messages in the Serial monitor ?

No, Just these lines :
GPS Start
AT+CMGF=1
AT CMGF=1

When i send sms , i receive delivery notification but on monitor not apear anything.

I tested separately both modules and works fine.

  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

Why are you copying buffer to buffer2 ? Why not just use buffer ?
Will the string in buffer2 be properly terminated with a zero ?

if (strstr(buffer, "TRACK"))

exit status 1
invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]


I used this example too

#include <SoftwareSerial.h>
#include <string.h>
SoftwareSerial Sim900Serial(2, 3);
byte buffer[64]; // buffer array for data recieve over serial port
int count=0;     // counter for buffer array
#define phonenumber "+8618612345678"  //Change to your phonenumber, the phonenumber should be the same as the format received by the SMS, may include your country code.
void setup()
{
  Sim900Serial.begin(19200);               // the SIM900 baud rate  
  Serial.begin(19200);             // the Serial port of Arduino baud rate.
  delay(500);
  Sim900_Inti();
}
 
void loop()
{
  if (Sim900Serial.available())              // if date is comming from softwareserial port(data is comming from gprs shield)
  {
    while(Sim900Serial.available())          // reading data into char array
    {
      buffer[count++]=Sim900Serial.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;                       // set counter of while loop to zero
 
 
  }
  if (Serial.available())            // if data is available on hardwareserial port ==> data is comming from PC or notebook
    Sim900Serial.write(Serial.read());       // write it to the GPRS shield
}
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 Sim900_Inti(void)
{
  Sim900Serial.println("AT+CMGF=1");
  delay(500);
  Sim900Serial.println("AT+CNMI=2,2");
   delay(500);
}
void Cmd_Read_Act(void)          //This function read the SMS sent to SIM900 shield, then act based on the command.
{  
  char buffer2[64];
  char comparetext[25];    //take out the first part of the SMS which include the phoneunmber
  for (int i=0; i<count;i++)
  { buffer2[i]=char(buffer[i]);}  
  memcpy(comparetext,buffer2,25); //take out the first part of the SMS which include the phoneunmber
  if (strstr(comparetext,phonenumber))
  {      
  if (strstr(buffer2,"ON"))            //If there are word 'ON' in the SMS, turn on GPIO 1 of SIM900
    {
      Sim900Serial.println("AT+SGPIO=0,1,1,1");// set GPIO 1 PIN to 1
    }
    if (strstr(buffer2,"OFF"))        //If there are word 'OFF' in the SMS, turn off GPIO 1 of SIM900
    {
      Sim900Serial.println("AT+SGPIO=0,1,1,0");// set GPIO 1 PIN to 0
    }
  }
}

And doesnt work.
At this function :

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
}

I receive this error :

C:\Users\redao\Documents\Arduino\test\test.ino: In function 'void clearBufferArray()':
 
C:\Users\redao\Documents\Arduino\test\test.ino:38:16: warning: converting to non-pointer type 'byte {aka unsigned char}' from NULL [-Wconversion-null]
 
     { buffer[i]=NULL;}                  // clear all index of array with command NULL

I read on a forum that i can switch NULL with 0. But doesnt work.

It may be a problem with string library ? i dont found a full .zip library , just the string.h file and this is what I imported to Arduino.


Another test :

#include <SoftwareSerial.h>

#include <string.h>

#include <TinyGPS.h>

SoftwareSerial Sim900Serial(2, 3);

byte buffer[128]; // buffer array for data recieve over serial port

int count = 0; // counter for buffer array

SoftwareSerial GPS(4, 5);

TinyGPS gps;

unsigned long fix_age;

long lat, lon;

float LAT, LON;

void gpsdump(TinyGPS &gps);

bool feedgps();

void getGPS();

void SIM900power() // software equivalent of pressing the GSM shield "power" button

{

  digitalWrite(9, HIGH);

  delay(10);
  Serial.println("GSM Start1");

  Sim900_Inti();
  Serial.println("GSM Start 2");

}

void setup()

{

  Sim900Serial.begin(19200); // the SIM900 baud rate

  GPS.begin(9600); // GPS module baud rate

  Serial.begin(19200); // the Serial port of Arduino baud rate.
  
  Serial.println("GPS Start");

  delay(500);

  Sim900_Inti();

}

void loop()

{
//Serial.println("GSM Start 3");
  Sim900Serial.listen();

  if (Sim900Serial.available()) // If date is comming from from GSM shield)

  {

    while (Sim900Serial.available()) // reading data into char array

    {

      buffer[count++] = Sim900Serial.read(); // writing data into array

      if (count == 64)break;

    }
//Serial.println("GSM Start 3");
    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; // set counter of while loop to zero

  }

  if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook

    Sim900Serial.println(Serial.read()); // write it to the GPRS shield

}

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
  }
Serial.println("GSM NULL");
}

void Sim900_Inti(void)

{
Serial.println("GSM Start 6");
  Sim900Serial.println("AT+CMGF=1"); // Set GSM shield to sms mode

  Serial.println("AT+CMGF=1");

  delay(500);

  Sim900Serial.println("AT+CNMI=2,2");

  Serial.println("AT CMGF=1");

  delay(500);
 Serial.println("GSM Start 7");
}

void Cmd_Read_Act(void) //Function reads the SMS sent to SIM900 shield.

{
 Serial.println("GSM Start 8");
  char buffer2[128];

  for (int i = 0; i < count; i++)

  {
    Serial.println("GSM Start 63");
    buffer2[i] = char(buffer[i]);
    Serial.println("GSM Start 64");
  }
Serial.println("GSM Start 65");
  if (strstr(buffer2, "TRACK")) //Comparing password entered with password stored in program
Serial.println("GSM Start 66");
  {

    Serial.println("Password Authenticated.");

    Serial.println("Sending reply SMS. ");

    SendTextMessage();

  }

}

void SendTextMessage()

{

  Sim900Serial.print("AT+CMGF=1\r"); //Sending the SMS in text mode

  delay(100);

  Sim900Serial.println("AT + CMGS = \"+40752482543\"");//The predefined phone number

  //Sim900Serial.print("AT+CMGS=");

  //Sim900Serial.println("+60174432599");

  delay(100);

  Sim900Serial.println("Please wait while Module calculates position");//the content of the message

  delay(100);

  Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26

  delay(100);

  Sim900Serial.println();

  delay(5000);

  int counter = 0;

  GPS.listen();

  for (;;)

  {

    long lat, lon;

    unsigned long fix_age, time, date, speed, course;

    unsigned long chars;

    unsigned short sentences, failed_checksum;

    long Latitude, Longitude;

    // retrieves /- lat/long in 100000ths of a degree

    gps.get_position(&lat, &lon, &fix_age);

    getGPS();

    Serial.print("Latitude : ");

    Serial.print(LAT / 1000000, 7);

    Serial.print(" :: Longitude : ");

    Serial.println(LON / 1000000, 7);

    /*if (LAT == 0 && LON == 0)

      {

      continue;

      }

      counter ;

      if (counter<30)

      {

      continue;

      }

    */

    Sim900Serial.print("AT+CMGF=1\r"); //Sending the SMS in text mode

    delay(100);

    Sim900Serial.println("AT + CMGS = \"+40752482543\"");//The predefined phone number

    delay(100);

    Sim900Serial.print("Latitude : ");

    Sim900Serial.print(LAT / 1000000, 7);

    Sim900Serial.print(" :: Longitude : ");

    Sim900Serial.println(LON / 1000000, 7); //the content of the message

    delay(100);

    Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26

    delay(5000);

    Sim900Serial.println();

    counter = 0;

    break;

  }

}

void getGPS()

{

  bool newdata = false;

  unsigned long start = millis();

  while (millis() - start < 1000)

  {

    if (feedgps ())

    {

      newdata = true;

    }

  }

  if (newdata)

  {

    gpsdump(gps);

  }

}

bool feedgps()

{

  while (GPS.available())

  {

    if (gps.encode(GPS.read()))

      return true;

  } return 0;

}

void gpsdump(TinyGPS &gps)

{

  gps.get_position(&lat, &lon);

  LAT = lat;

  LON = lon;

  {

    feedgps();

  }

}

What i receive in monitor is his :

GPS Start
GSM Start 6
AT+CMGF=1
AT CMGF=1
GSM Start 7

Its looks like the function Cmd_Read_Act(); its not accessed.

Can someone help me ?

Can someone help me ?

When you get a Mega, yes. Until then, you can forget trying to make the Arduino listen to the GPS and the phone at the same time.

PaulS:
When you get a Mega, yes. Until then, you can forget trying to make the Arduino listen to the GPS and the phone at the same time.

And with Arduino Mega i can run this code and works ?

And with Arduino Mega i can run this code and works ?

With a Mega you can use 2 separate hardware serial ports and ditch SoftwareSerial.

And with Arduino Mega i can run this code and works ?

No. But, you can get rid of the two software serial classes. If you make the appropriate changes to the software AND connect the hardware to the appropriate pins, then there is a better chance that the code will work. There are no guarantees, though. There may be other problems with the code.

But, your problem is like going to the emergency room after nearly cutting your leg off with a chainsaw, and complaining about an in-grown toenail. You have to fix the obvious problems first. And, the most obvious problem with your project is that you are using the wrong hardware.