# espos-ble-gateway BLE → SignalK gateway firmware, built on [espOS](https://github.com/dirkwa/espOS). Scans for Bluetooth Low Energy devices and bridges them to [signalk-server](https://github.com/SignalK/signalk-server)'s BLE provider API: advertisements are batched over HTTP, and the server drives GATT sessions (connect, subscribe, read, write) over a control WebSocket. The gateway decodes nothing. What a device *is* — a battery monitor, a tank sender, a thermometer — and how its bytes become SignalK paths is decided on the server by `bt-sensors-plugin-sk`. That keeps the firmware small and means adding support for a new sensor never involves reflashing anything. The application itself is one call: `app_main()` runs `espos_start()`, and espOS brings up logging, config, the web UI, WiFi, SignalK, OTA and — because `espos_ble` is in the build and Bluetooth is enabled — the gateway, in order. ## Hardware | Board | Radio | Flash | Status | |---|---|---|---| | Waveshare ESP32-P4 (+ ESP32-C6 over SDIO) | HCI at the C6 via esp_hosted | 16 MB | **in daily use**: two gateways, 4.3 M and 41 M advertisements forwarded | | Waveshare ESP32-P4 Touch-LCD-7B / -X (same P4 image, on WiFi) | HCI at the C6 via esp_hosted | 16 MB | buildable, not verified on hardware | | ESP32 / S3 / C6 | native Bluedroid | 8 or 16 MB | buildable, not verified on hardware | | Waveshare ESP32-C5-WIFI6-KIT-N16R8 | native Bluedroid, PSRAM in use | 16 MB | **runs**: on the bench, control WebSocket on, internal RAM low-water 15.8 KB; not yet soaked for days or tried with a GATT session | The ESP32-C3 is not released: its dev kits come with 4 MB of flash, and the 2.2 MB image does not fit the two OTA slots of a 4 MB table. **As cloned, this builds a 16 MB image on every target**: `partitions.csv` is a 16 MB table and `sdkconfig.defaults` sets `CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y`. Bluedroid is large — the image measures 2.13 MB on an ESP32-C6 — so two 2.5 MB OTA slots plus the web-UI storage partition come to 5.94 MB, which is why the table is sized the way it is. `esptool.py flash_id` reports the flash size of a board you are unsure about. Releases also carry each ESP32, S3 and C6 image laid out with espOS's 8 and 16 MB tables (3 and 6.5 MB slots), and the P4 image with the 16 MB one, as `…-merged-8mb.bin` and `…-merged-16mb.bin`. They are the same build, with no extra compile, and the update image is the same file. The flasher reads the board's flash and writes the largest layout that fits, so an 8 MB dev kit gets an image that starts, and a 16 or 32 MB board gets the big slots. Boards with 32 MB use the 16 MB layout. The ESP32-C5 builds with its own table, `partitions-esp32c5.csv`: 6 MB OTA slots, because its image already fills 88% of a 2.5 MB slot. A board flashed with an older C5 release keeps the 2.5 MB slots until it is flashed over USB again, since an OTA never rewrites the partition table. A **4 MB** board cannot hold that table at all: it runs past the end of the chip, and the flash fails partway through writing rather than because the board is short of RAM (issue #4). ### Other flash sizes Both paths below are edits to this repository, not options it ships with. Neither has been booted here — there is no 4 MB or 8 MB board on this bench — so both are starting points rather than supported configurations. What was checked is that each builds for `esp32c6` with zero warnings and that the image fits the slot, which is not the same as a device that runs. **8 MB, keeping OTA.** Releases ship this layout (`…-merged-8mb.bin`). To build it yourself, point the prologue at espOS's own 8 MB table (3 MB slots, ample for a 2.13 MB image) and delete the flash-size line, because a bundled `mb.csv` makes the prologue set the size itself: ```cmake espos_project_prologue(NAME "ble-gateway" PARTITIONS "${ESPOS_PARTITIONS_DIR}/8mb.csv" COMPONENTS espos_ble espos_eth) ``` ```diff -CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y ``` **4 MB, without OTA.** One `factory` slot holds the image at 71 % with the UI storage intact. The cost is absolute rather than a degradation: `esp_https_ota` needs a passive slot and fails without one, so **every update becomes a USB reflash**. The table ships as [`partitions-4mb.csv`](partitions-4mb.csv), so this is two edits. The first is the one that is easy to miss: the prologue still names `partitions.csv` otherwise, so the 16 MB table gets selected and the flash fails exactly as in #4. ```cmake espos_project_prologue(NAME "ble-gateway" PARTITIONS "${CMAKE_CURRENT_LIST_DIR}/partitions-4mb.csv" COMPONENTS espos_ble espos_eth) ``` A project's own table sets no flash size, unlike a bundled `mb.csv`, so the second edit states it: ```diff -CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y ``` Verified by building it for `esp32c6`: `0x221000` in a `0x300000` slot, zero warnings. The ESP32-P4 has no radio of its own, so Bluetooth (like WiFi) runs over the C6 co-processor. Nothing needs flashing on the C6 — its stock firmware already carries BLE with HCI over SDIO. **On the P4, check the antenna first.** The ESP32-C6-MINI-**1U** module has no PCB antenna; without an external 2.4 GHz antenna on its IPEX connector the radio sees nothing at all. ## Install The easiest way is the hosted flasher at : open it in Chrome or Edge, connect the board over USB, pick **BLE Gateway** and your board, and it writes the latest release. With the [signalk-espos-manager](https://github.com/signalk-espOS/signalk-espos-manager) plugin installed on your Signal K server, its Flash page does the same from the server's admin UI. To flash by hand, download the chip's `-merged.bin` from the [latest release](https://github.com/dirkwa/espos-ble-gateway/releases/latest) (`ble-gateway---merged.bin`, for example `ble-gateway-esp32c6-v1.1.0-merged.bin`) and write it at offset `0x0`: ```sh esptool.py --chip esp32c6 -p /dev/ttyUSB0 write_flash 0x0 ble-gateway-esp32c6-v1.1.0-merged.bin ``` The `-ota.bin` next to it is for over-the-air updates, not for a cable. ## Setup On first boot the device raises a WiFi provisioning portal (`espOS-xxxx`). Join it and pick a network; from then on everything is at `http:///`. On the Waveshare P4 POE-ETH the cable carries the network and the portal stays down while it has an address. A gateway that also has a stored WiFi network joins it beside the cable, and the station then shares the C6 radio with the BLE scanner: set `wifi.sta_enabled` to false to give the radio to BLE alone. The display boards (Touch-LCD-7B and -X) have no Ethernet and run on WiFi; the screen stays dark, since the gateway does not drive it. Credentials live in NVS, never in the firmware image. To provision without the portal, see [espos/docs/wifi.md](espos/docs/wifi.md) — put them in a CSV **outside the repo**, generate an NVS partition and flash it to `0x9000`. The gateway finds a SignalK server over mDNS and requests access as `ble_gateway `; approve it in the server's admin UI under Security → Access Requests. On a network with several servers, pin one with `sk.server_host` and `sk.server_pin`. Settings live under the `ble` namespace in the web UI. Defaults are sensible; `active_scan` is off deliberately (see [espos/docs/ble.md](espos/docs/ble.md)). ## Updates Once a gateway is on the network, updates come over the air: the signalk-espos-manager plugin lists it, shows when a newer release is out and installs it. The gateway accepts only images signed with this project's key, so a board that ran another project's firmware (a different signing key) needs one USB flash with this firmware first; after that, OTA works. A change to the partition table, such as the C5's larger slots, reaches only boards flashed over USB, because an OTA never rewrites it. ## Status `GET /api/v1/ble/status` reports scan, buffer and POST counters — enough to tell a BLE problem from a server problem at a glance. Full description in [espos/docs/rest-api.md](espos/docs/rest-api.md), and the component guide with the troubleshooting notes is [espos/docs/ble.md](espos/docs/ble.md). The device keeps its recent log at `http:///api/v1/logs`, which is the first thing to look at when something misbehaves. Report problems in this repository's [issues](https://github.com/dirkwa/espos-ble-gateway/issues), with that log attached. ## Build Needs ESP-IDF exactly `v6.1.1` (pinned in `.idf-version`, kept equal to the espOS submodule's pin — the build refuses a different one) and nothing else: the espOS web UI is a committed bundle, so there is no Node step. ```sh git clone --recursive https://github.com/dirkwa/espos-ble-gateway cd espos-ble-gateway . ~/esp-idf-v6.1.1/export.sh # esp32p4 here; esp32 / esp32s3 / esp32c6 the same way. # esp32c5 too, but only with espos at espOS 0.18.0 or newer: the build refuses # an older submodule (CMakeLists.txt), because the C5 needs espOS#207. espos/scripts/build.sh -B build-esp32p4 -DSDKCONFIG=build-esp32p4/sdkconfig -DIDF_TARGET=esp32p4 build idf.py -B build-esp32p4 -p /dev/ttyACM0 flash monitor ``` `espos/scripts/build.sh` is espOS's build wrapper — one build at a time per machine, half the cores, `nice`/`ionice`, scratch kept off tmpfs; on a small host that is the difference between a build and a frozen session. `scripts/build.sh` forwards to it. Plain `idf.py set-target esp32p4 && idf.py build` is the same build without the babysitting. The sdkconfig is assembled by espOS (`espos/docs/development.md`): its own defaults first, then this project's `sdkconfig.defaults` (Bluetooth and the little the gateway does differently), then a git-ignored `sdkconfig.local` for personal overrides, then the partition table. Defaults reach a fresh sdkconfig only — delete `build*/sdkconfig` after changing them. ## Releases `release-please` watches `main`, opens a `chore: release ` pull request, and merging that tags the release and triggers `release-firmware.yml`, which builds every supported chip and attaches both images per target (merged for a cable, app-only for an OTA). Two repository settings have to be right, and neither fails in a way that tells you so: * **Settings → Actions → General → "Allow GitHub Actions to create and approve pull requests"** must be enabled, and workflow permissions set to read/write. Without it release-please cannot open its release PR and the run fails with *"This run likely failed because of a workflow file issue"* and **zero jobs executed** — no job log, and nothing wrong with the workflow file. This cost three wrong diagnoses; `actionlint` passing while the run still fails is the signal that it is a permission, not the YAML. * **`GATEWAY_SIGNING_KEY_PEM`** must hold the app-signing key: ```sh gh secret set GATEWAY_SIGNING_KEY_PEM --repo / < signing_key.pem ``` A release built without it uses a per-run throwaway key: it installs over USB and then refuses every future OTA, on the boat, months later. The workflow fails closed rather than let that happen. ## Layout ``` espos/ espOS submodule: WiFi, config, web UI, SignalK, OTA, health, and components/espos_ble - where the gateway actually lives main/ app_main() = espos_start(); CMakeLists.txt names the components that make this build a gateway (deliberately thin) sdkconfig.defaults* only what differs from espOS: Bluetooth, PSRAM on the P4 partitions.csv needs 16 MB: 2.5 MB OTA slots, LittleFS storage for the UI partitions-esp32c5.csv the C5's: 6 MB OTA slots, same offsets up to ota_0 scripts/build.sh forwards to espos/scripts/build.sh ``` Anything that improves the gateway belongs in espOS, not here. ## History This replaces [sensesp-ble-gateway](https://github.com/dirkwa/sensesp-ble-gateway) (archived), which did the same job as a SensESP/Arduino library. The protocol is unchanged, so a server configured for that gateway works with this one. The rewrite exists mainly to get off Arduino, and it fixed several things on the way: GATT writes now honour `with_response` (JK-BMS and Daly-BMS reject write-with-response on their command characteristic), GATT operations act on the connection they were given rather than the first one matching a UUID, and advertisement drops are counted honestly. Not carried over: that gateway's **NimBLE** backend, which was scan-only. The archived repository remains the only place it exists. ### The ESP32-C5 needs its PSRAM **Without PSRAM** the BLE controller never starts: ``` W BLE_INIT: r_ble_controller_init failed 257 E espos_ble_backend: bt_controller_init: ESP_ERR_NO_MEM -- internal heap 30844 B free, largest block 15360 B; the controller needs ~24 KB CONTIGUOUS ``` WiFi and the Signal K client keep working, so the device is a WiFi/Signal K node with no BLE. That is the failure [espOS#127](https://github.com/signalk-espOS/espOS/issues/127) is named after, and what releases up to this one shipped no C5 image for. **With PSRAM in use it runs.** `sdkconfig.defaults.esp32c5` turns on the module's 8 MB and moves into it the Bluedroid host heap, every allocation larger than 256 bytes (`CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=256`; the default keeps anything under 16 KB internal) and TLS buffers. It also cuts the WiFi driver's static RX buffers from 10 to 6. espOS (from [#207](https://github.com/signalk-espOS/espOS/pull/207)) stops scanning until the station has an address: at a 50 % scan duty cycle, every association failed with reason 36. Measured on a Waveshare ESP32-C5-WIFI6-KIT-N16R8 with this repository's release configuration: the controller starts, the control WebSocket connects, and internal RAM sits at 22 KB free with a 15.8 KB low-water mark (the alarm is at 12 KB). The measurements and the rev v1.0 caveat (IDF does not encrypt PSRAM on that revision, and TLS buffers live there) are in [hardware.md](https://github.com/signalk-espOS/espOS/blob/main/docs/hardware.md#the-esp32-c5-as-a-ble-gateway). An earlier version of this section said the C5 "does not work". That was measured with PSRAM off for everything but what IDF moves by default, which it no longer is. ## License Apache-2.0 — see [LICENSE](LICENSE). Relicensed from the previous source-available terms in 2026 so that this firmware could be folded into espOS, which is Apache-2.0. ## This firmware now also lives in espOS The same gateway is an espOS example, at [`components/espos_ble/examples/ble_gateway`](https://github.com/signalk-espOS/espOS/tree/main/components/espos_ble/examples/ble_gateway). It is built by espOS's own CI on every change, which this repository is not, so the example is the copy that cannot drift out of step with the component it configures. Prefer it unless you want this repository's release history or its own partition table.