This project implements a comprehensive Vehicle-to-Everything (V2X) communication system that enables real-time data exchange between electric vehicles (EVs), charging stations (EVCS), and cloud platforms. The system integrates CAN bus monitoring, GPS tracking, ESP-NOW wireless communication, and IoT data forwarding to support V2V (Vehicle-to-Vehicle), V2G (Vehicle-to-Grid), and V2I (Vehicle-to-Infrastructure) use cases.
The architecture consists of two main components:
- On-board vehicle system using dual ESP32 microcontrollers and a Jetson Nano.
- Electrical Vehicle Charging Station (EVCS) with dedicated ESP32 and Raspberry Pi monitoring systems.
ESP32 (1) – CAN and GPS Data Collector
- Connected to the vehicle’s CAN bus via an MCP2515 controller and to the Jetson Nano via USB.
- Collects raw CAN bus data and GPS information from the vehicle.
- Transmits both streams to the Jetson Nano via USB serial.
- Data is processed by:
CAN_data_Receive_with_threads.c
ESP32 (2) – V2V and V2G Communication Hub
- Handles ESP-NOW communication with other vehicles and charging stations.
- Connected to the Jetson Nano via USB serial (
/dev/ttyUSB1,/dev/ttyUSB2, etc.).
- Receives parsed CAN data (speed, acceleration, brake status, gear, GPS, SOC, current, etc.) from the Jetson.
- Broadcasts this data to surrounding vehicles via ESP-NOW.
- Listens continuously for incoming messages from other vehicles or EVCSs.
- Forwards received external data back to the Jetson via USB serial.
- Stores incoming data in timestamped CSV files:
ESP_now_data_YYYYMMDD_HHMM.csv
- When the vehicle is plugged into a charging station, it initiates secure communication with the EVCS's ESP32.
- Monitors charging parameters (voltage, current, energy, power factor).
- Communication ends when the charging connector is removed.
- After disconnection, the vehicle continues transmitting battery data (voltage, current, SOC) as long as it remains within ~105 meters of the charging station.
Jetson Nano – Central V2X Bridge
This program acts as the central interface between ESP32(2) and the rest of the system.
- Sends parsed CAN data to ESP32(2) via USB serial for broadcasting.
- Receives data from ESP32(2) about other vehicles and EVCSs.
- Logs received data into timestamped CSV files:
ESP_now_data_YYYYMMDD_HHMM.csv - Monitors V2G sessions and tracks charging events.
- Merged CAN CSV (
parsed_filename):
Continuous log of CAN + GPS + charging detection status. - V2G Session CSV (
v2g_log_filename):
Summary logs of each charging session (start/end time, energy transferred, cost).
Jetson – Real-Time CAN and GPS Logger
- Receives raw CAN and GPS data from ESP32(1) via USB serial.
- Processes and stores data in two files:
| File | Purpose | Update Behavior |
|---|---|---|
can_data.csv |
Real-time log of CAN and GPS data | Continuously updated |
can_data_snapshot.csv |
Snapshot of recent data | Updated every 4 seconds with latest 1000 lines from circular buffer; collection paused briefly during update |
Uses threading and a circular buffer for efficient data handling.
Jetson – Cloud Data Uploader
- Sends selected vehicle telemetry (speed, acceleration, location, etc.) to a ThingsBoard server via MQTT.
- Reads data from:
merged_can_data_YYYYMMDD_HHMM.csv(generated byESPnow_EVCS_V2G.c) - Designed to run periodically or in real-time depending on configuration.
MQTT Configuration File
Contains essential connection parameters for Send2Server.c:
- Broker IP address
- Broker port
- Device token
- Other authentication details
!! If the server changes, this file must be updated accordingly.
CAN Data Parsing Library
- Used by
ESPnow_EVCS_V2G.cto convert raw hexadecimal CAN data fromcan_data_snapshot.csvinto human-readable values. - Enables meaningful interpretation of signals such as speed, gear, battery state, etc.
EVCS – Main ESP32 Controller (Slot 1)
Runs on the ESP32 in the primary charging slot and performs three key functions:
- AC Power Monitoring
- Uses a PZEM-004T sensor over UART to measure:
- Voltage, current, power, energy, frequency, power factor
- Uses a PZEM-004T sensor over UART to measure:
- Bidirectional V2G Communication
- Establishes secure ESP-NOW peer-to-peer communication with the vehicle’s ESP32.
- Detects vehicle connection via current threshold (>0.1A).
- Exchanges charging parameters (max power, cost per kWh).
- Tracks session metrics (energy delivered, cost).
- Responds to vehicle commands (e.g., stop charging).
- Data Forwarding
- Sends structured telemetry (including GPS coordinates) to a Raspberry Pi via serial.
- Supports geolocation-aware features by calculating distance between EV and EVCS.
Includes robust error handling, debug logging, and session management.
EVCS – Secondary Slot Monitor (Slot 2)
Dedicated to low-power devices (smartphones, e-scooters, laptops, tools).
- Uses PZEM-004T sensor to monitor AC power (voltage, current, power, energy, etc.).
- Reports data every 2 seconds to a Raspberry Pi via serial.
- Detects active usage using a simple heuristic: current > 0.1A.
- Sends structured output including fixed geographic coordinates of the station.
- Does not support V2G, billing, or remote control.
- Lightweight and passive — ideal for standard <2kW outlets.
Raspberry Pi – Dual-Slot Monitoring System
Runs on the Raspberry Pi connected to the dual-slot EVCS.
- Reads data from two ESP32s (one for EVs, one for small devices) via serial ports.
- Parses and validates incoming telemetry.
- Tracks charging sessions and calculates energy costs.
- Sends aggregated JSON reports to a cloud IoT platform (e.g., ThingsBoard) via MQTT.
- Supports configuration files, multi-threading, graceful shutdown, and error recovery.
Configured as a systemd service named
twizy_evcs.serviceon EELAB's EVCS1.
Recommended to deploy the same service on other EVCS Raspberry Pis.
Raspberry Pi – Single-Slot Monitoring Application
A simplified version of the dual-slot monitor for single-socket charging stations.
- Reads power data from one ESP32 via serial.
- Tracks charging sessions and computes energy and cost.
- Sends data to cloud via MQTT.
- Supports configuration file, retry logic, and graceful shutdown.
Intended for standalone deployment where only one charging point exists.
gcc CAN_data_Receive_with_threads.c -o CAN_with_thread_out -lpthread /dev/ttyUSBx
Replace /dev/ttyUSBx with the actual port connected to ESP32(1).
gcc ESPnow_EVCS_V2G.c parseCANFrame.c -o ESPnow_v2g_v2v_out -lpthread /dev/ttyUSBx
Replace /dev/ttyUSBx with the actual port connected to ESP32(2).
gcc Send2Server.c -o SendToCloud -lpaho-mqtt3c -lm
Ensure MQTT library is installed (sudo apt install libpaho-mqtt3c-dev)
This table summarizes the key software components used in the Twizy V2X system.
| Component | Source Files |
|---|---|
| Vehicle | |
| - ESP32 (1) – CAN & GPS Collector | ESP32_CANSniffer/src/main.cpp |
| - ESP32 (2) – V2V/V2G Communication | V2X_esp_master/src/main.cpp |
| - Jetson Nano – V2X Bridge | Jetson_Code/c_codes/tryAgain/working_codes/ESPnow_EVCS_V2G.c |
| - Jetson Nano – CAN/GPS Logger | Jetson_Code/c_codes/tryAgain/working_codes/CAN_data_Receive_with_threads.c |
| - Jetson Nano – Cloud Uploader | Jetson_Code/c_codes/tryAgain/working_codes/Send2Server.c |
| - Jetson Nano – CAN Parser Library | Jetson_Code/c_codes/tryAgain/working_codes/parseCANFrame.c |
| - Jetson Nano – MQTT Config | Jetson_Code/c_codes/tryAgain/working_codes/mqtt_config.txt |
| EVCS | |
| - EV Charging Slot (Slot 1) | Twizy_EVCS/Twizy_EVCS_ESP32_code/src/main.cpp |
| - Small Device Slot (Slot 2) | Twizy_EVCS/Twizy_EVCS_ESP32_slot2_code/src/main.cpp |
| - Dual-Slot RPi Monitor | Twizy_EVCS/Twizy_EVCS_RPI_code/EVCS_Monitor_Dual_Slot.c |
| - Single-Slot RPi Monitor | Twizy_EVCS/Twizy_EVCS_RPI_code/Twizy_EVCS_RPI.c |