Wifi connection is set up
This commit is contained in:
parent
0c826d24be
commit
a5cd5c89d3
@ -3,8 +3,11 @@
|
||||
#if defined(IDF_VER)
|
||||
#include <iostream>
|
||||
// #include "esp_event.h"
|
||||
// #include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_wifi.h"
|
||||
// #include "lwip/inet.h"
|
||||
// #include "lwip/ip_addr.h"
|
||||
#include "string.h"
|
||||
|
||||
const char* hotspotSSID = "Roboid";
|
||||
@ -12,7 +15,7 @@ const char* hotspotPassword = "alchemy7000";
|
||||
|
||||
esp_netif_t* wifi_netif = nullptr;
|
||||
// Semaphore to signal Wi-Fi connection status
|
||||
SemaphoreHandle_t wifi_semaphore;
|
||||
// SemaphoreHandle_t wifi_semaphore;
|
||||
static bool wifi_connected = false;
|
||||
|
||||
static void wifi_event_handler(void* arg,
|
||||
@ -26,26 +29,17 @@ static void wifi_event_handler(void* arg,
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == IP_EVENT) {
|
||||
if (event_id == IP_EVENT_STA_GOT_IP) {
|
||||
//ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
|
||||
//const char* ipaddr = IP2STR(&event->ip_info.ip);
|
||||
std::cout << "Wifi connected, IP address (tbd)\n";
|
||||
// ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
|
||||
// const char* ipaddr = IP2STR(&event->ip_info.ip);
|
||||
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) {
|
||||
std::cout << "Connecting to WiFi" << wifiSsid << "\n";
|
||||
std::cout << "Connecting to WiFi " << wifiSsid << "\n";
|
||||
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
@ -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,
|
||||
NULL);
|
||||
|
||||
wifi_config_t wifi_config = {.sta = {.bssid_set = false}};
|
||||
// Copy the ssid in the config
|
||||
strncpy((char*)wifi_config.sta.ssid, wifiSsid, sizeof(wifiSsid));
|
||||
// Copy the wifiPassword in the config
|
||||
strncpy((char*)wifi_config.sta.password, wifiPassword, sizeof(wifiPassword));
|
||||
wifi_config_t wifi_config = {};
|
||||
strncpy((char*)wifi_config.sta.ssid, wifiSsid, strlen(wifiSsid) + 1);
|
||||
strncpy((char*)wifi_config.sta.password, wifiPassword,
|
||||
strlen(wifiPassword) + 1);
|
||||
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
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
|
||||
if (wifi_connected) {
|
||||
success = true;
|
||||
std::cout << " Connected.\n";
|
||||
break;
|
||||
}
|
||||
printf("."); // Log a dot
|
||||
std::cout << ".";
|
||||
fflush(stdout); // Ensure output is printed immediately
|
||||
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";
|
||||
|
||||
return success;
|
||||
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#if defined(IDF_VER)
|
||||
|
||||
bool StartWifi(const char *wifiSsid, const char *wifiPassword,
|
||||
bool hotspotFallback = false);
|
||||
bool StartWifi(const char *wifiSsid, const char *wifiPassword);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user