ESP8266-01 and Neo 6M GPS module not working through Arduino Mega 2560.

Hello Everyone,

My project involves sending GPS data to Thingspeak from a Neo 6M module through an ESP8266-01. I am using an Arduino Mega 2560 as my microcontroller. When I use my GPS module individually, everything works just fine, and I get data as expected. But when I am trying to send said data to Thingspeak using an ESP8266-01, I start getting “0.00000” for both latitude and longitude instead of the actual values. I’m using both the TinyGPS++ library and the SoftwareSerial Library. I don’t know if that information is relevant or not, but I’m just putting it out there in case the libraries are interfering with each other.

This is my code:

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#define RX 50
#define TX 51
int RXPin = 10;
int TXPin = 11;
int GPSBaud = 9600;
String AP = "************";       // AP NAME
String PASS = "**********"; // AP PASSWORD
String API = "*************";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
String Latitude;
String Longitude;
TinyGPSPlus gps; 
boolean found = false; 
int valSensor = 1;
SoftwareSerial gpsSerial(RXPin, TXPin);
SoftwareSerial esp8266(RX,TX); 
  
void setup() {
  Serial.begin(9600);
  
 esp8266.begin(9600);
   sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
  esp8266.end();
}

void loop() {


 gpsSerial.begin(9600);


   Latitude = String(gps.location.lat(), 5);
   Longitude = String(gps.location.lng(),5);

    Serial.print("Latitude: ");
    Serial.println(Latitude);
    Serial.print("Longitude: ");
    Serial.println(Longitude);
gpsSerial.end();
 esp8266.begin(9600);
 String getData = "GET /update?api_key="+ API +"&field1="+Latitude+"&field2="+Longitude;
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
 esp8266.end();
}



void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

Thank you, everyone, in advance.

If you check the SoftwareSerial reference, do a Google search on 'Arduino SoftwareSerial Reference' to find it, you will see that only one software serial instance can be listening at a time, and you are using two.

The Mega has 3 hardware UART ports anyway, far better to use them.

I will try using the Hardware UART ports. But I'm only using one port at a time in my code, by using begin and end commands.

Thank you for your time!