Back to engineering archive

Record 052 / Connected systems

ESP32 IMU telemetry over Wi-Fi TCP

ESP32 firmware reads an MPU6050 over I2C and streams timestamped motion readings to a connected TCP client.

Development work
ESP32MPU6050I2CWi-FiTCPCSV telemetry

Firmware flow

Sample the IMU and stream a CSV row

Main-loop decisions from the matched ESP32 source; Wi-Fi and IMU setup happen earlier.

Client acceptance and sampling are separate checks. Sampling continues without a receiver, but transmission requires a connected client.

This is a source-level explanation. It does not establish MQTT, cellular support, notebook operation or measured telemetry performance.

Read the implementation

code2.txt — timed acquisition and CSV transmission. Network credentials are excluded.

  unsigned long now = millis();

  // Sample every SAMPLE_INTERVAL_MS
  if (now - lastSampleMs >= SAMPLE_INTERVAL_MS) {
    lastSampleMs = now;

    sensors_event_t a, g, temp;
    mpu.getEvent(&a, &g, &temp);

    if (client && client.connected()) {
      // csv: t_ms,ax,ay,az,gx,gy,gz
      char buf[160];
      int n = snprintf(
        buf, sizeof(buf),
        "%lu,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f\n",
        now,
        a.acceleration.x,
        a.acceleration.y,
        a.acceleration.z,
        g.gyro.x,
        g.gyro.y,
        g.gyro.z
      );
      if (n > 0) {
        client.write((const uint8_t*)buf, n);
      }
ActionAccept client, disable Nagle buffering and send the header
ActionRecord the time and read accelerometer/gyroscope events
ActionFormat the timestamp and six values; write when the formatted length is positive
ResultDelay 1 ms; continue the loop

Technical notes

Acquire the motion sample

setupMPU() starts the I2C interface, checks the MPU6050 and selects accelerometer, gyroscope and filter settings. The main loop uses millis() to schedule acquisition of acceleration and angular velocity.

One connected receiver

In Wi-Fi station mode the ESP32 runs a TCP server. When no client is connected, it accepts an available client, disables Nagle buffering and sends a column header. Samples are sent only while the client is connected.

Plain CSV, with clear limits

Each data row contains a millisecond timestamp and three acceleration plus three gyroscope values. The matched delivery identifies a Jupyter receiving workflow; the inspected firmware uses raw TCP, not MQTT or cellular networking. No notebook execution, sample-rate accuracy or network reliability test is established.

Engineering archiveArchive record

Project enquiries

Discuss your project.

Tell us what you’re developing and where you need engineering support.

Discuss your project