Skip to content

Twizy V2X Communication System 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 t

Notifications You must be signed in to change notification settings

MOHAMEDELHALOUA/V2X_Twizy_project

Repository files navigation

Twizy V2X Communication System

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:

  1. On-board vehicle system using dual ESP32 microcontrollers and a Jetson Nano.
  2. Electrical Vehicle Charging Station (EVCS) with dedicated ESP32 and Raspberry Pi monitoring systems.

Vehicle-Side Software Architecture

1. ESP32_CANSniffer/src/main.cpp

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

2. V2X_esp_master/src/main.cpp

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.).

Functions:

  • 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

V2G Communication:

  • 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.

3. Jetson_Code/c_codes/tryAgain/working_codes/ESPnow_EVCS_V2G.c

Jetson Nano – Central V2X Bridge

This program acts as the central interface between ESP32(2) and the rest of the system.

Key Responsibilities:

  • 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.

Generated Files:

  • 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).

4. Jetson_Code/c_codes/tryAgain/working_codes/CAN_data_Receive_with_threads.c

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.


5. Jetson_Code/c_codes/tryAgain/working_codes/Send2Server.c

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 by ESPnow_EVCS_V2G.c)
  • Designed to run periodically or in real-time depending on configuration.

6. Jetson_Code/c_codes/tryAgain/working_codes/mqtt_config.txt

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.


7. Jetson_Code/c_codes/tryAgain/working_codes/parseCANFrame.c

CAN Data Parsing Library

  • Used by ESPnow_EVCS_V2G.c to convert raw hexadecimal CAN data from can_data_snapshot.csv into human-readable values.
  • Enables meaningful interpretation of signals such as speed, gear, battery state, etc.

Electrical Vehicle Charging Station (EVCS) Software

1. Twizy_EVCS/Twizy_EVCS_ESP32_code/src/main.cpp

EVCS – Main ESP32 Controller (Slot 1)

Runs on the ESP32 in the primary charging slot and performs three key functions:

  1. AC Power Monitoring
    • Uses a PZEM-004T sensor over UART to measure:
      • Voltage, current, power, energy, frequency, power factor
  2. 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).
  3. 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.


2. Twizy_EVCS/Twizy_EVCS_ESP32_slot2_code/src/main.cpp

EVCS – Secondary Slot Monitor (Slot 2)

Dedicated to low-power devices (smartphones, e-scooters, laptops, tools).

Features:

  • 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.

3. Twizy_EVCS/Twizy_EVCS_RPI_code/EVCS_Monitor_Dual_Slot.c

Raspberry Pi – Dual-Slot Monitoring System

Runs on the Raspberry Pi connected to the dual-slot EVCS.

Capabilities:

  • 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.service on EELAB's EVCS1.
Recommended to deploy the same service on other EVCS Raspberry Pis.


4. Twizy_EVCS/Twizy_EVCS_RPI_code/Twizy_EVCS_RPI.c

Raspberry Pi – Single-Slot Monitoring Application

A simplified version of the dual-slot monitor for single-socket charging stations.

Features:

  • 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.


Compilation and Execution Instructions

1. CAN_data_Receive_with_threads.c

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).

2. ESPnow_EVCS_V2G.c

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).

3. Send2Server.c

gcc Send2Server.c -o SendToCloud -lpaho-mqtt3c -lm

Ensure MQTT library is installed (sudo apt install libpaho-mqtt3c-dev)

Project Hierarchy Overview

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

About

Twizy V2X Communication System 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 t

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published