ESPHome  2024.3.1
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 
6 namespace esphome {
7 namespace gps {
8 
9 static const char *const TAG = "gps";
10 
11 TinyGPSPlus &GPSListener::get_tiny_gps() { return this->parent_->get_tiny_gps(); }
12 
13 void GPS::update() {
14  if (this->latitude_sensor_ != nullptr)
15  this->latitude_sensor_->publish_state(this->latitude_);
16 
17  if (this->longitude_sensor_ != nullptr)
18  this->longitude_sensor_->publish_state(this->longitude_);
19 
20  if (this->speed_sensor_ != nullptr)
21  this->speed_sensor_->publish_state(this->speed_);
22 
23  if (this->course_sensor_ != nullptr)
24  this->course_sensor_->publish_state(this->course_);
25 
26  if (this->altitude_sensor_ != nullptr)
27  this->altitude_sensor_->publish_state(this->altitude_);
28 
29  if (this->satellites_sensor_ != nullptr)
30  this->satellites_sensor_->publish_state(this->satellites_);
31 }
32 
33 void GPS::loop() {
34  while (this->available() && !this->has_time_) {
35  if (this->tiny_gps_.encode(this->read())) {
36  if (tiny_gps_.location.isUpdated()) {
37  this->latitude_ = tiny_gps_.location.lat();
38  this->longitude_ = tiny_gps_.location.lng();
39 
40  ESP_LOGD(TAG, "Location:");
41  ESP_LOGD(TAG, " Lat: %f", this->latitude_);
42  ESP_LOGD(TAG, " Lon: %f", this->longitude_);
43  }
44 
45  if (tiny_gps_.speed.isUpdated()) {
46  this->speed_ = tiny_gps_.speed.kmph();
47  ESP_LOGD(TAG, "Speed:");
48  ESP_LOGD(TAG, " %f km/h", this->speed_);
49  }
50  if (tiny_gps_.course.isUpdated()) {
51  this->course_ = tiny_gps_.course.deg();
52  ESP_LOGD(TAG, "Course:");
53  ESP_LOGD(TAG, " %f °", this->course_);
54  }
55  if (tiny_gps_.altitude.isUpdated()) {
56  this->altitude_ = tiny_gps_.altitude.meters();
57  ESP_LOGD(TAG, "Altitude:");
58  ESP_LOGD(TAG, " %f m", this->altitude_);
59  }
60  if (tiny_gps_.satellites.isUpdated()) {
61  this->satellites_ = tiny_gps_.satellites.value();
62  ESP_LOGD(TAG, "Satellites:");
63  ESP_LOGD(TAG, " %d", this->satellites_);
64  }
65 
66  for (auto *listener : this->listeners_)
67  listener->on_update(this->tiny_gps_);
68  }
69  }
70 }
71 
72 } // namespace gps
73 } // namespace esphome
74 
75 #endif // USE_ARDUINO
TinyGPSPlus & get_tiny_gps()
Definition: gps.cpp:11
void loop() override
Definition: gps.cpp:33
TinyGPSPlus & get_tiny_gps()
Definition: gps.h:46
void update() override
Definition: gps.cpp:13
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7