richlegrand/bitbang

0.1.3

Latest
uploaded 6 hours ago
bitBang: reach this device from any browser, by a URL

Readme

# bitBang for ESP-IDF

Reach an ESP32 from any browser, anywhere, by a URL. The device registers
with the bitBang signaling server and talks to the browser over WebRTC data
channels, so it needs no port forwarding, no cloud account and no app.

What a device gets:

- **Its own web pages**, served by an ordinary `esp_http_server` in your
  firmware, as if the browser were on the same network.
- **A settings page**: declare a table of values in C and the browser renders
  them, with persistence in NVS.
- **A console**: the device's log in the browser, with input.
- **Firmware updates** from the browser, with rollback (optional).
- **Live media streams**, such as a camera's video (optional).

This component pulls in the rest: `bitbang_signaling`, `bitbang_httpd`,
`bitbang_settings`, `bitbang_console`, `bitbang_ota`, `bitbang_stream`, and
our build of the KVS WebRTC SDK's ESP-IDF port.

## Requirements

- ESP-IDF v5.5 or later.
- An ESP32 or ESP32-S3 **with PSRAM**: an ESP32 WROVER module or the
  ESP32-CAM; an ESP32-S3 with octal PSRAM (N8R8 modules, the ESP32-S3-EYE,
  the XIAO ESP32S3 Sense). Boards without PSRAM can't run it.
- 4 MB of flash or more. A bitBang image is 1.7 to 1.9 MB, larger than
  ESP-IDF's default partition tables allow.

## Adding it to a project

    idf.py add-dependency "richlegrand/bitbang^0.1.1"

Then, in your main component's `CMakeLists.txt`:

```cmake
idf_component_register(
    SRCS "main.c"
    INCLUDE_DIRS "."
    REQUIRES bitbang esp_http_server
)
```

### sdkconfig.defaults

bitBang needs these settings. Put them in your project's
`sdkconfig.defaults`, and delete any `sdkconfig` already generated so they
are read:

```
# Required
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0
CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
CONFIG_ESP_TLS_INSECURE=n
CONFIG_ESP_TLS_USING_MBEDTLS=y
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
CONFIG_MBEDTLS_SSL_DTLS_SRTP=y
CONFIG_MBEDTLS_SSL_ALPN=y
CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC=y
CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y
CONFIG_ENABLE_DATA_CHANNEL=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_KVS_MAX_CONCURRENT_STREAMS=4
CONFIG_LWIP_MAX_SOCKETS=24

# The board: flash size and the partition table below
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
```

For an ESP32-S3 with octal PSRAM, add `CONFIG_SPIRAM_MODE_OCT=y`.

A build missing one of the settings bitBang can't work without stops with an
`#error` naming it.

These are only what's required. This component also ships
`sdkconfig.bitbang` (and `sdkconfig.bitbang.esp32s3`), which adds the tuning
bitBang was measured with: 240 MHz, `-O2`, a 1 ms tick, hardware AES-GCM and
others, each explained and measured in the file. They arrive with the
component, so the order matters: ESP-IDF stops before downloading anything if
`SDKCONFIG_DEFAULTS` names a file that isn't there yet.

1. Run `idf.py reconfigure` once, with your `CMakeLists.txt` not yet naming
   them. That downloads the components into `managed_components/`.
2. Copy both files into your project:

       cp managed_components/richlegrand__bitbang/sdkconfig.bitbang* .

3. List them first in your top-level `CMakeLists.txt`, before `project.cmake`:

   ```cmake
   set(SDKCONFIG_DEFAULTS "sdkconfig.bitbang" "sdkconfig.defaults")
   include($ENV{IDF_PATH}/tools/cmake/project.cmake)
   ```

4. Delete the generated `sdkconfig`, so the defaults are read again, and
   build.

### partitions.csv

Two app slots on 4 MB of flash, so the device can also be updated from the
browser later. Save it as `partitions.csv` beside your top-level
`CMakeLists.txt`:

```
# Name,     Type, SubType, Offset,   Size,     Flags
nvs,        data, nvs,     0x9000,   0x15000,
otadata,    data, ota,     0x1e000,  0x2000,
ota_0,      app,  ota_0,   0x20000,  0x1f0000,
ota_1,      app,  ota_1,   0x210000, 0x1f0000,
```

`partitions/ota_8mb.csv` in this component is the same for 8 MB of flash,
with 3 MB slots.

## A first device

The smallest useful one: a page of its own at the device's URL.

```c
#include "esp_http_server.h"
#include "bitbang.h"

static esp_err_t root_get(httpd_req_t *req)
{
    httpd_resp_set_type(req, "text/html");
    return httpd_resp_sendstr(req, "<h1>Hello from an ESP32</h1>"
                                   "<p><a href=\"/*settings\">Settings</a>");
}

void app_main(void)
{
    bitbang_config_t cfg = BITBANG_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(bitbang_init(&cfg));        /* NVS, identity, Wi-Fi */

    httpd_config_t hcfg = HTTPD_DEFAULT_CONFIG();
    httpd_handle_t server = NULL;
    ESP_ERROR_CHECK(httpd_start(&server, &hcfg));
    httpd_uri_t root = { .uri = "/", .method = HTTP_GET, .handler = root_get };
    ESP_ERROR_CHECK(httpd_register_uri_handler(server, &root));

    ESP_ERROR_CHECK(bitbang_start(server));     /* connect and serve */
}
```

Requests from the browser go straight into your server's handlers, with no
socket in between. Between `bitbang_init` and `bitbang_start` is where a
program registers its settings tables and streams.

## Wi-Fi

The first time, the device has no network to join, so it opens an access
point named `bitbang-` and four characters of its ID. Join it from a phone
and a page asks for your network. The network that connects is stored.

For a build that connects without that step, set the default network in
menuconfig (bitBang > Wi-Fi), or in a `sdkconfig.defaults` you keep out of
version control:

```
CONFIG_BITBANG_WIFI_SSID="your network"
CONFIG_BITBANG_WIFI_PASSWORD="its password"
```

## The URL

Once connected, the device prints its URL to the console in a box, and after
each boot a QR code of it, for a phone to scan. The URL holds the device's access
code; anyone who has it can open the device, so treat it like a password.
For a second factor, set a PIN (bitBang > PIN in menuconfig).

The device's identity, an RSA key generated on first boot, lives in NVS.
Erasing NVS gives the device a new URL.

## Updates from the browser

Off by default. With the partition table above, add:

```
CONFIG_BITBANG_OTA=y
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
CONFIG_APP_ROLLBACK_ENABLE=y
```

A new image boots on probation, and deciding when to accept it is the app's
job: mark it valid too early and a broken image is a device someone has to
re-flash over USB. `docs/ota.md` (Probation and acceptance) has the approach
the camera example takes: accept on the first registration, and restart into
the previous image if that hasn't happened in 10 minutes.

## Documentation

The guides ship in this component's `docs/` folder:

- `docs/api.md`: `bitbang_init` and `bitbang_start`, the config struct,
  serving pages, Wi-Fi and provisioning, the URL and PIN, menuconfig
- `docs/settings.md`: settings tables, persistence, the settings page
- `docs/console.md`: the device console and its page
- `docs/ota.md`: firmware updates, partitions, rollback and probation
- `docs/streams.md`: media streams, with a camera as the worked example

They were written for building from the source tree, which adds the
components with `bitbang.cmake`. From the registry, the steps above replace
that part; the rest applies as written.

## License

MIT, for the bitBang components. The KVS WebRTC port's components are
Apache-2.0 (Amazon and Espressif), except `esp_usrsctp` (BSD-3-Clause),
`libsrtp2` (Apache-2.0 and BSD-3-Clause) and `libwebsockets` (MIT); each
carries its own license file.

Links

To add this component to your project, run:

idf.py add-dependency "richlegrand/bitbang^0.1.3"

download archive

Stats

  • Archive size
    Archive size ~ 134.66 KB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

richlegrand/bitbang version: 0.1.3
|