ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
nmea.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
7
8namespace esphome::gps {
9
11static constexpr size_t NMEA_MAX_LINE = 96;
12
25static constexpr uint8_t GPS_FIELD_COUNT = 9;
26static_assert(GPS_FIELD_GEOID_SEPARATION == 1 << (GPS_FIELD_COUNT - 1), "GPS_FIELD_COUNT must match the highest field");
27
29struct GPSData {
30 int32_t latitude_e7{0};
31 int32_t longitude_e7{0};
32 int32_t altitude_e3{0};
33#ifdef USE_GPS_GEOID_SEPARATION
35#endif
37 uint16_t course_e2{0};
38 uint16_t hdop_e2{0};
39 uint16_t year{0};
40#ifdef USE_GPS_MILLISECOND
41 uint16_t millisecond{0};
42#endif
43 uint8_t month{0};
44 uint8_t day{0};
45 uint8_t hour{0};
46 uint8_t minute{0};
47 uint8_t second{0};
48 uint8_t satellites{0};
49 uint16_t updated{0};
50 uint16_t valid{0};
51
52 bool has(GPSField field) const { return (this->valid & field) != 0; }
53 bool is_updated(GPSField field) const { return (this->updated & field) != 0; }
54
55 // A double quotient rounds once to the float nearest the decimal value
56 float latitude() const { return static_cast<float>(this->latitude_e7 / 1e7); }
57 float longitude() const { return static_cast<float>(this->longitude_e7 / 1e7); }
58 float altitude() const { return static_cast<float>(this->altitude_e3 / 1e3); }
59 float speed_knots() const { return static_cast<float>(this->speed_e3 / 1e3); }
60 float speed_kmh() const { return static_cast<float>(this->speed_e3 * 1852.0 / 1e6); }
61 float course() const { return static_cast<float>(this->course_e2 / 1e2); }
62 float hdop() const { return static_cast<float>(this->hdop_e2 / 1e2); }
63#ifdef USE_GPS_GEOID_SEPARATION
64 float geoid_separation() const { return static_cast<float>(this->geoid_separation_e3 / 1e3); }
65#endif
66};
67
71bool parse_nmea_sentence(char *line, size_t len, GPSData &data);
72
73} // namespace esphome::gps
bool parse_nmea_sentence(char *line, size_t len, GPSData &data)
Parse one NMEA line of len bytes, which is changed in place; the sentence starts at the last '$'.
Definition nmea.cpp:226
GPSField
Bits of GPSData::updated and GPSData::valid.
Definition nmea.h:14
@ GPS_FIELD_LOCATION
Definition nmea.h:15
@ GPS_FIELD_GEOID_SEPARATION
Definition nmea.h:23
@ GPS_FIELD_COURSE
Definition nmea.h:17
@ GPS_FIELD_SPEED
Definition nmea.h:16
@ GPS_FIELD_SATELLITES
Definition nmea.h:19
@ GPS_FIELD_DATE
Definition nmea.h:22
@ GPS_FIELD_HDOP
Definition nmea.h:20
@ GPS_FIELD_ALTITUDE
Definition nmea.h:18
@ GPS_FIELD_TIME
Definition nmea.h:21
const void size_t len
Definition hal.h:68
void const char int line
Definition log.h:110
static void uint32_t
Values from GGA and RMC sentences as scaled integers; a name ending in eN is the value times 10^N.
Definition nmea.h:29
int32_t latitude_e7
degrees, north positive
Definition nmea.h:30
float geoid_separation() const
Definition nmea.h:64
int32_t geoid_separation_e3
meters from the WGS84 ellipsoid up to mean sea level
Definition nmea.h:34
float speed_kmh() const
Definition nmea.h:60
uint16_t millisecond
of the time; 0 when the time has no fraction
Definition nmea.h:41
float longitude() const
Definition nmea.h:57
uint8_t hour
UTC.
Definition nmea.h:45
float hdop() const
Definition nmea.h:62
uint32_t speed_e3
knots
Definition nmea.h:36
int32_t altitude_e3
meters above mean sea level
Definition nmea.h:32
float altitude() const
Definition nmea.h:58
int32_t longitude_e7
degrees, east positive
Definition nmea.h:31
float course() const
Definition nmea.h:61
float speed_knots() const
Definition nmea.h:59
uint16_t valid
GPSField bits set by any sentence so far.
Definition nmea.h:50
uint8_t satellites
Definition nmea.h:48
uint16_t updated
GPSField bits set by the last sentence.
Definition nmea.h:49
uint16_t hdop_e2
Definition nmea.h:38
bool has(GPSField field) const
Definition nmea.h:52
float latitude() const
Definition nmea.h:56
bool is_updated(GPSField field) const
Definition nmea.h:53
uint16_t year
Definition nmea.h:39
uint16_t course_e2
degrees from true north
Definition nmea.h:37