2

I am trying to create a project which involves getting latitude and longitude via GPS Module and send it via GSM Module. My problem here is that individual code is working perfectly. However, if am to initialize both together, none will work.

#include "GSM.h"//wrapper class for Sim800l library
#include "GPS.h"//wrapper class for TinyGPS library
GSM *gsm;
GPS *gps;
void setup()
{
    Serial.begin(9600); // Debugging only
    gsm = new GSM();//initializes SIM800l 
    //gps = new GPS(11,10 );//initializes NEO6MV2//uncommmenting this line won't make both modules work
}
String telNum;
int i = 1;
void loop()
{
    /*
    Serial.println(gps->getLongitude(),6);
    Serial.println(gps->getLatitude(),6);
    */

    Serial.println(gsm->read(i,telNum));
    Serial.println(telNum);
     ++i;
}

enter image description here

3
  • 1
    sounds like you need to specify software serial pins.
    – dandavis
    Jan 30, 2018 at 8:24
  • 1
    also, that's not a 5v device, and your red wire goes nowhere ;)
    – dandavis
    Jan 30, 2018 at 8:24
  • The original libraries uses software serial. Or maybe I didn't understand you @dandavis
    – Kinyo356
    Jan 30, 2018 at 8:32

3 Answers 3

1

I got it working by initializing and deleting the pointer to the GPS and GSM object.

Initialization and freeing of the objects are made in the loop function. I initialize the GPS, run it in a loop for sometime, destroy it and initialize the GSM and same procedure as GPS.

Though I don't know if it'll have any effects but its working perfectly

#include "GSM.h"
#include "GPS.h"
GSM *gsm;
GPS *gps;
void setup()
{
    Serial.begin(9600); // Debugging only
}

unsigned long previousMillis = 0;//could be any value depending on how quick you want it to elapse
const long interval = 100; //could be any value depending on how long you want it to run
void loop()
{
    gps = new GPS(6, 7);//initialization <SoftwareSerial::begin() function called here>
    while (previousMillis <= interval) 
    {       
        //GPS codes here
        ++previousMillis;
    }
    delete gps;         //destory initialized object
    previousMillis = 0; //reset value

    gsm = new GSM();    //initialization <SoftwareSerial::begin() function called here>
    while (previousMillis <= interval)
    {

        //GSM codes here
        ++previousMillis;
    }
    delete gsm;//destory initialized object

    previousMillis = 00;//reset value
}
0

You can't use two softwareserial devices together (or not reliably, and not without knowing exactly what you are doing).

It's not even easy using one software and one hardware serial together unless you know what you are doing.

SoftwareSerial blocks everything else from happening while it's sending or receiving, so using it for things that send unsolicited messages (as GPSs do all the time) causes havoc.

The best you can hope for is to use HardwareSerial for the GPS and SoftwareSerial for the GSM, which is call-and-response most of the time.

Better would be to use a better board that has multiple hardware serial ports.

1
  • 1
    Ok. Do you have any link that can guide me through the hardware serial? Gooogling it just keeps bringing up libraries and no examples.
    – Kinyo356
    Jan 30, 2018 at 14:16
0

You can use Listen function for example:

First_softwareserial.listen();// when you need gsm serial
Second_softwareserial.listen();// when you need gps serial
1
  • 1
    but both libraries in the Question have the SoftwareSerial built in. so show how to use them the way you describe
    – Juraj
    Sep 15, 2021 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.