ESP32/main/main.c

63 lines
1.7 KiB
C

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "lwip/inet.h"
#include "iperf.h"
#include "wifi_cfg.h"
static const char *TAG = "main";
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR " gw:" IPSTR " netmask:" IPSTR,
IP2STR(&event->ip_info.ip),
IP2STR(&event->ip_info.gw),
IP2STR(&event->ip_info.netmask));
// Auto-start iperf server after getting IP
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait 1 second for stability
iperf_cfg_t cfg;
memset(&cfg, 0, sizeof(cfg));
cfg.flag = IPERF_FLAG_SERVER | IPERF_FLAG_TCP;
cfg.sport = 5001;
iperf_start(&cfg);
ESP_LOGI(TAG, "iperf TCP server started on port 5001");
}
}
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));
wifi_cfg_init();
wifi_cfg_apply_from_nvs();
ESP_LOGI(TAG, "System init complete. iperf server will start after WiFi connects.");
while(1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}