Added Wifi

Seems to be working.

It's 90% the same code as the examples
This commit is contained in:
2023-10-28 00:58:33 +01:00
parent e8c1dc0647
commit cf9a549cf2
6 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
idf_component_register(SRCS "wifi.c"
INCLUDE_DIRS "."
REQUIRES driver esp_wifi
)

View File

@@ -0,0 +1,14 @@
menu "WiFi"
config WIFI_SSID
string "WiFi SSID"
default "VM8094728"
help
SSID (network name) for the example to connect to.
config WIFI_PASSWORD
string "WiFi Password"
default "tx3YrxvzMrbr"
help
WiFi password (WPA or WPA2) for the example to use.
endmenu

61
components/wifi/wifi.c Normal file
View File

@@ -0,0 +1,61 @@
#include <string.h>
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
static const char* TAG = "WIFI";
static void event_handler(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(TAG,"connect to the AP fail - trying to reconnect");
}
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(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
}
}
void start_wifi() {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASSWORD,
.failure_retry_cnt = 20,
// TODO Figure out what I actually removed here
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
}

3
components/wifi/wifi.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void start_wifi();