ESPHome 2026.5.0b1
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
6static const char *const TAG = "gps";
7
8TinyGPSPlus &GPSListener::get_tiny_gps() { return this->parent_->get_tiny_gps(); }
9
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
22 if (this->latitude_sensor_ != nullptr) {
24 }
25
26 if (this->longitude_sensor_ != nullptr) {
28 }
29
30 if (this->speed_sensor_ != nullptr) {
32 }
33
34 if (this->course_sensor_ != nullptr) {
36 }
37
38 if (this->altitude_sensor_ != nullptr) {
40 }
41
42 if (this->satellites_sensor_ != nullptr) {
44 }
45
46 if (this->hdop_sensor_ != nullptr) {
47 this->hdop_sensor_->publish_state(this->hdop_);
48 }
49}
50
51void GPS::loop() {
52 while (this->available() > 0 && !this->has_time_) {
53 if (!this->tiny_gps_.encode(this->read())) {
54 continue;
55 }
56 if (this->tiny_gps_.location.isUpdated()) {
57 this->latitude_ = this->tiny_gps_.location.lat();
58 this->longitude_ = this->tiny_gps_.location.lng();
59 ESP_LOGV(TAG, "Latitude, Longitude: %.6f°, %.6f°", this->latitude_, this->longitude_);
60 }
61
62 if (this->tiny_gps_.speed.isUpdated()) {
63 this->speed_ = this->tiny_gps_.speed.kmph();
64 ESP_LOGV(TAG, "Speed: %.3f km/h", this->speed_);
65 }
66
67 if (this->tiny_gps_.course.isUpdated()) {
68 this->course_ = this->tiny_gps_.course.deg();
69 ESP_LOGV(TAG, "Course: %.2f°", this->course_);
70 }
71
72 if (this->tiny_gps_.altitude.isUpdated()) {
73 this->altitude_ = this->tiny_gps_.altitude.meters();
74 ESP_LOGV(TAG, "Altitude: %.2f m", this->altitude_);
75 }
76
77 if (this->tiny_gps_.satellites.isUpdated()) {
78 this->satellites_ = this->tiny_gps_.satellites.value();
79 ESP_LOGV(TAG, "Satellites: %d", this->satellites_);
80 }
81
82 if (this->tiny_gps_.hdop.isUpdated()) {
83 this->hdop_ = this->tiny_gps_.hdop.hdop();
84 ESP_LOGV(TAG, "HDOP: %.3f", this->hdop_);
85 }
86
87 for (auto *listener : this->listeners_) {
88 listener->on_update(this->tiny_gps_);
89 }
90 }
91}
92
93} // namespace esphome::gps
sensor::Sensor * altitude_sensor_
Definition gps.h:61
float speed_
Definition gps.h:50
sensor::Sensor * hdop_sensor_
Definition gps.h:63
sensor::Sensor * course_sensor_
Definition gps.h:60
void update() override
Definition gps.cpp:21
float latitude_
Definition gps.h:48
std::vector< GPSListener * > listeners_
Definition gps.h:66
uint16_t satellites_
Definition gps.h:54
void dump_config() override
Definition gps.cpp:10
sensor::Sensor * speed_sensor_
Definition gps.h:59
sensor::Sensor * latitude_sensor_
Definition gps.h:57
float longitude_
Definition gps.h:49
void loop() override
Definition gps.cpp:51
bool has_time_
Definition gps.h:55
sensor::Sensor * satellites_sensor_
Definition gps.h:62
sensor::Sensor * longitude_sensor_
Definition gps.h:58
float altitude_
Definition gps.h:52
float hdop_
Definition gps.h:53
TinyGPSPlus tiny_gps_
Definition gps.h:65
TinyGPSPlus & get_tiny_gps()
Definition gps.h:45
float course_
Definition gps.h:51
TinyGPSPlus & get_tiny_gps()
Definition gps.cpp:8
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68