# Add Pinscope to an Arduino or PlatformIO project (ESP32)

Pinscope gives the ESP32'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 sketch for an ESP32 (Arduino-ESP32 2.x or 3.x), in the Arduino IDE or PlatformIO. Full docs: https://pinscope.dev/docs#arduino

## First, look at the project

- Find `setup()` and the `WiFi.begin()` call.
- Note whether it runs a web server (`WebServer`, `ESPAsyncWebServer`) and on which port.

## Steps

1. The library:
   - Arduino IDE: tell the user to install **Pinscope** with Tools → Manage Libraries.
   - PlatformIO: add to `platformio.ini`:
     ```
     lib_deps = pinscope/pinscope@^0.2.15
     ```
2. The project key. The user copies it from the dashboard: their project, tab **Connect a device**. Put it in a separate header that is not committed, for example `secrets.h` next to the sketch, added to `.gitignore`:
   ```c
   #define PINSCOPE_KEY "psk_..."   // the user fills this in
   ```
3. Start it in `setup()`, after `WiFi.begin()`. It does not wait for the connection and reconnects by itself:
   ```cpp
   #include <pinscope.h>
   #include "secrets.h"
   ...
   pinscope_config_t config = {};
   config.project_key = PINSCOPE_KEY;
   config.firmware_version = "1.0.0";   // shown in the dashboard; raise it with each build
   pinscope_start(&config);
   ```
4. If the web server is not on port 80, add `config.ports[0] = 8080;` (its port) before `pinscope_start()`.
5. So a firmware update that cannot reach Pinscope is rolled back, add this once, outside any function:
   ```cpp
   extern "C" bool verifyRollbackLater() { return true; }
   ```
   Updates need a partition scheme with two app slots; the default scheme has them.
6. Text passed to `pinscope_log()` reaches the dashboard's log; `Serial.print()` stays on the cable. If the sketch logs with Serial, you may add a small helper that writes to both.

Crash reports need Arduino-ESP32 3.x; everything else works on 2.x too.

## Check

- The sketch compiles.
- After uploading, the device appears in the dashboard under Devices, with its own address.

## 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).
