Skip to content

Instantly share code, notes, and snippets.

@goran-mahovlic
Created February 13, 2021 15:07
Show Gist options
  • Save goran-mahovlic/4fe6e1b688010376cadca9028d5e9dda to your computer and use it in GitHub Desktop.
Save goran-mahovlic/4fe6e1b688010376cadca9028d5e9dda to your computer and use it in GitHub Desktop.
/**
* Rob 5/16/2019
* Github: Rob4226
*
* Basic code using U-blox based GPS module via UART
* using HardwareSerial and NeoGPS Arudino library to
* parse various NMEA sentences with ESP32 or other boards.
*
* */
#include <Arduino.h>
#include <NMEAGPS.h>
//-------------------------------------------------------------------------
// Check that the config files are set up properly
#if !defined(NMEAGPS_PARSE_RMC) & \
!defined(NMEAGPS_PARSE_GGA) & \
!defined(NMEAGPS_PARSE_GLL)
#error You must uncomment at least one of NMEAGPS_PARSE_RMC, NMEAGPS_PARSE_GGA or NMEAGPS_PARSE_GLL in NMEAGPS_cfg.h!
#endif
#if !defined(GPS_FIX_LOCATION)
#error You must uncomment GPS_FIX_LOCATION in GPSfix_cfg.h!
#endif
#define gpsPort Serial2 //ESP32 pins 16RX, 17TX UART02
#define GPS_PORT_NAME "Serial2"
#define DEBUG_PORT Serial
#define BAUD_GPS 9600
#define BAUD_USB_SERIAL 115200
#define fixLEDPin 2
int incomingByte = 0;
using namespace NeoGPS;
NMEAGPS gps;
int batt;
int last_batt = 9000;
float lat=0.0, lng = 0.0;
int alt=0;
static void doSomeWork( const gps_fix & fix );
static void doSomeWork( const gps_fix & fix )
{
if (fix.valid.location) {
lat = fix.latitude();
lng = fix.longitude();
alt = fix.altitude();
Serial.println(lat,6);
Serial.println(lng,6);
}
} // doSomeWork
void setup()
{
Serial.begin(BAUD_USB_SERIAL);
pinMode(fixLEDPin, OUTPUT);
gpsPort.begin(BAUD_GPS);
//pinMode(34, INPUT); //vlažnost zemlje
adcAttachPin(34);
analogReadResolution(12);
analogSetAttenuation(ADC_11db);
// Wait for USB Serial
while (!Serial)
{
yield();
}
// By default, "16E TTL" and other U-blox GPS modules output RMC, VTG, CGA, GSA, GSV, GLL messages once a second (these are standard NMEA messages)
// Configure the GPS to only output what is needed, like just RMC as is shown below by disabling all other default sentences.
//gpsPort.println(F("$PUBX,40,RMC,0,0,0,0*47")); //RMC ON (Comented out to leave RMC on...uncomment this line to disable RMC)
//delay(100);
gpsPort.println(F("$PUBX,40,VTG,0,0,0,0*5E")); //VTG OFF
delay(100);
gpsPort.println(F("$PUBX,40,GGA,0,0,0,0*5A")); //GGA OFF
delay(100);
gpsPort.println(F("$PUBX,40,GSA,0,0,0,0*4E")); //GSA OFF
delay(100);
gpsPort.println(F("$PUBX,40,GSV,0,0,0,0*59")); //GSV OFF
delay(100);
gpsPort.println(F("$PUBX,40,GLL,0,0,0,0*5C")); //GLL OFF
delay(100);
}
void loop()
{
while (gpsPort.available()) // basically true every second if 1Hz from GPS, only true when the GPS returns a sentence
{
gps_fix fix = gps.read(); // save the latest
incomingByte = gpsPort.read(); // Clear serial buffer
//DEBUG_PORT.println(incomingByte);
// Set the "fix" LED to on or off
bool gpsWasFixed = fix.valid.status && (fix.status >= gps_fix::STATUS_STD);
digitalWrite(fixLEDPin, gpsWasFixed);
// When we have a valid location, print the latitude and longitude to the USB Serial terminal
if (fix.valid.location)
{
DEBUG_PORT.println("The fix's location is valid!");
DEBUG_PORT.print(" Latitude: ");
DEBUG_PORT.println(fix.latitudeL());
DEBUG_PORT.print(" Longitude: ");
DEBUG_PORT.println(fix.longitudeL());
}
digitalWrite(fixLEDPin,HIGH);
} // gps.available while loop
digitalWrite(fixLEDPin,LOW);
if(millis() - last_batt > 10000){
delay(150);
batt = analogRead(34);
Serial.print("Batt: ");
float batt_volt = map(batt, 720,1120,3000,4200)/1000.0;
Serial.print(batt_volt);
Serial.print("V ");
Serial.println(batt);
last_batt = millis();
gpsPort.println(F("$GPRMC,092751.000,A,5321.6802,N,00630.3371,W,0.06,31.66,280511,,,A*45"));
}
} // loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment