add some gnss sensors

This commit is contained in:
oarcher 2024-08-07 22:38:02 +02:00
parent 1256bfdd54
commit 1aa2ce8608
3 changed files with 72 additions and 32 deletions

View file

@ -2,14 +2,19 @@ import esphome.codegen as cg
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_ACCURACY,
CONF_ALTITUDE,
CONF_COURSE,
CONF_ID,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_SPEED,
DEVICE_CLASS_SIGNAL_STRENGTH,
ENTITY_CATEGORY_DIAGNOSTIC,
STATE_CLASS_MEASUREMENT,
UNIT_DECIBEL,
UNIT_DEGREES,
UNIT_KILOMETER_PER_HOUR,
UNIT_METER,
UNIT_PERCENT,
)
@ -29,6 +34,14 @@ IS_PLATFORM_COMPONENT = True
CONF_BER = "ber"
CONF_RSSI = "rssi"
ICON_LATITUDE = "mdi:latitude"
ICON_LONGITUDE = "mdi:longitude"
ICON_LOCATION = "mdi:map-marker"
ICON_COMPASS = "mdi:compass"
ICON_LOCATION_RADIUS = "mdi:map-marker-radius"
ICON_LOCATION_UP = "mdi:map-marker-up"
ICON_SPEED = "mdi:speedometer"
ICON_SIGNAL_BAR = "mdi:signal"
modem_sensor_ns = cg.esphome_ns.namespace("modem_sensor")
ModemSensorComponent = modem_sensor_ns.class_("ModemSensor", cg.PollingComponent)
@ -41,31 +54,52 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_RSSI): sensor.sensor_schema(
unit_of_measurement=UNIT_DECIBEL,
accuracy_decimals=0,
icon=ICON_SIGNAL_BAR,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_BER): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_LATITUDE): sensor.sensor_schema(
unit_of_measurement=UNIT_DEGREES,
accuracy_decimals=5,
# device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
icon=ICON_LATITUDE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_LONGITUDE): sensor.sensor_schema(
unit_of_measurement=UNIT_DEGREES,
accuracy_decimals=5,
# device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
icon=ICON_LONGITUDE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_ALTITUDE): sensor.sensor_schema(
unit_of_measurement=UNIT_METER,
accuracy_decimals=1,
# device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
icon=ICON_LOCATION_UP,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_SPEED): sensor.sensor_schema(
unit_of_measurement=UNIT_KILOMETER_PER_HOUR,
accuracy_decimals=1,
icon=ICON_SPEED,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_ACCURACY): sensor.sensor_schema(
unit_of_measurement=UNIT_METER,
accuracy_decimals=1,
icon=ICON_LOCATION_RADIUS,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_COURSE): sensor.sensor_schema(
unit_of_measurement=UNIT_DEGREES,
accuracy_decimals=1,
icon=ICON_COMPASS,
state_class=STATE_CLASS_MEASUREMENT,
),
}
@ -112,4 +146,16 @@ async def to_code(config):
altitude_sensor = await sensor.new_sensor(altitude)
cg.add(var.set_altitude_sensor(altitude_sensor))
if speed := config.get(CONF_SPEED, None):
speed_sensor = await sensor.new_sensor(speed)
cg.add(var.set_altitude_sensor(speed_sensor))
if course := config.get(CONF_COURSE, None):
course_sensor = await sensor.new_sensor(course)
cg.add(var.set_course_sensor(course_sensor))
if accuracy := config.get(CONF_ACCURACY, None):
accuracy_sensor = await sensor.new_sensor(accuracy)
cg.add(var.set_accuracy_sensor(accuracy_sensor))
await cg.register_component(var, config)

View file

@ -76,7 +76,7 @@ std::map<std::string, std::string> get_gnssinfo_tokens(const std::string &gnss_i
std::map<std::string, std::string> gnss_data;
if (data.find(",,,,,,") != std::string::npos) {
// no data recieved
ESP_LOGW(TAG, "No GNSS location available");
return gnss_data;
}
@ -202,19 +202,19 @@ void ModemSensor::update_gnss_sensors_() {
int minute = std::stoi(parts["time"].substr(2, 2));
int second = std::stoi(parts["time"].substr(4, 2));
ESP_LOGD(TAG, "Latitude: %f, Longitude: %f", lat, lon);
ESP_LOGD(TAG, "Altitude: %f m", alt);
ESP_LOGD(TAG, "Speed: %f km/h", speed_kmh);
ESP_LOGD(TAG, "COG: %f degrees", cog);
ESP_LOGD(TAG, "PDOP: %f", pdop);
ESP_LOGD(TAG, "HDOP: %f", hdop);
ESP_LOGD(TAG, "VDOP: %f", vdop);
ESP_LOGD(TAG, "GPS SVs: %d", gps_svs);
ESP_LOGD(TAG, "GLONASS SVs: %d", glonass_svs);
ESP_LOGD(TAG, "BEIDOU SVs: %d", beidou_svs);
ESP_LOGD(TAG, "Fix mode: %d", mode);
ESP_LOGD(TAG, "Date: %04d-%02d-%02d", year, month, day);
ESP_LOGD(TAG, "Time: %02d:%02d:%02d", hour, minute, second);
ESP_LOGV(TAG, "Latitude: %f, Longitude: %f", lat, lon);
ESP_LOGV(TAG, "Altitude: %f m", alt);
ESP_LOGV(TAG, "Speed: %f km/h", speed_kmh);
ESP_LOGV(TAG, "COG: %f degrees", cog);
ESP_LOGV(TAG, "PDOP: %f", pdop);
ESP_LOGV(TAG, "HDOP: %f", hdop);
ESP_LOGV(TAG, "VDOP: %f", vdop);
ESP_LOGV(TAG, "GPS SVs: %d", gps_svs);
ESP_LOGV(TAG, "GLONASS SVs: %d", glonass_svs);
ESP_LOGV(TAG, "BEIDOU SVs: %d", beidou_svs);
ESP_LOGV(TAG, "Fix mode: %d", mode);
ESP_LOGV(TAG, "Date: %04d-%02d-%02d", year, month, day);
ESP_LOGV(TAG, "Time: %02d:%02d:%02d", hour, minute, second);
// Sensors update
if (this->gnss_latitude_sensor_)
@ -225,16 +225,10 @@ void ModemSensor::update_gnss_sensors_() {
this->gnss_altitude_sensor_->publish_state(alt);
if (this->gnss_speed_sensor_)
this->gnss_speed_sensor_->publish_state(speed_kmh);
if (this->gnss_cog_sensor_)
this->gnss_cog_sensor_->publish_state(speed_kmh);
if (this->gnss_pdop_sensor_)
this->gnss_pdop_sensor_->publish_state(pdop);
if (this->gnss_hdop_sensor_)
this->gnss_hdop_sensor_->publish_state(hdop);
if (this->gnss_vdop_sensor_)
this->gnss_vdop_sensor_->publish_state(vdop);
if (this->gnss_mode_sensor_)
this->gnss_mode_sensor_->publish_state(mode);
if (this->gnss_course_sensor_)
this->gnss_course_sensor_->publish_state(cog);
if (this->gnss_accuracy_sensor_)
this->gnss_accuracy_sensor_->publish_state(hdop * 5);
}
}
}

View file

@ -24,6 +24,9 @@ class ModemSensor : public PollingComponent {
void set_latitude_sensor(sensor::Sensor *latitude_sensor) { this->gnss_latitude_sensor_ = latitude_sensor; }
void set_longitude_sensor(sensor::Sensor *longitude_sensor) { this->gnss_longitude_sensor_ = longitude_sensor; }
void set_altitude_sensor(sensor::Sensor *altitude_sensor) { this->gnss_altitude_sensor_ = altitude_sensor; }
void set_course_sensor(sensor::Sensor *course_sensor) { this->gnss_course_sensor_ = course_sensor; }
void set_speed_sensor(sensor::Sensor *speed_sensor) { this->gnss_speed_sensor_ = speed_sensor; }
void set_accuracy_sensor(sensor::Sensor *accuracy_sensor) { this->gnss_accuracy_sensor_ = accuracy_sensor; }
#endif // USE_MODEM_GNSS
// ========== INTERNAL METHODS ==========
@ -44,11 +47,8 @@ class ModemSensor : public PollingComponent {
sensor::Sensor *gnss_longitude_sensor_{nullptr};
sensor::Sensor *gnss_altitude_sensor_{nullptr};
sensor::Sensor *gnss_speed_sensor_{nullptr};
sensor::Sensor *gnss_cog_sensor_{nullptr};
sensor::Sensor *gnss_pdop_sensor_{nullptr};
sensor::Sensor *gnss_hdop_sensor_{nullptr};
sensor::Sensor *gnss_vdop_sensor_{nullptr};
sensor::Sensor *gnss_mode_sensor_{nullptr};
sensor::Sensor *gnss_course_sensor_{nullptr};
sensor::Sensor *gnss_accuracy_sensor_{nullptr};
void update_gnss_sensors_();
#endif // USE_MODEM_GNSS
};