ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
gps.cpp
Go to the documentation of this file.
1#include "gps.h"
2#include "esphome/core/log.h"
3
4namespace esphome::gps {
5
6ESPHOME_LOG_TAG(TAG, "gps");
7
8TinyGPSPlus &GPSListener::get_tiny_gps() { return this->parent_->get_tiny_gps(); }
9
10void GPS::dump_config() {
11 ESP_LOGCONFIG(TAG, "GPS:");
12 LOG_SENSOR(" ", "Latitude", this->latitude_sensor_);
13 LOG_SENSOR(" ", "Longitude", this->longitude_sensor_);
14 LOG_SENSOR(" ", "Speed", this->speed_sensor_);
15 LOG_SENSOR(" ", "Course", this->course_sensor_);
16 LOG_SENSOR(" ", "Altitude", this->altitude_sensor_);
17 LOG_SENSOR(" ", "Satellites", this->satellites_sensor_);
18 LOG_SENSOR(" ", "HDOP", this->hdop_sensor_);
19}
20
21void GPS::update() {
22 PUBLISH_STATE(this->latitude_sensor_, this->latitude_);
23
24 PUBLISH_STATE(this->longitude_sensor_, this->longitude_);
25
26 PUBLISH_STATE(this->speed_sensor_, this->speed_);
27
28 PUBLISH_STATE(this->course_sensor_, this->course_);
29
30 PUBLISH_STATE(this->altitude_sensor_, this->altitude_);
31
32 PUBLISH_STATE(this->satellites_sensor_, this->satellites_);
33
34 PUBLISH_STATE(this->hdop_sensor_, this->hdop_);
35}
36
37void GPS::loop() {
38 while (this->available() > 0 && !this->has_time_) {
39 if (!this->tiny_gps_.encode(this->read())) {
40 continue;
41 }
42 if (this->tiny_gps_.location.isUpdated()) {
43 this->latitude_ = this->tiny_gps_.location.lat();
44 this->longitude_ = this->tiny_gps_.location.lng();
45 ESP_LOGV(TAG, "Latitude, Longitude: %.6f°, %.6f°", this->latitude_, this->longitude_);
46 }
47
48 if (this->tiny_gps_.speed.isUpdated()) {
49 this->speed_ = this->tiny_gps_.speed.kmph();
50 ESP_LOGV(TAG, "Speed: %.3f km/h", this->speed_);
51 }
52
53 if (this->tiny_gps_.course.isUpdated()) {
54 this->course_ = this->tiny_gps_.course.deg();
55 ESP_LOGV(TAG, "Course: %.2f°", this->course_);
56 }
57
58 if (this->tiny_gps_.altitude.isUpdated()) {
59 this->altitude_ = this->tiny_gps_.altitude.meters();
60 ESP_LOGV(TAG, "Altitude: %.2f m", this->altitude_);
61 }
62
63 if (this->tiny_gps_.satellites.isUpdated()) {
64 this->satellites_ = this->tiny_gps_.satellites.value();
65 ESP_LOGV(TAG, "Satellites: %d", this->satellites_);
66 }
67
68 if (this->tiny_gps_.hdop.isUpdated()) {
69 this->hdop_ = this->tiny_gps_.hdop.hdop();
70 ESP_LOGV(TAG, "HDOP: %.3f", this->hdop_);
71 }
72
73#ifdef GPS_LISTENER_COUNT
74 for (auto *listener : this->listeners_) {
75 listener->on_update(this->tiny_gps_);
76 }
77#endif
78 }
79}
80
81} // namespace esphome::gps
float speed_
Definition gps.h:51
float latitude_
Definition gps.h:49
uint16_t satellites_
Definition gps.h:55
float longitude_
Definition gps.h:50
bool has_time_
Definition gps.h:56
float altitude_
Definition gps.h:53
float hdop_
Definition gps.h:54
TinyGPSPlus tiny_gps_
Definition gps.h:58
float course_
Definition gps.h:52
StaticVector< GPSListener *, GPS_LISTENER_COUNT > listeners_
Definition gps.h:60
TinyGPSPlus & get_tiny_gps()
Definition gps.cpp:8
ESPHOME_LOG_TAG(TAG, "gps")