· 5 months ago · Apr 20, 2025, 02:45 AM
1// put wifi.txt on SD or alternatively on SPIFFS
2// first line is SSID, next line is password
3#define SPI_PORT SPI3_HOST
4#define SPI_CLK 18
5#define SPI_MISO 38
6#define SPI_MOSI 23
7
8#define SD_PORT SPI3_HOST
9#define SD_CS 4
10
11#include <sys/stat.h>
12#include <sys/unistd.h>
13#include "driver/gpio.h"
14#include "driver/spi_master.h"
15#include "esp_spiffs.h"
16#include "esp_vfs_fat.h"
17#include "esp_wifi.h"
18#include "nvs_flash.h"
19
20static char wifi_ssid[65];
21static char wifi_pass[129];
22
23static bool wifi_load(const char* path, char* ssid, char* pass) {
24 FILE* file = fopen(path, "r");
25 if (file != nullptr) {
26 // parse the file
27 fgets(ssid, 64, file);
28 char* sv = strchr(ssid, '\n');
29 if (sv != nullptr) *sv = '\0';
30 sv = strchr(ssid, '\r');
31 if (sv != nullptr) *sv = '\0';
32 fgets(pass, 128, file);
33 fclose(file);
34 sv = strchr(pass, '\n');
35 if (sv != nullptr) *sv = '\0';
36 sv = strchr(pass, '\r');
37 if (sv != nullptr) *sv = '\0';
38 return true;
39 }
40 return false;
41}
42#ifdef SPI_PORT
43static void spi_init() {
44 spi_bus_config_t buscfg;
45 memset(&buscfg, 0, sizeof(buscfg));
46 buscfg.sclk_io_num = SPI_CLK;
47 buscfg.mosi_io_num = SPI_MOSI;
48 buscfg.miso_io_num = SPI_MISO;
49 buscfg.quadwp_io_num = -1;
50 buscfg.quadhd_io_num = -1;
51
52 buscfg.max_transfer_sz = 512 + 8;
53
54 // Initialize the SPI bus on VSPI (SPI3)
55 spi_bus_initialize(SPI_PORT, &buscfg, SPI_DMA_CH_AUTO);
56}
57
58#ifdef SD_CS
59static sdmmc_card_t* sd_card = nullptr;
60static bool sd_init() {
61 static const char mount_point[] = "/sdcard";
62 esp_vfs_fat_sdmmc_mount_config_t mount_config;
63 memset(&mount_config, 0, sizeof(mount_config));
64 mount_config.format_if_mount_failed = false;
65 mount_config.max_files = 5;
66 mount_config.allocation_unit_size = 0;
67
68 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
69 host.slot = SD_PORT;
70 // // This initializes the slot without card detect (CD) and write
71 // protect (WP)
72 // // signals.
73 sdspi_device_config_t slot_config;
74 memset(&slot_config, 0, sizeof(slot_config));
75 slot_config.host_id = (spi_host_device_t)SD_PORT;
76 slot_config.gpio_cs = (gpio_num_t)SD_CS;
77 slot_config.gpio_cd = SDSPI_SLOT_NO_CD;
78 slot_config.gpio_wp = SDSPI_SLOT_NO_WP;
79 slot_config.gpio_int = GPIO_NUM_NC;
80 if (ESP_OK != esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config,
81 &mount_config, &sd_card)) {
82 return false;
83 }
84 return true;
85}
86#endif
87#endif
88
89static void spiffs_init() {
90 esp_vfs_spiffs_conf_t conf;
91 memset(&conf, 0, sizeof(conf));
92 conf.base_path = "/spiffs";
93 conf.partition_label = NULL;
94 conf.max_files = 5;
95 conf.format_if_mount_failed = true;
96 if (ESP_OK != esp_vfs_spiffs_register(&conf)) {
97 puts("Unable to initialize SPIFFS");
98 while (1) vTaskDelay(5);
99 }
100}
101// example
102static void loop();
103static void loop_task(void* arg) {
104 uint32_t ts = pdTICKS_TO_MS(xTaskGetTickCount());
105 while (1) {
106 loop();
107 uint32_t ms = pdTICKS_TO_MS(xTaskGetTickCount());
108 if (ms > ts + 200) {
109 ms = pdTICKS_TO_MS(xTaskGetTickCount());
110 vTaskDelay(5);
111 }
112 }
113}
114#ifdef __cplusplus
115extern "C"
116#endif
117void app_main() {
118 printf("ESP-IDF version: %d.%d.%d\n", ESP_IDF_VERSION_MAJOR,
119 ESP_IDF_VERSION_MINOR, ESP_IDF_VERSION_PATCH);
120#ifdef SPI_PORT
121 spi_init(); // used by the SD reader
122#endif
123 bool loaded = false;
124 wifi_ssid[0] = 0;
125 wifi_pass[0] = 0;
126#ifdef SPI_PORT
127#ifdef SD_CS
128 if (sd_init()) {
129 puts("SD card found, looking for wifi.txt creds");
130 loaded = wifi_load("/sdcard/wifi.txt", wifi_ssid, wifi_pass);
131 }
132#endif
133#endif
134 if (!loaded) {
135 spiffs_init();
136 puts("Looking for wifi.txt creds on internal flash");
137 loaded = wifi_load("/spiffs/wifi.txt", wifi_ssid, wifi_pass);
138 }
139 if (loaded) {
140 printf("Initializing WiFi connection to %s\n", wifi_ssid);
141 wifi_init(wifi_ssid, wifi_pass);
142 }
143
144 TaskHandle_t loop_handle;
145 xTaskCreate(loop_task, "loop_task", 4096, nullptr, 10, &loop_handle);
146 printf("Free SRAM: %0.2fKB\n", esp_get_free_internal_heap_size() / 1024.f);
147}
148static void loop() {
149 // do work
150}
151