Hello,
i have a ublox-M8N + compass (HMC5883) module connected to an arduino uno and i use the tinygps++ library to parse the incoming NMEA sentences. The problem is that i never receive coordinates. I took it out to my balcony and to my yard, both with clear view to the sky but nothing. Even though the LED on the gps module started blinking, i didn't receive coordinates. Anyone knows why?
Normally i transmit the coordinates to a receiver connected to my laptop but i cut this part of the code as it is unnecessary
// Arduino Uno
// ---------------
/*
This sketch parses the incoming data from the gps to the arduino
GPS device hooked up on pins 2(rx) and 3(tx) and a HMC5883 Magnetic
Compass connected to the SCL/SDA pins.
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <stdlib.h>
static const int RXPin1 = 2, TXPin1 = 3, RPin1 = 4, RPin2 = 6;
static const uint32_t GPSBaud = 9600;
static double lat = 0.0,lon = 0.0;
// Assign a Uniquej ID to the HMC5883 Compass Sensor
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the NEO-M8N GPS module
SoftwareSerial s1(RXPin1, TXPin1);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup()
{
pinMode(RPin1, OUTPUT);
digitalWrite(RPin1, HIGH);
pinMode(RPin2, OUTPUT);
digitalWrite(RPin2, HIGH);
Serial.begin(GPSBaud);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
s1.begin(GPSBaud);
Serial.println(F("Debugging attached NEO-M8N GPS module and LoRa"));
Serial.print(F("Using TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println();
displaySensorDetails();
}
void loop()
{
Serial.println("Data from gps:");
delay(1000);
// This sketch displays information every time a new sentence is correctly encoded from the GPS Module.
while (s1.available() > 0) {
if (gps.encode(s1.read())) {
displayGpsInfo();
}
}
}
void displayGpsInfo()
{
// Prints the location if lat-lng information was recieved
Serial.print(F("Location: "));
if (gps.location.isValid())
{
lat = gps.location.lat();
lon = gps.location.lng();
Serial.print(lat, 6);
Serial.print(F(","));
Serial.print(lon, 6);
}
// prints invalid if no information was recieved in regards to location.
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" Date/Time: "));
// prints the received GPS module date if it was decoded in a valid response.
if (gps.date.isValid())
{
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
// prints invalid otherwise.
Serial.print(F("INVALID DATE"));
}
Serial.print(F(" "));
// prints the recieved GPS module time if it was decoded in a valid response.
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F("."));
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
// Print invalid otherwise.
Serial.print(F("INVALID TIME"));
}
Serial.println();
if (mag.begin())
{
displayCompassInfo();
}
}
void displayCompassInfo()
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" "); Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
float declinationAngle = 0.081;
heading += declinationAngle;
// Correct for when signs are reversed.
if (heading < 0)
heading += 2 * PI;
// Check for wrap due to addition of declination.
if (heading > 2 * PI)
heading -= 2 * PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180 / M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(1000);
}
float bearingCalc(char var[])
{
int i;
float y, x, b, lat, lon;
float lat_ar[8];
float lon_ar[8];
for (i = 0; i < 9; i++) {
lat_ar[i] = var[i];
lon_ar[i] = var[10+i];
}
//lat = atof
return b;
}
Serial monitor from inside the house:
Location: INVALID Date/Time: 30/05/2020 11:32:24.00
X: 4.55 Y: -32.27 Z: 88.98 uT
Heading (degrees): 282.66
Raw data from gps module from inside the house:
$GNVTG,,,,,,,,,N*2E
$GNGGA,,,,,,0,00,99.99,,,,,,*56
$GNGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*2E
$GNGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*2E
$GPGSV,1,1,00*79
$GLGSV,1,1,00*65
$GNGLL,,,,,,V,N*7A