· 6 months ago · Apr 04, 2025, 07:40 PM
1// PUT wifi.txt ON SPIFFS
2// FIRST LINE IS SSID
3// NEXT LINE IS PASSWORD
4#include <stddef.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <string.h>
8#include <sys/stat.h>
9#include <sys/unistd.h>
10
11#include "esp_event.h"
12#include "esp_http_client.h"
13#include "esp_idf_version.h"
14#include "esp_netif.h"
15#include "esp_spiffs.h"
16#include "esp_system.h"
17#include "esp_tls.h"
18#include "esp_wifi.h"
19#include "freertos/FreeRTOS.h"
20#include "freertos/event_groups.h"
21#include "freertos/task.h"
22#include "lwip/dns.h"
23#include "lwip/inet.h"
24#include "lwip/ip4_addr.h"
25#include "nvs_flash.h"
26
27#define WIFI_CONNECTED_BIT BIT0
28#define WIFI_FAIL_BIT BIT1
29
30EventGroupHandle_t wifi_event_group = NULL;
31size_t wifi_retry_count = 0;
32
33esp_err_t http_event_handler(esp_http_client_event_t* evt) {
34 switch (evt->event_id) {
35 case HTTP_EVENT_ERROR:
36 break;
37 case HTTP_EVENT_ON_CONNECTED:
38 break;
39 case HTTP_EVENT_HEADER_SENT:
40 break;
41 case HTTP_EVENT_ON_HEADER:
42 break;
43 case HTTP_EVENT_ON_DATA:
44 if (evt->user_data) {
45 FILE* f = (FILE*)evt->user_data;
46 // The last byte in evt->user_data is kept for the NULL
47 // character in case of out-of-bound access.
48 fwrite(evt->data, 1, evt->data_len, f);
49 }
50 break;
51 case HTTP_EVENT_ON_FINISH:
52 if (evt->user_data) {
53 fclose((FILE*)evt->user_data);
54 puts("Download finished");
55 }
56 break;
57 case HTTP_EVENT_DISCONNECTED:
58 int mbedtls_err = 0;
59 esp_tls_get_and_clear_last_error(
60 (esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL);
61 break;
62 case HTTP_EVENT_REDIRECT:
63 esp_http_client_set_header(evt->client, "From", "user@example.com");
64 esp_http_client_set_header(evt->client, "Accept", "text/html");
65 esp_http_client_set_redirection(evt->client);
66 break;
67 }
68 return ESP_OK;
69}
70void wifi_event_handler(void* arg, esp_event_base_t event_base,
71 int32_t event_id, void* event_data) {
72 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
73 esp_wifi_connect();
74 } else if (event_base == WIFI_EVENT &&
75 event_id == WIFI_EVENT_STA_DISCONNECTED) {
76 if (wifi_retry_count < 3) {
77 esp_wifi_connect();
78 ++wifi_retry_count;
79 } else {
80 xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
81 }
82 } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
83 wifi_retry_count = 0;
84 xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
85 }
86}
87static void http_download(const char* url, FILE* file) {
88 esp_http_client_config_t config = {
89 .url = url, .event_handler = http_event_handler, .user_data = file};
90 esp_http_client_handle_t client = esp_http_client_init(&config);
91 ESP_ERROR_CHECK(esp_http_client_perform(client));
92
93 esp_http_client_cleanup(client);
94}
95
96void app_main() {
97
98 char ssid[65];
99 ssid[0] = 0;
100 char pass[129];
101 pass[0] = 0;
102
103 esp_vfs_spiffs_conf_t conf = {.base_path = "/spiffs",
104 .partition_label = NULL,
105 .max_files = 5,
106 .format_if_mount_failed = true};
107 ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
108 puts("Looking for /spiffs/wifi.txt");
109 FILE* file = fopen("/spiffs/wifi.txt", "r");
110 int loaded = 0;
111 if (file != NULL) {
112 // parse the file
113 fgets(ssid, sizeof(ssid), file);
114 char* sv = strchr(ssid, '\n');
115 if (sv != NULL) *sv = '\0';
116 sv = strchr(ssid, '\r');
117 if (sv != NULL) *sv = '\0';
118 fgets(pass, sizeof(pass), file);
119 fclose(file);
120 file = NULL;
121 sv = strchr(pass, '\n');
122 if (sv != NULL) *sv = '\0';
123 sv = strchr(pass, '\r');
124 if (sv != NULL) *sv = '\0';
125 loaded = true;
126 }
127 if(!loaded) {
128 puts("Put a wifi.txt file in SPIFFS. First line is SSID. Next line is password.");
129 while(1) vTaskDelay(5);
130 }
131 nvs_flash_init();
132 wifi_event_group = xEventGroupCreate();
133
134 ESP_ERROR_CHECK(esp_netif_init());
135
136 ESP_ERROR_CHECK(esp_event_loop_create_default());
137 esp_netif_create_default_wifi_sta();
138
139 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
140 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
141
142 esp_event_handler_instance_t instance_any_id;
143 esp_event_handler_instance_t instance_got_ip;
144 ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
145 ESP_EVENT_ANY_ID,
146 &wifi_event_handler,
147 NULL,
148 &instance_any_id));
149 ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
150 IP_EVENT_STA_GOT_IP,
151 &wifi_event_handler,
152 NULL,
153 &instance_got_ip));
154
155 wifi_config_t wifi_config;
156 memset(&wifi_config,0,sizeof(wifi_config));
157 memcpy(wifi_config.sta.ssid,ssid,strlen(ssid)+1);
158 memcpy(wifi_config.sta.password,pass,strlen(pass)+1);
159 wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
160 wifi_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
161 wifi_config.sta.sae_h2e_identifier[0]=0;
162 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
163 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
164 ESP_ERROR_CHECK(esp_wifi_start() );
165 while(1) {
166 EventBits_t bits = xEventGroupGetBits(wifi_event_group)&(WIFI_CONNECTED_BIT|WIFI_FAIL_BIT);
167 if(bits==WIFI_CONNECTED_BIT) {
168 puts("Connected");
169 break;
170 }
171 if(bits==WIFI_FAIL_BIT) {
172 puts("Unable to connect");
173 while(1) vTaskDelay(5);
174 }
175 }
176 static const char* url =
177 "http://www.google.com/images/branding/googlelogo/1x/"
178 "googlelogo_light_color_272x92dp.png";
179 file = fopen("/spiffs/download.png", "wb");
180 if (file == NULL) {
181 puts("Unable to create download file");
182 while (1) vTaskDelay(5);
183 }
184 http_download(url, file);
185 file = fopen("/spiffs/download.png","rb");
186 fseek(file,0,SEEK_END);
187 size_t len = ftell(file);
188 fclose(file);
189 printf("download.png is %0.2fKB\n",((float)len)/1024.f);
190}