Problem Statement
Hello! I'm facing difficulties integrating a NEO 6M GPS module with an ESP01 module on an Arduino Mega for transmitting GPS data to a server. Although both the GPS module and ESP01 work individually, I encounter issues when trying to combine them for server communication. The Arduino successfully connects to Wi-Fi, but the program seems to halt thereafter, preventing GPS data retrieval and transmission to the server.
// Your code snippet here
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
const char* ssid = "...";
const char* password = ".........";
const char* serverAddress = "192.168.254.190";
const int serverPort = 80;
// Hardware serial for GPS module
#define GPS_SERIAL Serial1
// Software serial pins for ESP01
#define ESP_RX_PIN 10
#define ESP_TX_PIN 11
SoftwareSerial espSerial(ESP_RX_PIN, ESP_TX_PIN);
WiFiEspClient client;
TinyGPSPlus gps;
void setup() {
Serial.begin(9600);
GPS_SERIAL.begin(9600);
espSerial.begin(9600);
WiFi.init(&espSerial);
connectToWiFi();
}
void loop() {
// Update GPS data
while (GPS_SERIAL.available() > 0) {
gps.encode(GPS_SERIAL.read());
// Check if GPS data is valid
if (gps.location.isValid()) {
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
// Construct data string with latitude and longitude
String data = String(gps.location.lat(), 6) + "," + String(gps.location.lng(), 6);
// Send GPS data to the server
sendDataToServer(data);
}
}
delay(10000); // Wait for 10 seconds before sending next data
}
void connectToWiFi() {
Serial.println("Connecting to WiFi");
// Attempt to connect to Wi-Fi network
int status = WiFi.begin(ssid, password);
int attempts = 0;
while (status != WL_CONNECTED && attempts < 5) {
delay(1000);
Serial.println("Attempting to connect to Wi-Fi...");
status = WiFi.begin(ssid, password);
attempts++;
}
if (status == WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("Failed to connect to WiFi");
}
}
void sendDataToServer(String data) {
// Check if client is connected to the server
if (client.connect(serverAddress, serverPort)) {
Serial.println("Connected to server.");
// Construct the HTTP POST request
String postRequest = "POST /Arduino_server/receive_data.php HTTP/1.1\r\n";
postRequest += "Host: " + String(serverAddress) + "\r\n";
postRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
postRequest += "Content-Length: " + String(data.length() + 5) + "\r\n"; // Include "data=" prefix length
postRequest += "Connection: close\r\n\r\n";
postRequest += "data=" + data;
// Send the POST request
client.println(postRequest);
Serial.println("Request sent");
Serial.println(data);
// Wait for the server response
delay(2000);
// Read and print the server response
while (client.available()) {
char c = client.read();
Serial.print(c);
}
delay(2000);
// Close the connection
client.stop();
} else {
Serial.println("Connection to server failed");
}
}
Hardware Setup
I have connected the NEO 6M GPS module to the hardware serial pins (18 and 19) and the ESP01 to software serial pins (10 and 11) on the Arduino Mega.
Expected Outcome
I expect the Arduino to continuously retrieve GPS data and transmit it to the server via the ESP01 module.
Actual Outcome
The Arduino successfully connects to Wi-Fi, but it does not progress beyond this stage, failing to retrieve GPS data and transmit it to the server.
I would greatly appreciate any assistance or insights into resolving this issue. Whether it involves debugging the code, optimizing the hardware setup, or any other suggestions, your help would be invaluable.
why are you using SoftwareSerial on a Mega which has three spare hardware serial ports, e.g. you are using Serial1 but there are also Serial2 and Serial3
i did try another approach i use the serial1 and serial2
#include "WiFiEsp.h"
#include <TinyGPS++.h>
//Wi-Fi network credentials
const char* ssid = "...";
const char* password = ".....";
// Define your server details
const char* serverAddress = "192.168.254.190";
const int serverPort = 80;
WiFiEspClient client; //client object to communicate with the server
TinyGPSPlus gps; //TinyGPS++ object to handle GPS data
void setup()
{
Serial.begin(9600); // Initialize serial communication for debugging
Serial1.begin(9600); // Initialize hardware serial communication with ESP module
Serial2.begin(9600); // Initialize hardware serial communication with GPS module
// Initialize ESP module
WiFi.init(&Serial1);
// Connect to Wi-Fi
connectToWiFi();
}
void loop()
{
// Update GPS data
while (Serial2.available() > 0)
{
gps.encode(Serial2.read());
// Check if GPS data is valid
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
// Construct data string with latitude and longitude
String data = String(gps.location.lat(), 6) + "," + String(gps.location.lng(), 6);
// Send GPS data to the server
sendDataToServer(data);
}
}
delay(10000); // Wait for 10 seconds before sending next data
}
void connectToWiFi()
{
Serial.println("Connecting to WiFi");
// Attempt to connect to Wi-Fi network
int status = WiFi.begin(ssid, password);
int attempts = 0;
while (status != WL_CONNECTED && attempts < 5) {
delay(1000);
Serial.println("Attempting to connect to Wi-Fi...");
status = WiFi.begin(ssid, password);
attempts++;
}
if (status == WL_CONNECTED)
{
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("Failed to connect to WiFi");
}
}
void sendDataToServer(String data)
{
// Check if client is connected to the server
if (client.connect(serverAddress, serverPort))
{
Serial.println("Connected to server.");
// Construct the HTTP POST request
String postRequest = "POST /Arduino_server/receive_data.php HTTP/1.1\r\n";
postRequest += "Host: " + String(serverAddress) + "\r\n";
postRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
postRequest += "Content-Length: " + String(data.length() + 5) + "\r\n"; // Include "data=" prefix length
postRequest += "Connection: close\r\n\r\n";
postRequest += "data=" + data;
// Send the POST request
client.println(postRequest);
Serial.println("Request sent");
Serial.println(data);
// Wait for the server response
delay(2000);
// Read and print the server response
while (client.available())
{
char c = client.read();
Serial.print(c);
}
delay(2000);
// Close the connection
client.stop();
}
else
{
Serial.println("Connection to server failed");
}
}
Wiring
I put the ESP01 (Tx to pin 19 and Rx to pin 18)
I put the GPS (Tx to pin 17 and Rx to pin 16)
But only the ESP is working finely but the GPS is not transmitting any coordinates.
Output
This is the output when i check the serial monitor and this output is from the setup only the loop function is not displayed
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.5.4
Connecting to WiFi
[WiFiEsp] Connected to ...
WiFi connected
IP address:
192.168.254.165
Supposedly it will produce serial output of my gps becaue I put my gps serial in loop.
Thanks for reply, I really appreciate it I hope you can help me more
Instead of trying to do GPS Reading and Wi-Fi Communication in the loop() function, you can separate them into different functions and call them sequentially. You can edit your code like this:
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
const char* ssid = "...";
const char* password = ".........";
const char* serverAddress = "192.168.254.190";
const int serverPort = 80;
#define GPS_SERIAL Serial1
#define ESP_RX_PIN 10
#define ESP_TX_PIN 11
SoftwareSerial espSerial(ESP_RX_PIN, ESP_TX_PIN);
WiFiEspClient client;
TinyGPSPlus gps;
void setup() {
Serial.begin(9600);
GPS_SERIAL.begin(9600);
espSerial.begin(9600);
WiFi.init(&espSerial);
connectToWiFi();
}
void loop() {
readGPSData();
sendDataToServer();
delay(10000); // Wait for 10 seconds before sending next data
}
void readGPSData() {
while (GPS_SERIAL.available() > 0) {
gps.encode(GPS_SERIAL.read());
}
}
void sendDataToServer() {
if (gps.location.isValid()) {
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
String data = String(gps.location.lat(), 6) + "," + String(gps.location.lng(), 6);
if (client.connect(serverAddress, serverPort)) {
// Construct and send the HTTP POST request
String postRequest = "POST /Arduino_server/receive_data.php HTTP/1.1\r\n";
postRequest += "Host: " + String(serverAddress) + "\r\n";
postRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
postRequest += "Content-Length: " + String(data.length() + 5) + "\r\n";
postRequest += "Connection: close\r\n\r\n";
postRequest += "data=" + data;
client.println(postRequest);
delay(2000); // Wait for the server response
client.stop();
}
}
}
void connectToWiFi() {
Serial.println("Connecting to WiFi");
int status = WiFi.begin(ssid, password);
int attempts = 0;
while (status != WL_CONNECTED && attempts < 5) {
delay(1000);
Serial.println("Attempting to connect to Wi-Fi...");
status = WiFi.begin(ssid, password);
attempts++;
}
if (status == WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("Failed to connect to WiFi");
}
}
see if you can display the raw NMEA data using Serial2, e.g.
// Arduino Mega serial2 test
// mega pin 16 is Tx
// pin 17 is Rx
// for loopback test connect pin 16 to pin 17
// for RS232 shield connect pin 16 to Tx and pin 17 to Rx
// for loopback test connect 9 pin D connector pins 2 and 3
unsigned long time;
void setup() {
Serial.begin(115200); // initialise serial monitor port
Serial2.begin(115200); // initialise Serial2
Serial.write("Arduino Mega Serial2 test - for loopback test connect pin 18 to pin 19\n");
}
void loop() {
if (Serial2.available()) // read from Serial1 output to Serial
Serial.write(Serial2.read());
if (Serial.available()) { // read from Serial outut to Serial1
int inByte = Serial.read();
Serial.write(inByte); // local echo if required
Serial2.write(inByte);
}
}
Thankyousomuch for helping me, I really appreciate it!!!. This is my first time posting in this forum and I didn't expect that someone will help me cause I am newbie in this site.