Hi All,
I have an ESP8266 programmed using arduino connected to a NEO6YM GPS sensor. I trying to display latitude and longitude on the captive portal page alongside speed data, course, and distance to London (can use any location, but London is default in example sketches for the tinygps library.
The GPS module displays location data on the serial monitor, but not on the captive portal hosted on the esp8266 MCU.
Below is my code:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = D1, TXPin = D2;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin);
const char* topspeed = "";
String mylonstr = "";
String mylatstr = "";
double waypointlon;
double waypointlat;
double mylat;
double mylon;
double prevlat;
double prevlon;
double distanceToWaypoint;
double courseToWaypoint;
const char* coursetoWay;
int addr = 0;
int address = 0;
byte value; // unknown!
const byte DNS_PORT = 53;
IPAddress apIP(172, 217, 28, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);
String responseHTML = "<!DOCTYPE html><html lang='en'><head>"
"<meta name='viewport' content='width=device-width'>"
"<title>Beacon</title></head><body>"
"<h1>Tracker</h1>"
"<button onClick=\"window.location.reload();\">Refresh Location</button>"
"</body></html>";
String datarmylat = mylatstr;
String datarmylon = mylonstr;
String br = "<br><br>";
String urll = "<button><a href=\"http://maps.google.com/maps?z=12&t=m&q=loc:" + datarmylat + "+" + datarmylon + "/" + "</a>View on Google Maps</button>";
//"<p>" nmode + br + datarmylat + br + speedint + "</p>";
String responseHTML2 = responseHTML + br + urll + br;
void setup() {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("CaptivePortal");
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", apIP);
// replay to all requests with same HTML
webServer.onNotFound([]() {
webServer.send(200, "text/html", responseHTML2);
//webServer.send(200, "text/html", "test");
});
webServer.begin();
Serial.begin(9600);
ss.begin(GPSBaud);
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
// Latitude in degrees (double)
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
double mylat = (gps.location.lat(), 6);
// Longitude in degrees (double)
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
double mylon = (gps.location.lng(), 6);
String mylatstr = String(mylat,6);
String mylonstr = String(mylon,6);
// Raw latitude in whole degrees
Serial.print("Raw latitude = ");
Serial.print(gps.location.rawLat().negative ? "-" : "+");
Serial.println(gps.location.rawLat().deg);
// ... and billionths (u16/u32)
Serial.println(gps.location.rawLat().billionths);
// Raw longitude in whole degrees
Serial.print("Raw longitude = ");
Serial.print(gps.location.rawLng().negative ? "-" : "+");
Serial.println(gps.location.rawLng().deg);
// ... and billionths (u16/u32)
Serial.println(gps.location.rawLng().billionths);
// Raw date in DDMMYY format (u32)
Serial.print("Raw date DDMMYY = ");
Serial.println(gps.date.value());
//uint32_t timeint = (gps.date.value());
//char timy[32];
///sprintf(timy, "%02d", timeint);
//WiFi.softAP(timy, password);
// WiFi.softAP(timeint, password);
// Year (2000+) (u16)
Serial.print("Year = ");
Serial.println(gps.date.year());
// Month (1-12) (u8)
Serial.print("Month = ");
Serial.println(gps.date.month());
// Day (1-31) (u8)
Serial.print("Day = ");
Serial.println(gps.date.day());
// Raw time in HHMMSSCC format (u32)
Serial.print("Raw time in HHMMSSCC = ");
Serial.println(gps.time.value());
// Hour (0-23) (u8)
Serial.print("Hour = ");
Serial.println(gps.time.hour());
// Minute (0-59) (u8)
Serial.print("Minute = ");
Serial.println(gps.time.minute());
// Second (0-59) (u8)
Serial.print("Second = ");
Serial.println(gps.time.second());
// 100ths of a second (0-99) (u8)
Serial.print("Centisecond = ");
Serial.println(gps.time.centisecond());
// Raw speed in 100ths of a knot (i32)
Serial.print("Raw speed in 100ths/knot = ");
Serial.println(gps.speed.value());
// Speed in knots (double)
Serial.print("Speed in knots/h = ");
Serial.println(gps.speed.knots());
// Speed in miles per hour (double)
Serial.print("Speed in miles/h = ");
Serial.println(gps.speed.mph());
// const char* speedint = (gps.speed.mph());
//char spedy[32];
//sprintf(spedy, "%02d", speedint);
// if (topspeed < speedint) {
// topspeed = speedint;
// }
// WiFi.softAP(spedy, password);
// Speed in meters per second (double)
Serial.print("Speed in m/s = ");
Serial.println(gps.speed.mps());
// Speed in kilometers per hour (double)
Serial.print("Speed in km/h = ");
Serial.println(gps.speed.kmph());
// Raw course in 100ths of a degree (i32)
Serial.print("Raw course in degrees = ");
Serial.println(gps.course.value());
// Course in degrees (double)
Serial.print("Course in degrees = ");
Serial.println(gps.course.deg());
// Raw altitude in centimeters (i32)
Serial.print("Raw altitude in centimeters = ");
Serial.println(gps.altitude.value());
// Altitude in meters (double)
Serial.print("Altitude in meters = ");
Serial.println(gps.altitude.meters());
// Altitude in miles (double)
Serial.print("Altitude in miles = ");
Serial.println(gps.altitude.miles());
// Altitude in kilometers (double)
Serial.print("Altitude in kilometers = ");
Serial.println(gps.altitude.kilometers());
// Altitude in feet (double)
Serial.print("Altitude in feet = ");
Serial.println(gps.altitude.feet());
// Number of satellites in use (u32)
Serial.print("Number os satellites in use = ");
Serial.println(gps.satellites.value());
// Horizontal Dim. of Precision (100ths-i32)
Serial.print("HDOP = ");
Serial.println(gps.hdop.value());
if (gps.location.isValid())
{
static const double WAYPOINT_LAT = waypointlat, WAYPOINT_LON = waypointlon;
double distanceToWaypoint =
TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
WAYPOINT_LAT,
WAYPOINT_LON);
double courseToWaypoint =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
WAYPOINT_LAT,
WAYPOINT_LON);
if (mylon != prevlon || mylat != prevlat) {
static const double CHANGE_LAT = prevlat, CHANGE_LON = prevlon; //state change detection addrs.
double distanceToprev =
TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
CHANGE_LAT,
CHANGE_LON);
double courseToprev =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
CHANGE_LAT,
CHANGE_LON);
}
Serial.print(F("spacing Distance="));
Serial.print(distanceToprev/1000, 6); //distance from last position.
Serial.print(F("waypoint Distance="));
Serial.print(distanceToWaypoint/1000, 6); //distance to next waypoint.
//Serial.print(F(" km Course-to="));
Serial.print(courseToWaypoint, 6);
Serial.print(F(" degrees ["));
Serial.print(TinyGPSPlus::cardinal(courseToWaypoint));
Serial.println(F("]"));
}
}
}
Serial.print("Address =");
Serial.print(address);
Serial.print("waypoint lat =");
Serial.println(waypointlat);
Serial.print("waypoint lon =");
Serial.println(waypointlon);
}
I think it is not able to display double or const char data types in the html, that's my guess why it's not displaying that data on the captive portal. Either that or the uint32 and const chars are not comparable using greater than or equal to operators.
Any Suggestions?