I'm trying to send GPS data using a neo-6m GPS module through WiFi using esp8266 module, I have both of the modules working alone, but when I try using both the WiFi works but won't send the data from the GPS.
I call the GPS and save the latitude, longitude, date and time to separate variables, then try sending those variables using the WiFi to the server on the Arduino Uno, but they just show up as 0's.
#include <TinyGPS++.h>
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif
char ssid[] = "UPC9080883"; // your network SSID (name)
char pass[] = "OYKXVYND"; // your network password
int status = WL_IDLE_STATUS;
int ledStatus = LOW;
WiFiEspServer server(80);
// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial Serial2(RXPin, TXPin);
double latitude, longitude;
int days, months, years, hr, mins, secs;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200); // initialize serial for debugging
Serial1.begin(9600); // initialize serial for ESP module
WiFi.init(&Serial1); // initialize ESP module
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
// start the web server on port 80
server.begin();
}
void loop()
{
Serial1.end();
Serial2.begin(GPSBaud);
GPS();
Serial2.end();
Serial1.begin(9600);
WiFiEspClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New client"); // print a message out the serial port
buf.init(); // initialize the circular buffer
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
buf.push(c); // push it to the ring buffer
// printing the stream to the serial monitor will slow down
// the receiving of data from the ESP filling the serial buffer
//Serial.write(c);
// you got two newline characters in a row
// that's the end of the HTTP request, so send a response
if (buf.endsWith("\r\n\r\n"))
{
sendHttpResponse(client);
break;
}
}
}
// close the connection
client.stop();
Serial.println("Client disconnected");
}
}
void GPS() {
while (Serial2.available() > 0) {
gps.encode(Serial2.read());
if (gps.location.isUpdated()) {
// Latitude in degrees (double)
latitude = (gps.location.lat(), 6);
// Longitude in degrees (double)
longitude = (gps.location.lng(), 6);
// Day (1-31) (u8)
days = (gps.date.day());
// Month (1-12) (u8)
months = (gps.date.month());
// Year (2000+) (u16)
years = (gps.date.year());
// Hour (0-23) (u8)
hr = (gps.time.hour());
// Minute (0-59) (u8)
mins = (gps.time.minute());
// Second (0-59) (u8)
secs = (gps.time.second());
Serial.println(longitude);
Serial.println(latitude);
Serial.println(days);
Serial.println(months);
Serial.println(years);
Serial.println(hr);
Serial.println(mins);
Serial.println(secs);
}
}
}
void sendHttpResponse(WiFiEspClient client)
{
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.println("<br>");
client.println("<br>");
client.print("Latitude= ");
client.print(latitude);
client.print(" Longitude = ");
client.println(longitude);
client.println("<br>");
client.print("Date: ");
client.print(days);
client.print("/");
client.print(months);
client.print("/");
client.print(years);
client.println("<br>");
client.print("Time: ");
client.print(hr);
client.print(":");
client.print(mins);
client.print(":");
client.print(secs);
// The HTTP response ends with another blank line:
client.println();
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println();
}