CAN-logger2

Public

@ubx

Download board files

Files for version 1. Pick what you came for.

Share CAN-logger2

Check access before sharing the link.

Who can open this board

Anyone can open this board. No sign-in is required.

This link opens the latest version. Copying it does not grant additional access.

Loading…

README

ESP32 CAN Logger for CANaerospace with WiFi File Browser

This project implements a dual-mode ESP32 application for capturing and storing CAN bus data onto an SD card,
while also providing a temporary WiFi access point with a web-based file browser to retrieve logged data.

Features

WiFi File Browser

  • WiFi SoftAP Setup with WPA/WPA2 authentication.
  • Dynamic SSID Generation based on ESP32 MAC address.
  • Web-based File Browser:
    • Displays SD card files in a clean HTML table.
    • Supports file size display in human-readable format (KB, MB, GB).
    • Click-to-download functionality with MIME type detection.
  • Automatic Session Timeout
    • Tracks last HTTP activity.
    • Shuts down after configurable inactivity time.

CAN Logging System

  • CAN Bus Support (TWAI Driver)
    • Configured for 500 kbit/s.
    • Accepts all CAN frames.
    • Reliable driver startup with retry on failure.
  • SD Card Logging
    • Files named sequentially as CANxxxxx.BIN.
    • Automatic old file cleanup if free space < 2 GB (reclaims up to 4 GB).
    • Efficient batch writes with 64 KB buffering.
    • Uses fsync() to ensure data integrity.
  • Logging Format
    • Each CAN frame stored in a compact 21-byte binary format:
      • Timestamp: 8-byte double (Unix epoch).
      • CAN ID: 4-byte unsigned integer.
      • Length: 1-byte unsigned integer.
      • Data: 8-byte buffer.
    • Efficient storage compared to text-based logging.
  • Task-based Architecture
    • CAN_RX Task: Receives frames from TWAI driver.
    • CAN_Proc Task: Packs messages into binary structures.
    • SD_Writer Task: Buffers and writes batches to SD card.
  • Runtime Monitoring
    • Tracks total message count.
    • Periodic logging of statistics to console.

Technical Highlights

  • FreeRTOS Queues for decoupled CAN reception and SD writing.
  • High-throughput SD logging using buffered I/O.
  • Failsafe Storage Management with automatic cleanup.
  • Lightweight Web Server (ESP-IDF HTTPD) for SD browsing and downloads.
  • No RTC dependency: Uses a fictional start timestamp to emulate Unix time.
  • Tested on an ESP32-S3 board: ESP32-S3 1.64inch AMOLED Touch Display Development Board with a SANDISK Ultra
    64 GB, microSDXC, U1, UHS-I.
  • Performance Test: Running cangen can0 -D i -I i -L 4 -g 0.5 resulted in a transmission rate of
    approximately 1,800 messages per second.
  • Power consumption: Web server: 473 mW; Logger with display on: 420 mW; Logger with display off: 440 mW.

Typical Use Case

  1. Power up device → ESP32 starts in WiFi AP mode with SD card accessible via browser.
  2. Download / inspect past CAN logs through the web interface.
  3. After inactivity timeout → Device switches into logging mode, continuously recording
  4. CAN traffic onto the SD card.

Pin-to-Pin Wiring Table

ESP32 PinFunctionConnected ToNotes
GPIO1SPI MOSISD Card MOSIData out from ESP32 → SD
GPIO3SPI MISOSD Card MISOData in from SD → ESP32
GPIO2SPI SCLKSD Card SCLKSPI clock
GPIO41SPI CSSD Card CSChip select
GPIO9CSLCD Chip SelectActive low
GPIO10PCLKLCD SPI Clock / Pixel ClockQSPI clock
GPIO11DATA0LCD Data 0 (D0)QSPI data line
GPIO12DATA1LCD Data 1 (D1)QSPI data line
GPIO13DATA2LCD Data 2 (D2)QSPI data line
GPIO14DATA3LCD Data 3 (D3)QSPI data line
GPIO21RSTLCD ResetOptional, active low
GPIO18CAN TXCAN Transceiver TXDTransmit to CAN bus
GPIO17CAN RXCAN Transceiver RXDReceive from CAN bus
3V3PowerSD Card VCC, CAN VCCEnsure 3.3V compatible
GNDGroundSD Card GND, CAN GNDCommon ground reference
CANH/——CAN Bus High LineDifferential bus line
CANL/——CAN Bus Low LineDifferential bus line

Core Components

  • ESP32 Development Board

    • Example: ESP32-DevKitC, ESP32-WROOM, or M5Stack Core (if display/UI desired)
    • Must expose SPI pins and GPIOs used in the project
  • SD Card Module (SPI interface)

    • Example: MicroSD Card Module (3.3V logic, SPI mode support)
    • Ensure 3.3V compatible (no 5V modules without level shifting)
    • MicroSD card, Class 10, A1 or higher, ≥16 GB recommended
  • CAN Transceiver (connects ESP32 to CAN bus)

    • Example ICs:
      • SN65HVD230 (3.3V, commonly used with ESP32)
      • MCP2551 (5V, requires level shifting for ESP32)
      • TJA1050 (5V, also requires level shifting)
    • Choose based on bus voltage (3.3V-friendly parts are easier)

Power & Connectivity

  • Stable 5V Supply (for ESP32 board)
    • ESP32 boards usually regulate down to 3.3V internally
  • Common Ground Reference between ESP32, SD card, and CAN transceiver
  • CAN Bus Wiring
    • Twisted pair cable (for CANH & CANL)
    • 120 Ω termination resistors at both bus ends

Optional / Helpful

  • MicroSD Card Reader (for PC, to inspect logs directly if needed)
  • USB-to-CAN Adapter (for testing/logging against a PC CAN interface)
  • Case/Enclosure to protect the ESP32 + modules in automotive environment
  • For CANaerospace logs: use this program to adjust timestamps.
  • Format SD card: use this tool

Bill of materials

NumberPartQtyPictureSource
1ESP32-S3 1.64inch AMOLED
Touch Display Development Board
1AMOLED Touch Display
Development Board
2SN65HVD230 CAN Board1SN65HVD230 CAN Board
3Step Down Converter Modul1Step Down Converter Modul
4M12 5 Pin Cable Connector1M12 5 Pin Cable Connector
5micro sd card1
6M2x5 round screw4
7M2.5x5 countersunk screw1
8M2.5x4 insert1
9Optional:
SD-TF-extension-cable
1SD-TF-extension-cable

Troubleshooting: sdQueue full / canQueue full warnings

If you see warnings like:

  • W (xxxx) CAN_Proc: sdQueue full, dropped packet
  • W (xxxx) CAN_RX: canQueue full, dropped

it means the producer tasks (CAN reception/formatting) are temporarily faster than the consumer (SD writer).
The firmware includes several mitigations:

Additional steps you can take

  • Use a fast SD card (A1/A2 or High Endurance) and keep it healthy/formatted (FAT32).
  • Reduce other workload while logging (disable unnecessary peripherals, WiFi, or display if not needed).
  • If you still experience drops, you can increase queue depths in src/logger/logging.cpp:
    • CAN_QUEUE_LEN: number of CAN frame buffered between driver and formatter.
    • SD_QUEUE_LEN: number of formatted lines buffered before SD writing. Be mindful that increasing these consumes internal RAM.
  • If your board has PSRAM, keep it enabled. The logger uses a large batch buffer to write in big chunks for higher throughput.
  • Ensure stable 3.3 V supply. Brownouts can slow peripherals and the filesystem.

Notes

  • Even with a high-rate CAN traffic (e.g., ~1200 msgs/s), a proper SD card and power supply should avoid drops in most cases.
  • If absolute losslessness is required, consider reducing CAN bus load or increasing queue sizes.

Todo

  • Check display z-distance.
  • PCB, move modules up 2mm z-distance.
  • PCB, bigger holes for CAN connector cables.
  • Check usb hole, make it more tolerant.
  • Better lid locking.
Comments

No comments yet. Be the first to ask about this board.

Ask about this board

Sign in to BoardRepo

New here? Signing in creates your account; there is no separate sign-up.