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