Skip to content

fix: NTRIP v2 compatibility + flush() for reliable ESP32 connection#5

Open
yasunorioi wants to merge 1 commit into
GLAY-AK2:masterfrom
yasunorioi:fix/ntrip-v2-compatibility
Open

fix: NTRIP v2 compatibility + flush() for reliable ESP32 connection#5
yasunorioi wants to merge 1 commit into
GLAY-AK2:masterfrom
yasunorioi:fix/ntrip-v2-compatibility

Conversation

@yasunorioi
Copy link
Copy Markdown

@yasunorioi yasunorioi commented Apr 14, 2026

Summary

  • Add flush() after print() to ensure HTTP request is fully sent on ESP32 (lwIP stack does not guarantee immediate TCP send without flush)
  • Accept NTRIP v2 responses (HTTP/1.1 200 OK, HTTP/1.0 200 OK) in addition to v1 (ICY 200 OK)
  • Skip HTTP response headers when connecting to v2 casters
  • Harden readLine() with per-byte timeout and buffer overflow guard

  • ESP32でprint()後にflush()を追加し、HTTPリクエストの送信を確実にしました(lwIPスタックはflush()なしでは即時TCP送信を保証しません)
  • NTRIP v1(ICY 200 OK)に加え、v2レスポンス(HTTP/1.1 200 OKHTTP/1.0 200 OK)に対応しました
  • v2キャスター接続時にHTTPレスポンスヘッダーを読み飛ばす処理を追加しました
  • readLine()にバイト単位のタイムアウトとバッファオーバーフローガードを追加しま
    した

Problem

Intermittent timeout when connecting to BKG-style NTRIP casters. The root cause is twofold:

  1. Missing flush(): On ESP32, WiFiClient::print() buffers data internally. Without flush(), the TCP segment may not be sent immediately, causing casters to time out waiting for the complete request.

  2. v1-only response parsing: Many modern NTRIP casters (especially BKG-based) respond with HTTP/1.1 200 OK (NTRIP v2 protocol), which was not recognized.


BKG系NTRIPキャスターへの接続時に、断続的にタイムアウトが発生する問題です。原因は2つあります:

  1. flush()の欠落: ESP32のWiFiClient::print()は内部バッファにデータを保持
    します。flush()なしではTCPセグメントが即座に送信されず、キャスターがリクエスト待ちでタイムアウトすることがあります。

  2. v1レスポンスのみ対応: 多くの現代的なNTRIPキャスター(特にBKG系)はHTTP/1.1 200 OK(NTRIP v2プロトコル)で応答しますが、従来のコードでは認識できませんでした。


  • reqSrcTbl()strncmp()戻り値チェックが反転していた問題を修正(成功時にfalseを返していた)
  • 接続失敗時にstop()を呼びソケットリソースを解放するよう修正
  • reqRaw()のタイムアウトを20秒から10秒に短縮し、障害検出を高速化

Additional fixes

  • Fix inverted strncmp() return value check in reqSrcTbl() (was returning false on success)
  • Add stop() on failed connection to clean up socket resources
  • Reduce reqRaw() timeout from 20s to 10s for faster failure detection

Tested against

  • rtk.toiso.fit:2101 (NTRIP v1 caster, responds with ICY 200 OK)
  • BKG-style casters (NTRIP v2, responds with HTTP/1.1 200 OK)

Changes

Only src/NTRIPClient.cpp is modified. No changes to the header file or examples.

変更はsrc/NTRIPClient.cppのみです。ヘッダーファイルやexamplesへの変更はありません。

Problem:
- Intermittent timeout when connecting to BKG-style NTRIP casters
- ESP32 WiFiClient::print() does not guarantee immediate TCP send
  without flush(), causing some casters to time out waiting for
  the complete request
- Only NTRIP v1 response (ICY 200 OK) was accepted, but many
  modern casters respond with HTTP/1.1 200 OK (NTRIP v2)

Changes:
- Add flush() after print() in reqSrcTbl() and reqRaw() to ensure
  the HTTP request is fully transmitted before waiting for response
- Upgrade request to HTTP/1.1 with Host header and Ntrip-Version: 2.0
- Accept both v1 (ICY 200 OK) and v2 (HTTP/1.1 200, HTTP/1.0 200)
  responses
- Skip HTTP response headers when connecting to v2 casters
- Fix inverted strncmp() logic in reqSrcTbl() (was returning
  false on success)
- Add stop() on connection failure to clean up socket resources
- Harden readLine() with per-byte timeout and buffer overflow guard
- Reduce reqRaw() timeout from 20s to 10s for faster failure detection

Tested against:
- rtk.toiso.fit:2101 (NTRIP v1, ICY 200 OK)
- BKG-style casters (NTRIP v2, HTTP/1.1 200 OK)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant