fixes that made it work
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "esp_wifi_bsp.c"
|
||||
|
||||
PRIV_REQUIRES
|
||||
esp_event
|
||||
nvs_flash
|
||||
REQUIRES
|
||||
esp_wifi
|
||||
INCLUDE_DIRS "./")
|
||||
SRCS "esp_wifi_bsp.c"
|
||||
REQUIRES esp_wifi
|
||||
PRIV_REQUIRES esp_event nvs_flash
|
||||
INCLUDE_DIRS "./")
|
||||
|
||||
@@ -1,42 +1,113 @@
|
||||
#include "esp_wifi_bsp.h"
|
||||
#include "wifi_config.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_log.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
static const char *TAG = "wifi_sta";
|
||||
|
||||
void espwifi_Init(void) {
|
||||
nvs_flash_init(); // Initialize default NVS storage
|
||||
esp_netif_init(); // Initialize TCP/IP stack
|
||||
esp_event_loop_create_default(); // Create default event loop
|
||||
esp_netif_create_default_wifi_sta(); // Attach TCP/IP stack to default event loop
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); // Default config
|
||||
esp_wifi_init(&cfg); // Initialize Wi-Fi
|
||||
esp_event_handler_instance_t Instance_WIFI_IP;
|
||||
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &Instance_WIFI_IP);
|
||||
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &Instance_WIFI_IP);
|
||||
wifi_config_t wifi_config = {
|
||||
.sta =
|
||||
{
|
||||
.ssid = "K2P",
|
||||
.password = "1234567890",
|
||||
},
|
||||
};
|
||||
esp_wifi_set_mode(WIFI_MODE_STA); // Set mode to STA
|
||||
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); // Configure Wi-Fi
|
||||
esp_wifi_start(); // Start Wi-Fi
|
||||
}
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static char s_ip_str[20] = "N/A";
|
||||
static int s_retry_count = 0;
|
||||
#define MAX_RETRY 10
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
|
||||
if (event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
|
||||
char ip[25];
|
||||
uint32_t pxip = event->ip_info.ip.addr;
|
||||
sprintf(ip, "%d.%d.%d.%d", (uint8_t) (pxip), (uint8_t) (pxip >> 8), (uint8_t) (pxip >> 16), (uint8_t) (pxip >> 24));
|
||||
printf("IP: %s\n", ip);
|
||||
} else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
printf("disconnected\n");
|
||||
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT) {
|
||||
if (event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
ESP_LOGW(TAG, "Disconnected");
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_DISCONNECTED_BIT);
|
||||
strncpy(s_ip_str, "N/A", sizeof(s_ip_str));
|
||||
if (s_retry_count < MAX_RETRY) {
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
s_retry_count++;
|
||||
ESP_LOGI(TAG, "Retry %d/%d", s_retry_count, MAX_RETRY);
|
||||
esp_wifi_connect();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Max retries reached, will retry later");
|
||||
// Reset counter so background reconnect can try again
|
||||
vTaskDelay(pdMS_TO_TICKS(10000));
|
||||
s_retry_count = 0;
|
||||
esp_wifi_connect();
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
uint32_t ip = event->ip_info.ip.addr;
|
||||
snprintf(s_ip_str, sizeof(s_ip_str), "%d.%d.%d.%d",
|
||||
(uint8_t)(ip), (uint8_t)(ip >> 8),
|
||||
(uint8_t)(ip >> 16), (uint8_t)(ip >> 24));
|
||||
ESP_LOGI(TAG, "Connected: %s", s_ip_str);
|
||||
s_retry_count = 0;
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_DISCONNECTED_BIT);
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_sta_init(void)
|
||||
{
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
nvs_flash_erase();
|
||||
nvs_flash_init();
|
||||
}
|
||||
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&cfg);
|
||||
|
||||
esp_event_handler_instance_t inst_any;
|
||||
esp_event_handler_instance_t inst_got_ip;
|
||||
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler, NULL, &inst_any);
|
||||
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
&wifi_event_handler, NULL, &inst_got_ip);
|
||||
|
||||
wifi_config_t wifi_config = {};
|
||||
strncpy((char *)wifi_config.sta.ssid, WIFI_SSID, sizeof(wifi_config.sta.ssid));
|
||||
strncpy((char *)wifi_config.sta.password, WIFI_PASSWORD, sizeof(wifi_config.sta.password));
|
||||
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
esp_wifi_start();
|
||||
|
||||
ESP_LOGI(TAG, "WiFi STA init complete, connecting to %s", WIFI_SSID);
|
||||
}
|
||||
|
||||
bool wifi_sta_wait_connected(uint32_t timeout_ms)
|
||||
{
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT,
|
||||
pdFALSE, pdTRUE,
|
||||
pdMS_TO_TICKS(timeout_ms));
|
||||
return (bits & WIFI_CONNECTED_BIT) != 0;
|
||||
}
|
||||
|
||||
bool wifi_sta_is_connected(void)
|
||||
{
|
||||
EventBits_t bits = xEventGroupGetBits(s_wifi_event_group);
|
||||
return (bits & WIFI_CONNECTED_BIT) != 0;
|
||||
}
|
||||
|
||||
EventGroupHandle_t wifi_sta_get_event_group(void)
|
||||
{
|
||||
return s_wifi_event_group;
|
||||
}
|
||||
|
||||
void wifi_sta_get_ip_str(char *buf, size_t len)
|
||||
{
|
||||
strncpy(buf, s_ip_str, len);
|
||||
buf[len - 1] = '\0';
|
||||
}
|
||||
|
||||
@@ -1,11 +1,25 @@
|
||||
#ifndef ESP_WIFI_BSP_H
|
||||
#define ESP_WIFI_BSP_H
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_wifi.h" //WIFI
|
||||
|
||||
void espwifi_Init(void);
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_DISCONNECTED_BIT BIT1
|
||||
|
||||
void wifi_sta_init(void);
|
||||
bool wifi_sta_wait_connected(uint32_t timeout_ms);
|
||||
bool wifi_sta_is_connected(void);
|
||||
EventGroupHandle_t wifi_sta_get_event_group(void);
|
||||
void wifi_sta_get_ip_str(char *buf, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
8
components/esp_wifi_bsp/wifi_config.h
Normal file
8
components/esp_wifi_bsp/wifi_config.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define WIFI_CONFIG_H
|
||||
|
||||
#define WIFI_SSID "Novoyuuparosk_H3C"
|
||||
#define WIFI_PASSWORD "northwich"
|
||||
#define WS_SERVER_URI "ws://192.168.1.100:8765"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user