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