Wifi connection is set up

This commit is contained in:
Pascal Serrarens 2025-04-03 09:40:59 +02:00
parent 0c826d24be
commit a5cd5c89d3
2 changed files with 34 additions and 25 deletions

View File

@ -3,8 +3,11 @@
#if defined(IDF_VER) #if defined(IDF_VER)
#include <iostream> #include <iostream>
// #include "esp_event.h" // #include "esp_event.h"
// #include "esp_log.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_wifi.h" #include "esp_wifi.h"
// #include "lwip/inet.h"
// #include "lwip/ip_addr.h"
#include "string.h" #include "string.h"
const char* hotspotSSID = "Roboid"; const char* hotspotSSID = "Roboid";
@ -12,7 +15,7 @@ const char* hotspotPassword = "alchemy7000";
esp_netif_t* wifi_netif = nullptr; esp_netif_t* wifi_netif = nullptr;
// Semaphore to signal Wi-Fi connection status // Semaphore to signal Wi-Fi connection status
SemaphoreHandle_t wifi_semaphore; // SemaphoreHandle_t wifi_semaphore;
static bool wifi_connected = false; static bool wifi_connected = false;
static void wifi_event_handler(void* arg, static void wifi_event_handler(void* arg,
@ -28,22 +31,13 @@ static void wifi_event_handler(void* arg,
if (event_id == IP_EVENT_STA_GOT_IP) { if (event_id == IP_EVENT_STA_GOT_IP) {
// ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data; // ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
// const char* ipaddr = IP2STR(&event->ip_info.ip); // const char* ipaddr = IP2STR(&event->ip_info.ip);
std::cout << "Wifi connected, IP address (tbd)\n";
wifi_connected = true; wifi_connected = true;
xSemaphoreGive(wifi_semaphore); // Signal that connection is established // xSemaphoreGive(wifi_semaphore); // Signal that connection is
// established
} }
} }
} }
// void log_dots_task(void* pvParameters) {
// while (!wifi_connected) {
// printf("."); // Log a dot
// vTaskDelay(500 / portTICK_PERIOD_MS); // Wait 500ms
// }
// printf("\nWi-Fi connected!\n");
// vTaskDelete(NULL);
// }
bool StartWifi(const char* wifiSsid, const char* wifiPassword) { bool StartWifi(const char* wifiSsid, const char* wifiPassword) {
std::cout << "Connecting to WiFi " << wifiSsid << "\n"; std::cout << "Connecting to WiFi " << wifiSsid << "\n";
@ -59,11 +53,10 @@ bool StartWifi(const char* wifiSsid, const char* wifiPassword) {
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler,
NULL); NULL);
wifi_config_t wifi_config = {.sta = {.bssid_set = false}}; wifi_config_t wifi_config = {};
// Copy the ssid in the config strncpy((char*)wifi_config.sta.ssid, wifiSsid, strlen(wifiSsid) + 1);
strncpy((char*)wifi_config.sta.ssid, wifiSsid, sizeof(wifiSsid)); strncpy((char*)wifi_config.sta.password, wifiPassword,
// Copy the wifiPassword in the config strlen(wifiPassword) + 1);
strncpy((char*)wifi_config.sta.password, wifiPassword, sizeof(wifiPassword));
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
@ -75,13 +68,30 @@ bool StartWifi(const char* wifiSsid, const char* wifiPassword) {
for (int i = 0; i < 20; i++) { // 20 iterations, each 500ms for (int i = 0; i < 20; i++) { // 20 iterations, each 500ms
if (wifi_connected) { if (wifi_connected) {
success = true; success = true;
std::cout << " Connected.\n";
break; break;
} }
printf("."); // Log a dot std::cout << ".";
fflush(stdout); // Ensure output is printed immediately
vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(500)); // Wait 500ms vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(500)); // Wait 500ms
} }
if (!wifi_connected) if (wifi_connected) {
esp_netif_ip_info_t ip_info = {};
esp_netif_t* esp_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
// Get IP information (IP address, netmask, gateway)
if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK) {
std::cout << "Failed to get IP info\n";
return false;
}
// Convert the IP address to string format using inet_ntoa
char ip_str[16]; // IPv4 address can have a max of 15 characters + null
// terminator
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&ip_info.ip));
std::cout << "IP address = " << ip_str << "\n";
} else
std::cout << "\nCould not connect to home network.\n"; std::cout << "\nCould not connect to home network.\n";
return success; return success;

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#if defined(IDF_VER) #if defined(IDF_VER)
bool StartWifi(const char *wifiSsid, const char *wifiPassword, bool StartWifi(const char *wifiSsid, const char *wifiPassword);
bool hotspotFallback = false);
#endif #endif