Creat Serial Connection in Funktion

Hey,
i want to creat a function where i pull the Time from GPS(tt-go tbeam board) i got the time pulled with tinygps++ works perfekt since i only need to set the time once in the begging i created a function where the serial connection to the gps moduls begins. but it just doesn't work do i need to to set the serial connection in the setup() ? Check code below any help is welcome.

bool timesetted =false;
ESP32Time currenttime; 

void setup() {

Serial.begin(115200);

}


void loop() {
if(!timesetted){
  timesetted = setTimefromGPS(currenttime);
  if(!timesetted){
    Serial.println("Die Uhrzeit konnte nicht gesetzt werden");
  }
  else{
  Serial.println("Die Uhrzeit wurde gesetzt");
}
}
Serial.print(currenttime.getTime());
Serial.println(currenttime.getMillis());
}

bool setTimefromGPS(ESP32Time time){
    bool timesetted = false; 

    //connect to gps and pull time 
    TinyGPSPlus gps; 
    SoftwareSerial gpsserial(32,12);
    
    gpsserial.begin(9600);
    /*delay(1000);
    Serial.println("gps.avaibable =");
    Serial.println(gpsserial.available());
    if(gpsserial.available()==0){
        Serial.println("Baudrate war nicht auf 115200 eingestellt");
        gpsserial.end();
        setserial115200();
        delay(50);
        gpsserial.begin(115200);
    }
    */
    Serial.println("Die Verbindung zum GPS Modul wurde aufgebaut");
    // Dispatch incoming characters
    while (gpsserial.available() > 0){
    gps.encode(gpsserial.read());}
    Serial.print("Verfügbaresatelliten: ");
    Serial.println(gps.satellites.value());


    if (gps.time.isUpdated()){
        if(gpsserial.available()){
        time.setTime(gps.time.second(), gps.time.minute(), gps.time.hour(), gps.date.day(), gps.date.month(), gps.date.year(), gps.time.centisecond());
        timesetted=true; }
        }
    else{
        timesetted=false;
        }
return timesetted;
}

You declare these objects inside of a function. As soon as the function exits, the objects are destroyed.

As a solution, you can make these objects global.

[edit]

Maybe this is not a problem after all, since you do not use these obects anywhere else.

[edit2]

So what do you see in the serial monitor?

This is done right after initialisation. There is probably nothing in the serial buffer at that time, so the function will effectively do nothing.

Instead, you can wait until the expected amount of bytes is available:

while (gpsserial.available() <= 10);  // Or whatever number of bytes you expect.
gps.encode(gpsserial.read());

hey,
i'm planing on running it on battery thats why i only want to have to connection and gps modul turned on when i need it.
thanks a lot for the help it was the while state which is casing the problem. i put in a delay and a serial.print without decoding it and i get the full nema sentences but when i put the delay in front of the orginal while loop the gps.encode(gpsserial.read(); still won't work.

with this code i got all the serial information i'm expecting from the gps module but i can't process it.

Serial1.begin(9600, SERIAL_8N1, 34, 12);

delay(5000);
while(Serial1.available()) {
    Serial.write(Serial1.read());
    Serial1.println();
}
return false;

i got it to work with this code: but i don't really know why the declared serial connection does not work.

bool setTimefromGPS(ESP32Time time){
    bool timesetted = false; 

    //connect to gps and pull time 
    TinyGPSPlus gps; 
    Serial1.begin(9600, SERIAL_8N1, 34, 12);
 
    Serial.println("Die Verbindung zum GPS Modul wurde aufgebaut");
    delay(5000);
    while(Serial1.available()) {
    Serial.write(Serial1.read());
    Serial1.println();
    }
    // Dispatch incoming characters
    delay(5000);
    while (Serial1.available() > 0){
    gps.encode(Serial1.read());}
    Serial.print("Verfügbaresatelliten: ");
    Serial.println(gps.satellites.value());


    if (gps.time.isUpdated()){
        time.setTime(gps.time.second(), gps.time.minute(), gps.time.hour(), gps.date.day(), gps.date.month(), gps.date.year(), gps.time.centisecond());
        timesetted=true; 
        }
    else{
        timesetted=false;
        }
return timesetted;

That is also an option, maybe not as preferable as explicitly waiting for the expected data, but if it works, it works.

Can you please mark the most helpful post as the solution? This prevents helpers spending time on a solved issue and it will lead people with the same question to the correct answer directly.

Yeah Thanks for your help jfjlaros!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.