diff --git a/README.ko.md b/README.ko.md index 28663ee..02026f1 100644 --- a/README.ko.md +++ b/README.ko.md @@ -288,6 +288,7 @@ BLE ELM327 어댑터에 연결하여 OBD-II(및 GM mode 22) PID 명령을 전송 | `rx_char_uuid` | string | `"FFF1"` | 알림 캐릭터리스틱 (응답 수신). | | `tx_delay` | duration | `50ms` | 연속 명령 사이 딜레이. | | `init_commands` | list | `[]` | 폴링 시작 전 연결마다 한 번 전송하는 초기 명령. | +| `default_commands` | list | 자동 | `pre_commands`가 없는 센서를 폴링하기 직전에 보낼 명령. 보통 생략하면 됨 — 아래 "헤더 복원" 참고. | | `auto_connect` | bool | `true` | 연결 끊김 시 자동 재연결. | **OBD 센서 전용 필드:** @@ -297,10 +298,34 @@ BLE ELM327 어댑터에 연결하여 OBD-II(및 GM mode 22) PID 명령을 전송 | `preset` | string | 내장 프리셋 사용 (아래 표 참조). | | `mode` | string | OBD 모드. `"01"`=표준, `"22"`=제조사 확장. 기본 `"01"`. | | `pid` | string | PID hex 코드. 예: `"0C"`, `"199A"`. | -| `formula` | string | 수식. `a`,`b`,`c`,`d`는 응답 바이트. 예: `"(a*256+b)/4"`. | +| `formula` | string | 수식. `a`~`t`가 응답의 0~19번째 바이트. 예: `"(a*256+b)/4"`. 응답이 짧아 식에 쓴 바이트가 없으면 그 주기는 발행하지 않음. | | `update_interval` | duration | 이 PID를 폴링하는 간격. 기본 `60s`. | | `pre_commands` | list | 이 PID 전에 전송할 추가 명령 (예: GM 헤더 전환). | +### 헤더 복원 (ATSH) + +ELM327의 전송 헤더는 **한 번 바꾸면 계속 유지**됩니다. 그래서 `pre_commands: ["ATSH7C6"]` 같은 +센서 뒤에 `pre_commands`가 없는 표준 센서(rpm, speed 등)가 폴링되면, 그 요청까지 7C6으로 나가 +`NO DATA`가 됩니다. + +앱이 이를 자동으로 처리합니다. **한 센서라도 `pre_commands`에 `ATSH`를 쓰면**, `pre_commands`가 +없는 센서 앞에 헤더 복원 명령을 자동으로 넣습니다. 복원할 헤더는 이 순서로 정해집니다: + +1. `default_commands` — 명시했다면 이 값 +2. `init_commands`의 마지막 `ATSH…` +3. `ATSH7DF` — ELM327의 11bit CAN 기본 헤더 + +`ATSH`를 쓰는 센서가 하나도 없으면 아무것도 보내지 않습니다. K-line(ISO 9141 / KWP)처럼 헤더 +형식이 다른 프로토콜에 `ATSH7DF`를 밀어넣지 않기 위한 것입니다. + +즉 **보통은 `default_commands`를 생략**하면 되고, 복원 헤더를 직접 지정해야 할 때만 씁니다: + +```yaml +obd: + init_commands: ["ATSP6"] + default_commands: ["ATSH7E0"] # 브로드캐스트(7DF) 대신 엔진 ECU 직접 지정 +``` + --- ## sensors — 센서 설정 diff --git a/README.md b/README.md index 6384ffc..53e1844 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ Connects to an ELM327 BLE adapter, sends OBD-II (and GM mode 22) PID commands, a | `rx_char_uuid` | string | `"FFF1"` | Notify characteristic (receive responses). | | `tx_delay` | duration | `50ms` | Delay between consecutive commands. | | `init_commands` | list | `[]` | Commands sent once on each connection before polling starts. | +| `default_commands` | list | auto | Commands sent before polling any sensor that has no `pre_commands`. Usually omit it — see "Header restore" below. | | `auto_connect` | bool | `true` | Automatically reconnect on disconnect. | **OBD sensor-specific fields:** @@ -297,10 +298,35 @@ Connects to an ELM327 BLE adapter, sends OBD-II (and GM mode 22) PID commands, a | `preset` | string | Use a built-in preset (see table below). | | `mode` | string | OBD mode. `"01"` = standard, `"22"` = manufacturer specific. Default `"01"`. | | `pid` | string | PID hex code (without mode prefix). E.g. `"0C"`, `"199A"`. | -| `formula` | string | Math expression. Variables `a`,`b`,`c`,`d` are response bytes. E.g. `"(a*256+b)/4"`. | +| `formula` | string | Math expression. Variables `a`–`t` are response bytes 0–19. E.g. `"(a*256+b)/4"`. If the response is too short to bind a byte used in the expression, nothing is published for that poll. | | `update_interval` | duration | How often to poll this PID. Default `60s`. | | `pre_commands` | list | Extra AT/OBD commands to send before this PID (e.g., header switch for GM). | +### Header restore (ATSH) + +An ELM327 transmit header **stays in effect once set**. So a sensor with +`pre_commands: ["ATSH7C6"]` leaves the header pointing at the cluster, and the next standard +sensor (rpm, speed, …) that has no `pre_commands` gets sent there too — returning `NO DATA`. + +The app handles this automatically. **If any sensor uses `ATSH` in `pre_commands`**, a restore +command is inserted before every sensor that has no `pre_commands`. The header to restore is +chosen in this order: + +1. `default_commands`, if you set it +2. the last `ATSH…` in `init_commands` +3. `ATSH7DF` — the ELM327 default header for 11-bit CAN + +If no sensor uses `ATSH`, nothing is sent. This avoids pushing `ATSH7DF` onto protocols with a +different header format, such as K-line (ISO 9141 / KWP). + +So you can normally **omit `default_commands`**; set it only to pin a specific restore header: + +```yaml +obd: + init_commands: ["ATSP6"] + default_commands: ["ATSH7E0"] # address the engine ECU instead of broadcasting to 7DF +``` + --- ## sensors — Sensor Config diff --git a/templates.yaml b/templates.yaml index b4b014b..d3e1550 100644 --- a/templates.yaml +++ b/templates.yaml @@ -112,8 +112,8 @@ templates: - { key: trans_temp, preset: gm_trans_temp, update_interval: 10s } - id: obd_hyundai_kia - name: "OBD-II + 현대/기아 확장" - description: "RPM, 속도, 연료잔량%, 총 주행거리 (표준 & 현대/기아 PID)" + name: "OBD-II + 현대/기아 계기판" + description: "RPM, 속도, 총 주행거리, 연료잔량(L), 계기판 전압 — 계기판 7C6 / 22B002" device: id: car_obd_hk name: "Hyundai/Kia OBD" @@ -124,55 +124,51 @@ templates: rx_char_uuid: "2AF0" tx_delay: 50ms init_commands: ["ATSP6"] + # default_commands 생략 — 아래 ATSH7C6 센서가 있으므로 앱이 ATSH7DF로 자동 복원 sensors: - { key: rpm, preset: rpm, update_interval: 1s } - { key: speed, preset: speed, update_interval: 1s } - # 연료 잔량 (%) - - key: fuel_level - preset: fuel_level - update_interval: 1s - - key: fuel_level_ext - mode: "22" - pid: "002F" - formula: "a/2.55" - unit: "%" - state_class: measurement - update_interval: 1s - - key: fuel_level_303e - mode: "22" - pid: "303E" - formula: "a-100" - unit: "%" - state_class: measurement - update_interval: 1s - # 총 주행거리 (km) + # 계기판(CLU) UDS. 한 응답에 주행거리·연료·전압이 함께 담겨 온다. + # 응답: 62 B0 02 [a][b][c][d][e][f][g][h][i]… (a = 62B002 에코 다음 바이트) + # e → 연료잔량 e*0.5 L + # f → 계기판 전압 f*0.078125 V + # g:h:i → 총 주행거리 24bit km + # 2013년식 이후 현대·기아·제네시스에서 널리 동작. 미지원 차는 NO DATA. + # 일부 기아는 주행거리가 i:j:k:l(32bit)로 밀려 있다는 보고가 있음. + # 값이 이상하면 formula를 "i*16777216+j*65536+k*256+l" 로 바꿔볼 것. - key: odometer - preset: odometer - update_interval: 1s - - key: odometer_ed03 mode: "22" - pid: "ED03" - formula: "(((a*65536)+b*256)+c)*0.256" + pid: "B002" + pre_commands: ["ATSH7C6"] + formula: "g*65536+h*256+i" unit: "km" device_class: distance state_class: total_increasing - update_interval: 1s - - key: odometer_f22e + accuracy_decimals: 0 + icon: mdi:counter + update_interval: 60s + - key: fuel_level mode: "22" - pid: "F22E" - formula: "(((a*256*256*256)+b*256*256)+c*256)+d" - unit: "km" - device_class: distance - state_class: total_increasing - update_interval: 1s - - key: odometer_2103 - mode: "21" - pid: "03" - formula: "(((a*65536)+b*256)+c)*0.256" - unit: "km" - device_class: distance - state_class: total_increasing - update_interval: 1s + pid: "B002" + pre_commands: ["ATSH7C6"] + formula: "e*0.5" + unit: "L" + state_class: measurement + accuracy_decimals: 1 + icon: mdi:gas-station + update_interval: 30s + publish: { deadband: 0.5, min_interval: 60s } + - key: cluster_voltage + mode: "22" + pid: "B002" + pre_commands: ["ATSH7C6"] + formula: "f*0.078125" + unit: "V" + device_class: voltage + state_class: measurement + accuracy_decimals: 2 + entity_category: diagnostic + update_interval: 60s - id: gatt_meter_relay name: "GATT power meter + relay"