tracker: plain dev modem, GNSS from the +CGNSSINFO URC
CI / YAML lint (push) Failing after 4s
CI / ESPHome config validation (push) Failing after 4s

pr#14683 brought rssi, ber, network_type and urc to dev, but no GNSS, so the
fork of pr#9802 is gone. GNSS is switched with modem.send_at and the position
is parsed from the URC until the modem gets official GNSS support. The device
tracker also gets the accuracy (DOP * gnss_uere_m), the vertical accuracy and
the battery level.
This commit is contained in:
randy committed 2026-10-24 23:45:56 +02:00
1 parent 5025159798
commit b660638999
1 file changed
+111 -83
+111 -83
View File
@@ -34,6 +34,8 @@ substitutions:
# the AXP itself cuts off at 2.9 V (VOFF) # the AXP itself cuts off at 2.9 V (VOFF)
battery_low_percent: "20" battery_low_percent: "20"
battery_off_voltage: "3.3" battery_off_voltage: "3.3"
# The modem reports only DOP; accuracy in m = DOP * this typical range error. Tune against a known spot
gnss_uere_m: "5"
esphome: esphome:
name: mcu-${device} name: mcu-${device}
@@ -75,10 +77,6 @@ external_components:
- source: github://m5stack/esphome-yaml/components - source: github://m5stack/esphome-yaml/components
components: [axp192] components: [axp192]
refresh: 1h refresh: 1h
# Signal, network type and GNSS sensors from pr#9802 plus our fix for the AT+CGPS=1 resend loop
# (https://github.com/esphome/esphome/pull/9802#issuecomment-6381760281); the modem itself is in dev
- source: github://randybb/esphome@modem-gnss-fix
components: [modem]
logger: logger:
level: VERBOSE level: VERBOSE
@@ -104,6 +102,14 @@ modem:
# pin_code: "" # pin_code: ""
init_at: init_at:
- AT+CPSI=60 # URC +CPSI every 60 s, for the operator - AT+CPSI=60 # URC +CPSI every 60 s, for the operator
- AT+CGNSSINFO=30 # URC +CGNSSINFO every 30 s while GNSS runs; dev has no GNSS sensors
on_connect:
# Survives an ESP reboot without a modem power cycle; the modem answers ERROR if GNSS already runs
- if:
condition:
switch.is_on: gnss_switch
then:
- modem.send_at: AT+CGPS=1
# API is unreachable over LTE, the tracker has to talk MQTT # API is unreachable over LTE, the tracker has to talk MQTT
mqtt: mqtt:
@@ -291,43 +297,54 @@ sensor:
entity_category: diagnostic entity_category: diagnostic
filters: filters:
- heartbeat: ${report_interval} - heartbeat: ${report_interval}
# lat/lon deltas are ~50 m at 48° N # Filled from the +CGNSSINFO URC. lat/lon deltas are ~50 m at 48° N
latitude: - platform: template
id: latitude id: latitude
name: Latitude name: Latitude
internal: true internal: true
on_value: update_interval: never
- script.execute: publish_location on_value:
filters: - script.execute: publish_location
- or: filters:
- heartbeat: ${report_interval} - or:
- delta: 0.0005 - heartbeat: ${report_interval}
longitude: - delta: 0.0005
id: longitude - platform: template
name: Longitude id: longitude
internal: true name: Longitude
on_value: internal: true
- script.execute: publish_location update_interval: never
filters: on_value:
- or: - script.execute: publish_location
- heartbeat: ${report_interval} filters:
- delta: 0.0007 - or:
altitude: - heartbeat: ${report_interval}
id: altitude - delta: 0.0007
name: Altitude - platform: template
internal: true id: altitude
speed: name: Altitude
id: speed internal: true
name: Speed update_interval: never
internal: true - platform: template
course: id: speed
id: course name: Speed
name: Course internal: true
internal: true update_interval: never
accuracy: - platform: template
id: gnss_accuracy id: course
name: GNSS Accuracy name: Course
internal: true internal: true
update_interval: never
- platform: template
id: gnss_accuracy
name: GNSS Accuracy
internal: true
update_interval: never
- platform: template
id: gnss_vertical_accuracy
name: GNSS Vertical Accuracy
internal: true
update_interval: never
switch: switch:
- platform: gpio - platform: gpio
@@ -343,11 +360,15 @@ switch:
- platform: output - platform: output
id: gnss_enable id: gnss_enable
output: gnss_power output: gnss_power
- platform: modem - platform: template
gnss: id: gnss_switch
id: gnss_switch name: GNSS
name: GNSS optimistic: true
restore_mode: ALWAYS_ON restore_mode: ALWAYS_ON
turn_on_action:
- modem.send_at: AT+CGPS=1
turn_off_action:
- modem.send_at: AT+CGPS=0
text_sensor: text_sensor:
- <<: !include common/text_sensor/version.yaml - <<: !include common/text_sensor/version.yaml
@@ -362,7 +383,7 @@ text_sensor:
name: Modem Network name: Modem Network
entity_category: diagnostic entity_category: diagnostic
update_interval: never update_interval: never
# The operator comes from the +CPSI URC; the component has no sensor for it # The operator comes from the +CPSI URC, the position from +CGNSSINFO; the component has sensors for neither
- platform: modem - platform: modem
network_type: network_type:
id: modem_network_raw id: modem_network_raw
@@ -378,40 +399,43 @@ text_sensor:
- lambda: |- - lambda: |-
// +CPSI: <system mode>,<operation mode>[,<MCC>-<MNC>,...] // +CPSI: <system mode>,<operation mode>[,<MCC>-<MNC>,...]
char op[16]; char op[16];
if (sscanf(x.c_str(), "+CPSI: %*[^,],%*[^,],%15[^,]", op) == 1 && id(modem_operator).state != op) if (sscanf(x.c_str(), "+CPSI: %*[^,],%*[^,],%15[^,]", op) == 1) {
id(modem_operator).publish_state(op); if (id(modem_operator).state != op)
id(modem_operator).publish_state(op);
font: return;
- file: "gfonts://Roboto" }
id: font_small // +CGNSSINFO: <mode>,<satellite counts>,<ddmm.mmmmmm>,<N/S>,<dddmm.mmmmmm>,<E/W>,<date>,<UTC time>,
size: 12 // <alt m>,<speed kn>,<course>,<PDOP>,<HDOP>,<VDOP>
// The number of satellite counts differs between firmwares, so the fields are found from N/S.
display: // Without a fix all fields are empty
- platform: ssd1306_i2c if (x.rfind("+CGNSSINFO:", 0) != 0)
model: "SSD1306 128x64" return;
address: 0x3C std::vector<std::string> f;
i2c_id: system_i2c for (size_t i = x.find(':') + 1, j; ; i = j + 1) {
update_interval: 2s j = x.find(',', i);
lambda: |- f.push_back(x.substr(i, j == std::string::npos ? j : j - i));
it.printf(0, 0, id(font_small), "%s %s", id(modem_network).has_state() ? id(modem_network).state.c_str() : "--", if (j == std::string::npos)
id(modem_operator).state.c_str()); break;
if (id(modem_rssi).has_state() && !std::isnan(id(modem_rssi).state)) }
it.printf(127, 0, id(font_small), TextAlign::TOP_RIGHT, "%.0f dBm", id(modem_rssi).state); size_t ns = 1;
it.printf(0, 16, id(font_small), "PPP: %s", id(atmodem).is_connected() ? "OK" : "--"); while (ns < f.size() && f[ns] != "N" && f[ns] != "S")
it.printf(127, 16, id(font_small), TextAlign::TOP_RIGHT, "MQTT: %s", id(mqtt_client)->is_connected() ? "OK" : "--"); ns++;
if (std::isnan(id(latitude).state)) if (ns + 10 > f.size()) {
it.printf(0, 32, id(font_small), "GNSS: no fix"); id(latitude).publish_state(NAN);
else id(longitude).publish_state(NAN);
it.printf(0, 32, id(font_small), "%.5f %.5f", id(latitude).state, id(longitude).state); id(speed).publish_state(NAN);
auto now = id(sntp_time).now(); return;
if (now.is_valid()) }
it.strftime(0, 48, id(font_small), "%H:%M", now); auto num = [&](size_t i) { return f[i].empty() ? NAN : strtof(f[i].c_str(), nullptr); };
else auto deg = [](double v) { double d = floor(v / 100); return d + (v - d * 100) / 60; };
it.printf(0, 48, id(font_small), "Up %.2f d", id(uptime_sensor).state); double lat = deg(strtod(f[ns - 1].c_str(), nullptr)), lon = deg(strtod(f[ns + 1].c_str(), nullptr));
// The unrounded value; HA only gets 10 % steps id(altitude).publish_state(num(ns + 5));
float bat = std::isnan(id(battery_soc).state) ? id(battery_level).state : id(battery_soc).state; id(speed).publish_state(num(ns + 6) * 1.852f);
if (!std::isnan(bat)) id(course).publish_state(num(ns + 7));
it.printf(127, 48, id(font_small), TextAlign::TOP_RIGHT, "Bat %.0f%%", bat); id(gnss_accuracy).publish_state(num(ns + 9) * ${gnss_uere_m});
id(gnss_vertical_accuracy).publish_state(ns + 10 < f.size() ? num(ns + 10) * ${gnss_uere_m} : NAN);
id(latitude).publish_state(f[ns] == "S" ? -lat : lat);
id(longitude).publish_state(f[ns + 2] == "W" ? -lon : lon);
script: script:
# Latitude and longitude change in the same poll; one message for both # Latitude and longitude change in the same poll; one message for both
@@ -429,14 +453,18 @@ script:
payload: |- payload: |-
root["latitude"] = id(latitude).state; root["latitude"] = id(latitude).state;
root["longitude"] = id(longitude).state; root["longitude"] = id(longitude).state;
if (!std::isnan(id(gnss_accuracy).state))
root["gps_accuracy"] = id(gnss_accuracy).state;
if (!std::isnan(id(altitude).state)) if (!std::isnan(id(altitude).state))
root["altitude"] = id(altitude).state; root["altitude"] = id(altitude).state;
if (!std::isnan(id(speed).state)) if (!std::isnan(id(speed).state))
root["speed"] = id(speed).state; root["speed"] = id(speed).state;
if (!std::isnan(id(course).state)) if (!std::isnan(id(course).state))
root["course"] = id(course).state; root["course"] = id(course).state;
if (!std::isnan(id(gnss_accuracy).state))
root["gps_accuracy"] = roundf(id(gnss_accuracy).state);
if (!std::isnan(id(gnss_vertical_accuracy).state))
root["vertical_accuracy"] = roundf(id(gnss_vertical_accuracy).state);
if (!std::isnan(id(battery).state))
root["battery_level"] = id(battery).state;
time: time:
# Over PPP; one NTP exchange is ~150 B, so a few a day are enough for the ESP's clock # Over PPP; one NTP exchange is ~150 B, so a few a day are enough for the ESP's clock