Skip to content
TheChatArchiveThe Chat Archive
grok-3-preview-02-24· xAIHugging Face LMArena Human Preference 140K

give me a minimal sample C code aimed at esp-idf that would create an FTP server on the…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-preview-04-17’s answer.
  1. User

    give me a minimal sample C code aimed at esp-idf that would create an FTP server on the device so I can transfer files from VFS mounted SD card. This code should be focused on minimal memory footprint. Assume WIFI is already enabled and configured

  2. Assistant

    Below is a minimal sample C code for an ESP-IDF project that sets up an FTP server on an ESP32 device, allowing file transfers from a VFS-mounted SD card. This example focuses on a minimal memory footprint by using the lightweight esp-ftpd library (a third-party FTP server implementation for ESP-IDF) and avoids unnecessary features. It assumes Wi-Fi is already enabled and configured, and the SD card is mounted via VFS at /sdcard.

    Prerequisites

    1. ESP-IDF Setup: Ensure you have ESP-IDF installed and configured.
    2. SD Card Mounted: The SD card should already be mounted at /sdcard using the VFS (e.g., via esp_vfs_fat_sdmmc_mount).
    3. Wi-Fi Configured: Wi-Fi is already connected and the device has an IP address.
    4. esp-ftpd Library: You need to include the esp-ftpd library in your project. You can add it as a component by cloning it from its GitHub repository (e.g., https://github.com/nopnop2002/esp-idf-ftpServer) into your project's components directory.

    Minimal FTP Server Code

    #include <stdio.h>
    #include <string.h>
    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "esp_log.h"
    #include "esp_vfs.h"
    #include "ftpd.h"
    
    static const char *TAG = "ftp_server";
    
    // FTP server configuration
    #define FTP_USER "user"         // FTP username
    #define FTP_PASS "pass"         // FTP password
    #define FTP_ROOT_DIR "/sdcard"  // Root directory for FTP access (VFS-mounted SD card)
    #define FTP_PORT 21             // FTP control port
    #define FTP_MAX_CLIENTS 1       // Limit to 1 client to save memory
    #define FTP_TIMEOUT_MS 30000    // 30 seconds timeout to save resources
    
    void ftp_server_task(void *pvParameters) {
        // FTP server configuration
        ftpd_config_t ftpd_config = {
            .port = FTP_PORT,
            .max_clients = FTP_MAX_CLIENTS,
            .timeout_ms = FTP_TIMEOUT_MS,
            .root_dir = FTP_ROOT_DIR,
            .user = FTP_USER,
            .pass = FTP_PASS,
            .task_priority = 2,  // Low priority to minimize resource usage
            .task_stack_size = 2048,  // Minimal stack size for FTP task
        };
    
        ESP_LOGI(TAG, "Starting FTP server...");
        if (ftpd_start(&ftpd_config) != ESP_OK) {
            ESP_LOGE(TAG, "Failed to start FTP server");
            vTaskDelete(NULL);
        }
    
        ESP_LOGI(TAG, "FTP server started. Connect using user: %s, pass: %s", FTP_USER, FTP_PASS);
        ESP_LOGI(TAG, "Root directory: %s", FTP_ROOT_DIR);
    
        // Keep the task running (FTP server runs in the background)
        while (1) {
            vTaskDelay(pdMS_TO_TICKS(1000)); // Minimal delay to prevent task hogging CPU
        }
    }
    
    void app_main(void) {
        // Ensure SD card is mounted at /sdcard before starting FTP server
        // (This is assumed to be done elsewhere in your code)
    
        // Start FTP server task
        xTaskCreate(ftp_server_task, "ftp_server_task", 2048, NULL, 2, NULL);
    }
    

    Key Features of the Code

    1. Minimal Memory Footprint:

      • The FTP server task uses a minimal stack size (2048 bytes).
      • The task priority is set low (2) to avoid interfering with other critical tasks.
      • Only one FTP client is allowed at a time (FTP_MAX_CLIENTS = 1).
      • A timeout of 30 seconds (FTP_TIMEOUT_MS) ensures idle connections are closed to free resources.
    2. VFS Integration:

      • The FTP server uses the VFS-mounted SD card directory (/sdcard) as its root directory.
      • No additional file system abstraction is added, keeping the code lightweight.
    3. Simple Authentication:

      • A hardcoded username (user) and password (pass) are used for simplicity. In a production environment, consider using more secure methods.
    4. Logging:

      • Minimal logging is used to avoid excessive memory and CPU usage. Only essential information is logged.

    How to Use

    1. Build and Flash:

      • Add the esp-ftpd component to your project (e.g., clone it into the components directory).
      • Ensure your CMakeLists.txt and sdkconfig are properly configured for SD card and Wi-Fi support.
      • Build and flash the code to your ESP32 device using ESP-IDF tools.
    2. Connect to the FTP Server:

      • Use an FTP client (e.g., FileZilla, ftp command-line tool) to connect to the ESP32.
      • Use the IP address of your ESP32 (which you can find in your Wi-Fi logs or via ESP_LOGI if you print it).
      • Login credentials: Username = user, Password = pass.
      • Access files on the SD card under the /sdcard directory.

    Example FTP Client Usage

    Using the ftp command-line tool:

    ftp <ESP32_IP_ADDRESS>
    # Enter username: user
    # Enter password: pass
    # Use commands like 'ls', 'get', 'put' to interact with the SD card
    

    Notes

    1. Security:

      • This example uses plain FTP, which is not secure (credentials and data are sent in plaintext). For production use, consider implementing FTPS (FTP over TLS) or using a more secure protocol like SFTP.
      • Hardcoded credentials are used for simplicity. In a real application, consider a more secure authentication mechanism.
    2. Memory Optimization:

      • The stack size (2048) and task priority (2) are set to minimal values. If you encounter stability issues, you may need to increase these slightly.
      • The FTP_MAX_CLIENTS is set to 1 to minimize memory usage. If you need to support multiple clients, increase this value, but be aware of the increased memory footprint.
    3. Dependencies:

      • Ensure the esp-ftpd library is included in your project. If it's not available, you may need to implement a custom FTP server, which would significantly increase the complexity and memory footprint.
    4. Error Handling:

      • This example includes minimal error handling for simplicity. In a production environment, add proper error handling for SD card access, FTP server startup, and network issues.

    This code provides a minimal, functional FTP server for file transfers from an SD card on an ESP32, optimized for low memory usage.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.