// put wifi.txt on SD or alternatively on SPIFFS // first line is SSID, next line is password #define SPI_PORT SPI3_HOST #define SPI_CLK 18 #define SPI_MISO 19 #define SPI_MOSI 23 #define SD_PORT SPI3_HOST #define SD_CS 4 #include #include #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_spiffs.h" #include "esp_vfs_fat.h" #include "esp_wifi.h" #include "nvs_flash.h" static char wifi_ssid[65]; static char wifi_pass[129]; static bool wifi_load(const char* path, char* ssid, char* pass) { FILE* file = fopen(path, "r"); if (file != NULL) { // parse the file fgets(ssid, 64, file); char* sv = strchr(ssid, '\n'); if (sv != NULL) *sv = '\0'; sv = strchr(ssid, '\r'); if (sv != NULL) *sv = '\0'; fgets(pass, 128, file); fclose(file); sv = strchr(pass, '\n'); if (sv != NULL) *sv = '\0'; sv = strchr(pass, '\r'); if (sv != NULL) *sv = '\0'; return true; } return false; } #ifdef SPI_PORT static void spi_init() { spi_bus_config_t buscfg; memset(&buscfg, 0, sizeof(buscfg)); buscfg.sclk_io_num = SPI_CLK; buscfg.mosi_io_num = SPI_MOSI; buscfg.miso_io_num = SPI_MISO; buscfg.quadwp_io_num = -1; buscfg.quadhd_io_num = -1; buscfg.max_transfer_sz = 512 + 8; // Initialize the SPI bus on VSPI (SPI3) spi_bus_initialize(SPI_PORT, &buscfg, SPI_DMA_CH_AUTO); } #ifdef SD_CS static sdmmc_card_t* sd_card = NULL; static bool sd_init() { static const char mount_point[] = "/sdcard"; esp_vfs_fat_sdmmc_mount_config_t mount_config; memset(&mount_config, 0, sizeof(mount_config)); mount_config.format_if_mount_failed = false; mount_config.max_files = 5; mount_config.allocation_unit_size = 0; sdmmc_host_t host = SDSPI_HOST_DEFAULT(); host.slot = SD_PORT; // // This initializes the slot without card detect (CD) and write // protect (WP) // // signals. sdspi_device_config_t slot_config; memset(&slot_config, 0, sizeof(slot_config)); slot_config.host_id = (spi_host_device_t)SD_PORT; slot_config.gpio_cs = (gpio_num_t)SD_CS; slot_config.gpio_cd = SDSPI_SLOT_NO_CD; slot_config.gpio_wp = SDSPI_SLOT_NO_WP; slot_config.gpio_int = GPIO_NUM_NC; if (ESP_OK != esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &sd_card)) { return false; } return true; } #endif #endif static void spiffs_init() { esp_vfs_spiffs_conf_t conf; memset(&conf, 0, sizeof(conf)); conf.base_path = "/spiffs"; conf.partition_label = NULL; conf.max_files = 5; conf.format_if_mount_failed = true; if (ESP_OK != esp_vfs_spiffs_register(&conf)) { puts("Unable to initialize SPIFFS"); while (1) vTaskDelay(5); } } // example static void loop(); static void loop_task(void* arg) { uint32_t ts = pdTICKS_TO_MS(xTaskGetTickCount()); while (1) { loop(); uint32_t ms = pdTICKS_TO_MS(xTaskGetTickCount()); if (ms > ts + 200) { ms = pdTICKS_TO_MS(xTaskGetTickCount()); vTaskDelay(5); } } } #ifdef __cplusplus extern "C" #endif void app_main() { printf("ESP-IDF version: %d.%d.%d\n", ESP_IDF_VERSION_MAJOR, ESP_IDF_VERSION_MINOR, ESP_IDF_VERSION_PATCH); #ifdef SPI_PORT spi_init(); // used by the SD reader #endif bool loaded = false; wifi_ssid[0] = 0; wifi_pass[0] = 0; #ifdef SPI_PORT #ifdef SD_CS if (sd_init()) { puts("SD card found, looking for wifi.txt creds"); loaded = wifi_load("/sdcard/wifi.txt", wifi_ssid, wifi_pass); } #endif #endif if (!loaded) { spiffs_init(); puts("Looking for wifi.txt creds on internal flash"); loaded = wifi_load("/spiffs/wifi.txt", wifi_ssid, wifi_pass); } if (loaded) { printf("Initializing WiFi connection to %s\n", wifi_ssid); wifi_init(wifi_ssid, wifi_pass); } TaskHandle_t loop_handle; xTaskCreate(loop_task, "loop_task", 4096, NULL, 10, &loop_handle); printf("Free SRAM: %0.2fKB\n", esp_get_free_internal_heap_size() / 1024.f); } static void loop() { // do work }