/*
* Pinscope without a board: a simulated ESP32 in Wokwi.
*
* 1. Sign up at https://pinscope.dev (free, three devices), then
* "Connect a device" in the dashboard and copy the project key.
* 2. Paste it below and press the green Run button.
* 3. In about half a minute the device is in your dashboard: its logs,
* metrics, and this little web page at an HTTPS address of its own.
* Keep this Wokwi window visible next to the dashboard: browsers slow
* down a simulation in a background tab, and it stops answering.
*
* Press the button on the board to make the firmware crash on purpose:
* the crash report shows up in the dashboard, decoded to the line once
* you upload the .elf (in Wokwi: F1, Download ELF with Debugging Symbols).
*/
#include <WebServer.h>
#include <WiFi.h>
#include <pinscope.h>
const char *PINSCOPE_KEY = "psk_paste_your_key_here";
const int LED_PIN = 26;
const int BUTTON_PIN = 4;
WebServer server(80);
bool ledOn = false;
// Each line goes to the serial monitor and to the dashboard's log.
void logLine(const char *fmt, ...) {
char line[160];
va_list args;
va_start(args, fmt);
int n = vsnprintf(line, sizeof(line) - 1, fmt, args);
va_end(args);
if (n < 0) return;
if (n > (int)sizeof(line) - 2) n = sizeof(line) - 2;
line[n++] = '\n';
Serial.write(line, n);
pinscope_log(line, n);
}
void sendPage() {
String page =
"<!doctype html><meta name=viewport content='width=device-width'>"
"<title>Simulated ESP32</title>"
"<style>body{font:18px sans-serif;text-align:center;margin-top:3em}"
"a{display:inline-block;padding:.6em 1.4em;color:#fff;text-decoration:none;"
"background:LED_BG;border-radius:8px}</style>"
"<h1>Simulated ESP32</h1><p>Uptime: UPTIME s</p>"
"<p><a href='/toggle'>LED is LED_TEXT</a></p>";
page.replace("LED_BG", ledOn ? "#0a7183" : "#555");
page.replace("LED_TEXT", ledOn ? "on" : "off");
page.replace("UPTIME", String(millis() / 1000));
server.send(200, "text/html", page);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.print("Connecting to Wokwi-GUEST (this can take half a minute)");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" connected");
server.on("/", sendPage);
server.on("/toggle", [] {
ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn);
logLine("LED turned %s from the web page", ledOn ? "on" : "off");
server.sendHeader("Location", "/");
server.send(303);
});
server.begin();
if (strncmp(PINSCOPE_KEY, "psk_paste", 9) == 0) {
Serial.println("Paste your project key into PINSCOPE_KEY (pinscope.dev, Connect a device).");
return;
}
pinscope_config_t config = {};
config.project_key = PINSCOPE_KEY;
config.device_name = "wokwi-esp32";
config.firmware_version = "1.0.0";
pinscope_start(&config);
Serial.println("Pinscope started: open your dashboard");
}
void loop() {
server.handleClient();
static uint32_t last;
if (millis() - last >= 10000) {
last = millis();
float temperature = 21.5 + 2.0 * sin(millis() / 60000.0); // a pretend sensor
pinscope_metric("temperature", temperature);
logLine("temperature %.1f C", temperature);
}
if (digitalRead(BUTTON_PIN) == LOW) {
logLine("button pressed: crashing on purpose");
delay(100);
volatile int *nowhere = nullptr;
*nowhere = 42; // the crash report shows this line
}
delay(2);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4