# Add Pinscope to an ESP-IDF project

Pinscope gives the device's web server a secure address, sends its logs, metrics and crash reports to the Pinscope dashboard, and updates its firmware from there. You are adding it to an existing ESP-IDF 5.x or 6.x project for an ESP32 (ESP32, S2, S3, C2, C3, C5, C6 or C61). Full docs: https://pinscope.dev/docs

## First, look at the project

- Find `app_main()` and where networking starts: `esp_netif_init()` and `esp_event_loop_create_default()`, then WiFi or Ethernet.
- Note whether it runs an HTTP server (`httpd_start`) and on which port.
- Note the partition table (`CONFIG_PARTITION_TABLE_*`, `partitions.csv`).

## Steps

1. Add the component, from the project folder:
   ```
   idf.py add-dependency "pinscope/pinscope^0.2.15"
   ```
2. The project key. The user copies it from the dashboard: their project, tab **Connect a device**. It goes into `CONFIG_PINSCOPE_PROJECT_KEY`: tell the user to set it with `idf.py menuconfig` → Pinscope, which writes it to `sdkconfig`. Check whether `sdkconfig` is committed in this repository (`git ls-files sdkconfig`, `.gitignore`): if it is, the key would be committed with it, so tell the user and suggest keeping settings in `sdkconfig.defaults` and adding `sdkconfig` to `.gitignore`. Never put the key in `sdkconfig.defaults` if that file is committed. The build stops with a clear message if the key is missing or malformed.
3. Start it once, after `esp_netif_init()` and `esp_event_loop_create_default()`. It does not need the network to be up yet and reconnects by itself:
   ```c
   #include "pinscope.h"
   ...
   pinscope_start(NULL);
   ```
4. If the HTTP server is not on port 80, set `CONFIG_PINSCOPE_LOCAL_PORT` to its port (menuconfig → Pinscope), or start with a config: `pinscope_config_t cfg = { .ports = { 8080 } }; pinscope_start(&cfg);`.
5. Firmware updates need two OTA app slots. If the partition table has only a factory app, switch to one with `ota_0` and `ota_1` (for 4 MB of flash):
   ```
   # Name,   Type, SubType, Offset,   Size
   nvs,      data, nvs,     0x9000,   0x6000
   otadata,  data, ota,     0xf000,   0x2000
   phy_init, data, phy,     0x11000,  0x1000
   ota_0,    app,  ota_0,   0x20000,  0x1E0000
   ota_1,    app,  ota_1,   0x200000, 0x1E0000
   ```
   select it, and switch on rollback so an update that cannot reach Pinscope is undone, in `sdkconfig.defaults`:
   ```
   CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
   CONFIG_PARTITION_TABLE_CUSTOM=y
   CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
   CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
   ```
   With 8 MB or more of flash, keep the larger flash size setting; the table fits any size from 4 MB up. Ask before changing the partition table of a product already in the field: devices flashed with the old table need a cable flash to change it.
6. Nothing else is needed for logs (ESP_LOG goes to the dashboard), metrics or crash reports. For decoded crashes, the user uploads `build/<project>.elf` under Firmware.

7. Optional, for the EU Cyber Resilience Act: if the user wants vulnerability tracking, an SBOM of each build comes from `pip install esp-idf-sbom` and `idf.py sbom-create` (writes `build/<project>.spdx`); the user attaches it to the uploaded firmware in the dashboard's Security tab, or from CI with `ci.sh build/<project>.bin --sbom build/<project>.spdx`. Do not add it to the build unasked.

## Check

- `idf.py build` succeeds.
- After flashing, the serial log shows `pinscope: online at https://...` and the device appears in the dashboard under Devices.

## Rules

- Never invent a project key and never write the user's key into a file that is committed. Ask the user for it, and keep it where the steps below say. A key is `psk_` followed by 24 letters, digits, `-` or `_`, 28 characters in all; if the one you are given is not, ask again.
- Change as little as you can: add Pinscope, do not restructure the project, rename files or touch unrelated code.
- Do not add a TLS library, certificates or port forwarding. Pinscope makes one outbound connection to `connect.pinscope.dev` on TCP port 443 and encrypts it itself.
- Do not edit Pinscope's own source; configure it.
- When you are done, tell the user exactly what you changed and what they still have to do (enter the key, flash, upload the firmware for updates).
