/*****************************************
* Program : Tes Koneksi WiFi NodeMCU V3
* Input : -
* Output : Serial Monitor
*****************************************/
#include <WiFi.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiServer server(80);
TaskHandle_t ledTaskHandle;
TaskHandle_t wifiTaskHandle;
void setup() {
Serial.begin(115200);
delay(1000);
// Setup pin 13 as output for LED blinking
pinMode(13, OUTPUT);
// Create an RTOS task for LED blinking
xTaskCreatePinnedToCore(
ledTask, // Task function
"LED Blink", // Name of the task
1000, // Stack size for the task
NULL, // Parameter passed to the task
1, // Task priority
&ledTaskHandle, // Task handle
0 // Run the task on core 0
);
// Create an RTOS task for WiFi connection
xTaskCreatePinnedToCore(
wifiTask, // Task function
"WiFi Connect", // Name of the task
4000, // Stack size for the task
NULL, // Parameter passed to the task
1, // Task priority
&wifiTaskHandle, // Task handle
1 // Run the task on core 1
);
}
// Task function for LED blinking
void ledTask(void *parameter) {
for (;;) {
digitalWrite(13, HIGH);
vTaskDelay(400 / portTICK_PERIOD_MS); // Delay of 1 second
digitalWrite(13, LOW);
vTaskDelay(400 / portTICK_PERIOD_MS); // Delay of 1 second
}
}
// Task function for WiFi connection
void wifiTask(void *parameter) {
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
vTaskDelay(500 / portTICK_PERIOD_MS); // Delay 500 ms in FreeRTOS
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Delete the task once WiFi is connected to free resources
vTaskDelete(NULL);
}
void loop() {
// Main loop can remain empty or handle other tasks if necessary
}