This basic example showcases how to use PlatformIO with ESP-IDF only (no Arduino) to connect to Wifi in station mode and print Hello world in a loop. It is based on the basic Hello World example from PlatformIO ESP32 with ESP-IDF minimal C++ example:
#include <cstdio> #include <freertos/FreeRTOS.h> #include <freertos/task.h> #include <esp_wifi.h> #include <esp_log.h> #include <nvs_flash.h> #include <esp_netif.h> #include <esp_event.h> extern "C" { void app_main(void); } static void NetworkEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { esp_wifi_connect(); ESP_LOGI("wifi", "Retrying to connect to Wifi..."); } else 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("wifi", "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); } } void InitNVS() { // Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); } void InitWifi() { // Initialize TCP/IP network interface (required for Wi-Fi) ESP_ERROR_CHECK(esp_netif_init()); // Initialize the event loop ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_create_default_wifi_sta(); // Initialize Wi-Fi wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // Set Wi-Fi to station mode ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // Configure the Wi-Fi connection wifi_config_t wifi_config = { .sta = { .ssid = "MyWifi", .password = "mypassword" } }; // Register event handler ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &NetworkEventHandler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &NetworkEventHandler, NULL, NULL)); // Set Wi-Fi configuration and start Wi-Fi ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); } void app_main() { InitNVS(); InitWifi(); while(true) { printf("Hello PlatformIO!\n"); // Wait for one second vTaskDelay(1000 / portTICK_PERIOD_MS); } }
[env:esp32dev] platform = espressif32 board = esp32dev framework = espidf monitor_speed = 115200
All sdkconfig
settings have been left at their respective defaults.