My IoT module is Realtek 8720DN, which uses the Ameba Arduino SDK. The startup time of Wifi.begin() is 3-10 seconds, and I need to wait for this function to complete.
I would like to ask if there is a way to execute Wifi.begin() asynchronously? Or how to rewrite it?
How about use the FreeRTOS to create a task?
like bellow:
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
// WiFi credentials
const char* ssid = "yourSSID";
const char* password = "yourPASSWORD";
// Task Handle
TaskHandle_t TaskHandle_WiFiConnect;
void WiFiConnectTask(void *pvParameters) {
Serial.println("WiFi Connect Task Started");
WiFi.begin(ssid, password);
// Wait for connection or timeout
for (int i = 0; i < 30; ++i) { // 30 attempts, ~3-10 seconds
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi!");
break;
}
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait for 1 second
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect to WiFi. Please check your settings.");
}
vTaskDelete(NULL); // Delete this task if it's no longer needed
}
void setup() {
Serial.begin(115200);
// Initialize WiFi (if necessary)
// Create WiFi Connection Task
xTaskCreate(
WiFiConnectTask, /* Task function */
"WiFi Connect", /* Name of task */
2048, /* Stack size */
NULL, /* Task input parameter */
1, /* Priority of the task */
&TaskHandle_WiFiConnect); /* Task handle */
}
void loop() {
// Your main code here, the loop can perform other tasks while waiting for WiFi to connect
}