mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 06:33:51 +00:00
Fix GPS time source. (#704)
* Change ESP32 default power_save_mode to light * Update
This commit is contained in:
@@ -8,5 +8,41 @@ static const char *TAG = "gps";
|
||||
|
||||
TinyGPSPlus &GPSListener::get_tiny_gps() { return this->parent_->get_tiny_gps(); }
|
||||
|
||||
void GPS::loop() {
|
||||
while (this->available() && !this->has_time_) {
|
||||
if (this->tiny_gps_.encode(this->read())) {
|
||||
if (tiny_gps_.location.isUpdated()) {
|
||||
ESP_LOGD(TAG, "Location:");
|
||||
ESP_LOGD(TAG, " Lat: %f", tiny_gps_.location.lat());
|
||||
ESP_LOGD(TAG, " Lon: %f", tiny_gps_.location.lng());
|
||||
}
|
||||
|
||||
if (tiny_gps_.speed.isUpdated()) {
|
||||
ESP_LOGD(TAG, "Speed:");
|
||||
ESP_LOGD(TAG, " %f km/h", tiny_gps_.speed.kmph());
|
||||
}
|
||||
if (tiny_gps_.course.isUpdated()) {
|
||||
ESP_LOGD(TAG, "Course:");
|
||||
ESP_LOGD(TAG, " %f °", tiny_gps_.course.deg());
|
||||
}
|
||||
if (tiny_gps_.altitude.isUpdated()) {
|
||||
ESP_LOGD(TAG, "Altitude:");
|
||||
ESP_LOGD(TAG, " %f m", tiny_gps_.altitude.meters());
|
||||
}
|
||||
if (tiny_gps_.satellites.isUpdated()) {
|
||||
ESP_LOGD(TAG, "Satellites:");
|
||||
ESP_LOGD(TAG, " %d", tiny_gps_.satellites.value());
|
||||
}
|
||||
if (tiny_gps_.satellites.isUpdated()) {
|
||||
ESP_LOGD(TAG, "HDOP:");
|
||||
ESP_LOGD(TAG, " %.2f", tiny_gps_.hdop.hdop());
|
||||
}
|
||||
|
||||
for (auto *listener : this->listeners_)
|
||||
listener->on_update(this->tiny_gps_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gps
|
||||
} // namespace esphome
|
||||
|
||||
@@ -27,14 +27,7 @@ class GPS : public Component, public uart::UARTDevice {
|
||||
this->listeners_.push_back(listener);
|
||||
}
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
void loop() override {
|
||||
while (this->available() && !this->has_time_) {
|
||||
if (this->tiny_gps_.encode(this->read())) {
|
||||
for (auto *listener : this->listeners_)
|
||||
listener->on_update(this->tiny_gps_);
|
||||
}
|
||||
}
|
||||
}
|
||||
void loop() override;
|
||||
TinyGPSPlus &get_tiny_gps() { return this->tiny_gps_; }
|
||||
|
||||
protected:
|
||||
|
||||
@@ -6,5 +6,29 @@ namespace gps {
|
||||
|
||||
static const char *TAG = "gps.time";
|
||||
|
||||
void GPSTime::from_tiny_gps_(TinyGPSPlus &tiny_gps) {
|
||||
if (!tiny_gps.time.isValid() || !tiny_gps.date.isValid())
|
||||
return;
|
||||
if (!tiny_gps.time.isUpdated() || !tiny_gps.date.isUpdated())
|
||||
return;
|
||||
if (tiny_gps.date.year() < 2019)
|
||||
return;
|
||||
|
||||
time::ESPTime val{};
|
||||
val.year = tiny_gps.date.year();
|
||||
val.month = tiny_gps.date.month();
|
||||
val.day_of_month = tiny_gps.date.day();
|
||||
// Set these to valid value for recalc_timestamp_utc - it's not used for calculation
|
||||
val.day_of_week = 1;
|
||||
val.day_of_year = 1;
|
||||
|
||||
val.hour = tiny_gps.time.hour();
|
||||
val.minute = tiny_gps.time.minute();
|
||||
val.second = tiny_gps.time.second();
|
||||
val.recalc_timestamp_utc(false);
|
||||
this->synchronize_epoch_(val.timestamp);
|
||||
this->has_time_ = true;
|
||||
}
|
||||
|
||||
} // namespace gps
|
||||
} // namespace esphome
|
||||
|
||||
@@ -18,20 +18,7 @@ class GPSTime : public time::RealTimeClock, public GPSListener {
|
||||
}
|
||||
|
||||
protected:
|
||||
void from_tiny_gps_(TinyGPSPlus &tiny_gps) {
|
||||
if (!tiny_gps.time.isValid() || !tiny_gps.date.isValid())
|
||||
return;
|
||||
time::ESPTime val{};
|
||||
val.year = tiny_gps.date.year();
|
||||
val.month = tiny_gps.date.month();
|
||||
val.day_of_month = tiny_gps.date.day();
|
||||
val.hour = tiny_gps.time.hour();
|
||||
val.minute = tiny_gps.time.minute();
|
||||
val.second = tiny_gps.time.second();
|
||||
val.recalc_timestamp_utc(false);
|
||||
this->synchronize_epoch_(val.timestamp);
|
||||
this->has_time_ = true;
|
||||
}
|
||||
void from_tiny_gps_(TinyGPSPlus &tiny_gps);
|
||||
bool has_time_{false};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user