Why use Software Serial with an ESP32?
Whiles I am using an AdaFruit Ultimate GPS the principle is the same.
I load up the #include <TinyGPS++.h> library.
I load #include <HardwareSerial.h>
I declare a TinyGPSPlus GPS; and HardwareSerial GPSSerial ( 1 );
In setup I do these things GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial
xTaskCreatePinnedToCore( fGPS_Parse, "fGPS_Parse", TaskStack10K3, NULL, Priority5, NULL, TaskCore0 ); // assigned to core 0
sema_GPS_Gate = xSemaphoreCreateMutex();
xSemaphoreGive( sema_GPS_Gate );
This is the defined task to get the GPS data:////////////////////////////////////////////////////////////////////////////
void fGPS_Parse( void *pvParameters )
{
int iBit = 1;
for (;;)
{
xEventGroupWaitBits (eg, evtGPS_Parse, pdTRUE, pdTRUE, portMAX_DELAY) ;
if ( xSemaphoreTake( sema_GPS_Gate, xTicksToWait0 ) == pdTRUE )
{
//query GPS: has a new complete chunk of data been received?
if ( GPSSerial.available() > 1 )
{
if ( GPS.encode(GPSSerial.read()) )
{
if ( GPS.location.isValid())
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.Lat = GPS.location.lat();// store data into structure
xPosit.Lon = GPS.location.lng();
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.location.isValid())
if (GPS.speed.isValid())
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.MPH = GPS.speed.mph();
xPosit.KPH = GPS.speed.kmph();
xSemaphoreGive( sema_Posit );
}
} // if (GPS.speed.isValid())
if (GPS.time.isValid())
{
if ( xSemaphoreTake( sema_Time, xSemaphoreTicksToWait ) == pdTRUE )
{
xTime.iSeconds = GPS.time.second();
xTime.iMinutes = GPS.time.minute();
xTime.iHours = GPS.time.hour();
xSemaphoreGive( sema_Time );
}
} // if (GPS.time.isValid())
if (GPS.date.isValid())
{
if ( xSemaphoreTake( sema_Date, xSemaphoreTicksToWait ) == pdTRUE )
{
xDate.iMonth = GPS.date. month();
xDate.iDay = GPS.date.day();
xDate.iYear = GPS.date.year();
xSemaphoreGive( sema_Date );
}
} // if (GPS.date.isValid())
if ( GPS.altitude.isValid() )
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.Alti = GPS.altitude.meters();
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.altitude.isValid() )
if ( GPS.course.isUpdated() )
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.Hdg = GPS.course.deg();
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.course.isUpdated() )
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xQueueOverwrite( xQ_Posit, (void *) &xPosit );
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.encode(GPSSerial.read()))
} // if ( GPSSerial.available() > 0 )
xSemaphoreGive( sema_GPS_Gate );
}
} // for (;;)
vTaskDelete( NULL );
} // void fGPS_Parse( void *pvParameters )
////////////////////////////////////////////////////////////////////////////////
GPS Parsing is triggered by a 1mS hardware timer. Works great.