mirror of
https://github.com/randybb/esphome-configs.git
synced 2026-12-06 09:06:52 +01:00
tracker: plain dev modem, GNSS from the +CGNSSINFO URC
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:
1 parent
5025159798
commit
b660638999
1 file changed
+77
-49
+77
-49
@@ -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
|
||||||
|
update_interval: never
|
||||||
on_value:
|
on_value:
|
||||||
- script.execute: publish_location
|
- script.execute: publish_location
|
||||||
filters:
|
filters:
|
||||||
- or:
|
- or:
|
||||||
- heartbeat: ${report_interval}
|
- heartbeat: ${report_interval}
|
||||||
- delta: 0.0005
|
- delta: 0.0005
|
||||||
longitude:
|
- platform: template
|
||||||
id: longitude
|
id: longitude
|
||||||
name: Longitude
|
name: Longitude
|
||||||
internal: true
|
internal: true
|
||||||
|
update_interval: never
|
||||||
on_value:
|
on_value:
|
||||||
- script.execute: publish_location
|
- script.execute: publish_location
|
||||||
filters:
|
filters:
|
||||||
- or:
|
- or:
|
||||||
- heartbeat: ${report_interval}
|
- heartbeat: ${report_interval}
|
||||||
- delta: 0.0007
|
- delta: 0.0007
|
||||||
altitude:
|
- platform: template
|
||||||
id: altitude
|
id: altitude
|
||||||
name: Altitude
|
name: Altitude
|
||||||
internal: true
|
internal: true
|
||||||
speed:
|
update_interval: never
|
||||||
|
- platform: template
|
||||||
id: speed
|
id: speed
|
||||||
name: Speed
|
name: Speed
|
||||||
internal: true
|
internal: true
|
||||||
course:
|
update_interval: never
|
||||||
|
- platform: template
|
||||||
id: course
|
id: course
|
||||||
name: Course
|
name: Course
|
||||||
internal: true
|
internal: true
|
||||||
accuracy:
|
update_interval: never
|
||||||
|
- platform: template
|
||||||
id: gnss_accuracy
|
id: gnss_accuracy
|
||||||
name: GNSS Accuracy
|
name: GNSS Accuracy
|
||||||
internal: true
|
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) {
|
||||||
|
if (id(modem_operator).state != op)
|
||||||
id(modem_operator).publish_state(op);
|
id(modem_operator).publish_state(op);
|
||||||
|
return;
|
||||||
font:
|
}
|
||||||
- file: "gfonts://Roboto"
|
// +CGNSSINFO: <mode>,<satellite counts>,<ddmm.mmmmmm>,<N/S>,<dddmm.mmmmmm>,<E/W>,<date>,<UTC time>,
|
||||||
id: font_small
|
// <alt m>,<speed kn>,<course>,<PDOP>,<HDOP>,<VDOP>
|
||||||
size: 12
|
// The number of satellite counts differs between firmwares, so the fields are found from N/S.
|
||||||
|
// Without a fix all fields are empty
|
||||||
display:
|
if (x.rfind("+CGNSSINFO:", 0) != 0)
|
||||||
- platform: ssd1306_i2c
|
return;
|
||||||
model: "SSD1306 128x64"
|
std::vector<std::string> f;
|
||||||
address: 0x3C
|
for (size_t i = x.find(':') + 1, j; ; i = j + 1) {
|
||||||
i2c_id: system_i2c
|
j = x.find(',', i);
|
||||||
update_interval: 2s
|
f.push_back(x.substr(i, j == std::string::npos ? j : j - i));
|
||||||
lambda: |-
|
if (j == std::string::npos)
|
||||||
it.printf(0, 0, id(font_small), "%s %s", id(modem_network).has_state() ? id(modem_network).state.c_str() : "--",
|
break;
|
||||||
id(modem_operator).state.c_str());
|
}
|
||||||
if (id(modem_rssi).has_state() && !std::isnan(id(modem_rssi).state))
|
size_t ns = 1;
|
||||||
it.printf(127, 0, id(font_small), TextAlign::TOP_RIGHT, "%.0f dBm", id(modem_rssi).state);
|
while (ns < f.size() && f[ns] != "N" && f[ns] != "S")
|
||||||
it.printf(0, 16, id(font_small), "PPP: %s", id(atmodem).is_connected() ? "OK" : "--");
|
ns++;
|
||||||
it.printf(127, 16, id(font_small), TextAlign::TOP_RIGHT, "MQTT: %s", id(mqtt_client)->is_connected() ? "OK" : "--");
|
if (ns + 10 > f.size()) {
|
||||||
if (std::isnan(id(latitude).state))
|
id(latitude).publish_state(NAN);
|
||||||
it.printf(0, 32, id(font_small), "GNSS: no fix");
|
id(longitude).publish_state(NAN);
|
||||||
else
|
id(speed).publish_state(NAN);
|
||||||
it.printf(0, 32, id(font_small), "%.5f %.5f", id(latitude).state, id(longitude).state);
|
return;
|
||||||
auto now = id(sntp_time).now();
|
}
|
||||||
if (now.is_valid())
|
auto num = [&](size_t i) { return f[i].empty() ? NAN : strtof(f[i].c_str(), nullptr); };
|
||||||
it.strftime(0, 48, id(font_small), "%H:%M", now);
|
auto deg = [](double v) { double d = floor(v / 100); return d + (v - d * 100) / 60; };
|
||||||
else
|
double lat = deg(strtod(f[ns - 1].c_str(), nullptr)), lon = deg(strtod(f[ns + 1].c_str(), nullptr));
|
||||||
it.printf(0, 48, id(font_small), "Up %.2f d", id(uptime_sensor).state);
|
id(altitude).publish_state(num(ns + 5));
|
||||||
// The unrounded value; HA only gets 10 % steps
|
id(speed).publish_state(num(ns + 6) * 1.852f);
|
||||||
float bat = std::isnan(id(battery_soc).state) ? id(battery_level).state : id(battery_soc).state;
|
id(course).publish_state(num(ns + 7));
|
||||||
if (!std::isnan(bat))
|
id(gnss_accuracy).publish_state(num(ns + 9) * ${gnss_uere_m});
|
||||||
it.printf(127, 48, id(font_small), TextAlign::TOP_RIGHT, "Bat %.0f%%", bat);
|
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
|
||||||
|
|||||||
Reference in new issue
Block a user