StackSense

UCI CS 147 (Internet of Things) · June–August 2026 · Solo project

A sensor node that turns a gym weight-stack machine into a per-rep motion tracker

Stack

ESP32 (LILYGO T-Display), C++ (PlatformIO), VL53L1X time-of-flight sensor, LSM6DSO accelerometer, I2C, Wi-Fi / HTTP, Java (Javalin), MySQL, AWS EC2 (Ubuntu), HTML / JavaScript dashboard

Goal and outcome

Beginners can't see how well they control each rep: does the range of motion quietly shrink as they tire, or do they let the weight drop on the way down? A coach could tell them, but coaching is expensive for beginners, and the feedback is verbal rather than measured.

StackSense sits on top of the weight stack and moves with it. An upward-facing laser time-of-flight (ToF) sensor measures how far the stack travels, and an accelerometer records acceleration on the same axis. The device counts reps live, with feedback on its screen and buzzer; after each set it uploads the raw samples to a server, which computes range of motion, concentric and eccentric duration, and peak velocity for every rep.

Outcome: the device was tested in a public gym, data reaches AWS EC2 over Wi-Fi, and a web dashboard charts stored sets and provides a ready-made analysis prompt users can paste into their own AI chatbot.

  1. Sensors

    • VL53L1X ToF: stack displacement
    • LSM6DSO: acceleration, same axis

    I2C

  2. ESP32 · LILYGO T-Display

    • Smoothing, rep detection
    • 40 KB set buffer
    • Screen, buzzer, two buttons

    Wi-Fi · HTTP POST (binary, 5 B/sample)

  3. AWS EC2 (Ubuntu)

    • Javalin: decode, per-rep analysis
    • MySQL: stored sets

    Query

  4. Web dashboard

    • Charts per set
    • Copyable AI analysis prompt
Final data flow, redrawn from the report's System Architecture section: sensors → ESP32 → Wi-Fi → Javalin and MySQL on AWS EC2 → web dashboard.

Early attempts (dropped)

  • BLE to a phone, which would forward data to the cloud
  • Uploading each set as JSON
  • Passing samples in a GET query string

Final implementation

  • ESP32 joins Wi-Fi in station mode
  • HTTP/1.1 POST with a packed binary body (5 bytes per sample)
  • Javalin on AWS EC2 decodes and analyzes; MySQL on the same instance stores it
  • Web dashboard plus a copyable AI analysis prompt

Not built / future work

  • Server-side natural-language coaching with AWS Bedrock (not built: billed per token on every set)
  • Accounts with per-user data isolation and authenticated delete
  • TLS uploads with a signed device token
  • A new case with the ToF sensor facing the floor and a magnetic mount on the side of the stack
Watch the demoStackSense demo videoYouTube · opens in a new tab

My role

Solo project: everything below is my work, matching the task table in the report.

  • Hardware: wired both sensors on one I2C bus (SCL on GPIO 22, SDA on GPIO 21), plus two interrupt-driven buttons and a buzzer.
  • Sensor characterization: the ToF reads at about 53 Hz with roughly 2 mm of noise; the accelerometer reads 1.03 g at rest, not 1.0 g, so I tare it before every set; just bumping the desk produces 2.5 g spikes.
  • Signal processing: a per-set tare baseline, moving-average smoothing, and velocity estimated over a 6-point window.
  • Rep detection: an Idle / Up / Down state machine using peak/valley detection with hysteresis and a minimum range-of-motion check; each completed rep triggers buzzer and screen feedback.
  • Buffering: a fixed 40 KB array holds about 150 s of raw samples, so a whole set is stored and uploaded afterward.
  • Server: a Javalin endpoint decodes binary uploads, validates them, stores them in MySQL and computes per-rep metrics. I checked the full path from HTTP POST to decode, validation and storage.
  • Dashboard and deployment: built the visualization and analysis dashboard, deployed under stacksense.xxxmnalx.com, with a copyable AI analysis prompt.
  • Collected real data at the gym.

Technical problems

Problem 1: Getting data off the device: from BLE to Wi-Fi

Problem
The first architecture was ESP32 → BLE → phone gateway → cloud. The device side worked; the phone side didn't. nRF Connect can read and write BLE characteristics but can't forward anything to a server, which left Web Bluetooth or a native iOS app. Web Bluetooth isn't supported in Safari or Chrome on iOS, and building a native iOS app was beyond my skills at the time.
What I did
  • Switched to a Wi-Fi architecture: the ESP32 joins the access point in station mode and sends the set straight to the server when it ends.
  • The gateway/ page in the repository (“StackSense BLE Probe”) is left over from this attempt.
Why this approach
Taking the phone out of the data path removed the dependency on browser Bluetooth support entirely. The trade-off is that changing the Wi-Fi name and password is harder for the user.

Problem 2: Fitting a whole set into one upload: from JSON to a binary POST

Problem
My first plan was to upload each set as JSON, the format I knew best, but it didn't fit in the ESP32's limited RAM. After switching to packed binary, I passed the samples in a GET query string as in an earlier lab, and larger sets were refused: Jetty, underneath Javalin, rejects request lines longer than its header size limit, so the request never reached my handler.
What I did
  • Pack each sample into 5 bytes (time delta, height, acceleration), keep the whole set in a fixed 40 KB buffer, and use a compile-time assertion so struct padding can't creep in.
  • When the set ends, send one HTTP POST whose body is those raw bytes, declared as application/octet-stream; the server decodes it on a 5-byte stride.
Why this approach
A request body has no URL-length limit, and at 5 bytes per sample a whole set fits both in device memory and in a single request.
struct __attribute__((packed)) Sample {
  uint8_t dt;
  int16_t height;
  int16_t accel;
};
static_assert(sizeof(Sample) == 5, "Sample must stay packed at 5 bytes");
// …
static uint8_t sampleBuf[MAX_SAMPLES * sizeof(Sample)];   // 40 KB
// …
http.addHeader("Content-Type", "application/octet-stream");
int code = http.POST(sampleBuf, bufferUsed);
Excerpt from src/main.cpp in the repository: the sample struct, the buffer and the upload call (“// …” marks omitted lines).

Results and verification

Samples in a 10-rep set
2,519
Effective sampling rate (46.5 s)
54 Hz
Uploaded in under 2 s
12,595 B
  • Tested in a public gym: in a representative set of ten reps, the device collected 2,519 samples in 46.5 s (54 Hz effective) and uploaded 12,595 bytes in under two seconds, exactly 5 bytes per sample.
  • How it was verified: the server re-segments the reps from the raw samples on its own, and its segmentation matched the live count shown on the device.

Limitations

  • Designed for one user, one machine and one server, with no accounts, so anyone visiting the dashboard can see every stored set.
  • Uploads use plain HTTP without authentication, so anyone on the same network could read or forge a set.
  • The ToF reading depends on a diffuse surface above the sensor; a polished or angled panel, or a slight tilt of the device, makes the signal collapse, so the current design needs a prepared target surface.
  • The user presses a button to end a set; continuous non-stop cycling isn't handled.

Evidence