Built in LED blinks when using WiFi.h

jhsrennie
Posts: 10
Joined: Sat Jul 13, 2024 6:16 pm

Built in LED blinks when using WiFi.h

Postby jhsrennie » Tue Aug 06, 2024 9:42 am

I have a generic ESP32-WROOM-32D dev board that I'm using to learn ESP32 programming using the Arduino IDE. I've noticed that when I use the WiFi object from WiFi.h the built in LED illuminates to show the state of the connection. When I call WiFi.begin() the LED turns on solid, then once the WiFi connects to my access point the LED starts flashing rapidly.

I just assumed this was a "feature", but then I tried using the Espressif ESP-IDF with VSCode instead of the Arduino IDE, and when I use the WiFi example provided the built in LED does not illuminate. So there must be something in the Arduino WiFi object code that is controlling the LED.

My question is can I change some setting in the WiFi object to determine whether or not the built in LED is used to show the status of the connection? I have tried Googling this but with no luck.

I did find the question ESP32 WiFi.h Constantly Blinking Onboard LED on this site:

https://esp32.com/viewtopic.php?f=2&t=3 ... 1f0021f454

but the question has no solution. As a last resort if anyone can point me to the code for the WiFi object I can go through it to look for anything that is changing the built in LED.

If it's relevant I can post the code I'm using in the Aduino and ESP-IDF but I don't think it's useful as it's just the template code provided with the dev environments.

aliarifat794
Posts: 200
Joined: Sun Jun 23, 2024 6:18 pm

Re: Built in LED blinks when using WiFi.h

Postby aliarifat794 » Wed Aug 07, 2024 1:53 pm

You can try this code to control the built-in LED according to the wifi status.

Code: Select all


#include <WiFi.h>

const int ledPin = 2; // GPIO2 is usually the built-in LED

void setup() {
    pinMode(ledPin, OUTPUT);
    WiFi.begin("your_SSID", "your_PASSWORD");

    // Add your own logic to control the LED
}

void loop() {
    if (WiFi.status() == WL_CONNECTED) {
        digitalWrite(ledPin, HIGH); // Turn on the LED when connected
    } else {
        digitalWrite(ledPin, LOW); // Turn off the LED when not connected
    }

    // Other code...
}
If you need help about ESP32-WROOM board installation in Arduino IDE, you can see this: https://www.theengineeringprojects.com/ ... eries.html

jhsrennie
Posts: 10
Joined: Sat Jul 13, 2024 6:16 pm

Re: Built in LED blinks when using WiFi.h

Postby jhsrennie » Wed Aug 07, 2024 2:56 pm

Thanks, but that is not what I'm asking.

It seems like the WiFi object already does does something similar to what you suggest. If I turn on the WiFi using:

Code: Select all

void connectWiFi() {
  WiFi.begin(MY_SSID, MY_PWD, 6);
  int loopcnt = 0;
  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf("%d WiFi status = %d, signal = %d\n", loopcnt++, WiFi.status(), WiFi.RSSI());
    delay(1000);
  }
  Serial.println("Connected to the Wi-Fi network");
  Serial.print("IP address = ");
  Serial.println(WiFi.localIP());
}
Then the builtin LED turns on solid as soon as I call WiFi.begin() then it starts flashing rapidly as soon as the WiFi connects and gets an IP address. I want the WiFi.begin() function to not do anything with the builtin LED.

If I use the IDF code instead of the WiFi object the WiFi works and does not flash the LED, so the flashing of the LED is not inherently connected to the operation of the WiFi. The code in WiFi.begin() must be explicitly turning the LED on.

This is the (rather longer!) IDF code that I tested. This code enables the WiFi but with no flashing of the builtin LED:

Code: Select all

// *****************************************************
// ESP-IDF network code
// *****************************************************

#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_BOTH
#define EXAMPLE_H2E_IDENTIFIER ""
#define WIFI_MAXIMUM_RETRY  5

// Event group to monitor the connection status
EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT      BIT1

static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  // Number of connect retries
  static int s_retry_num = 0;

  // WiFi starting
  if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
    esp_wifi_connect();
  }
  // WiFi didn't connect so we need to try again
  else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
    if (s_retry_num < WIFI_MAXIMUM_RETRY) {
      esp_wifi_connect();
      s_retry_num++;
      Serial.printf("Connection failed, retrying ...\n");
    }
    else {
      xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
    }
    Serial.printf("Too many retries\n");
  }
  // Connected and got IP address
  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;
    Serial.printf("IP address =  %d.%d.%d.%d\n", IP2STR(&(event->ip_info.ip)));
    xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
    s_retry_num = 0;
  }
}

void wifi_init_sta(void)
{
  s_wifi_event_group = xEventGroupCreate();

  // We'll assume these functions succeed
  esp_netif_init();
  // Create the default event loop so we can use it for WiFi notifications
  esp_event_loop_create_default();
  // Create the WiFi station
  esp_netif_create_default_wifi_sta();
  // Initialise the WiFi using the default settings
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  esp_wifi_init(&cfg);

  // Register an event handler for all WiFi events
  esp_event_handler_instance_t instance_any_id;
  esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id);

  // Register an event handle for the "got IP address" event
  esp_event_handler_instance_t instance_got_ip;
  esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip);

  // Initialise the config
  wifi_config_t wifi_config = {
    .sta = {
      .ssid = MY_SSID,
      .password = MY_PWD,
      .threshold = {.rssi = 0, .authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD},
      .sae_pwe_h2e = ESP_WIFI_SAE_MODE,
      .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER,
    },
  };

  // Set the configuration and start the WiFi
  esp_wifi_set_mode(WIFI_MODE_STA);
  esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
  esp_wifi_start();

  /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
  EventBits_t bits = xEventGroupWaitBits(
    s_wifi_event_group,
    WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
    pdFALSE,
    pdFALSE,
    portMAX_DELAY);

  // Check the bits to see what happened
  if (bits & WIFI_CONNECTED_BIT)
    Serial.printf("Connected to the Wi-Fi network SSID:%s\n", MY_SSID);
  else if (bits & WIFI_FAIL_BIT)
    Serial.printf("Failed to connect to SSID:%s\n", MY_SSID);
  else
    Serial.printf("UNEXPECTED EVENT\n");
}

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: Built in LED blinks when using WiFi.h

Postby lbernstone » Wed Aug 07, 2024 4:52 pm

What version of arduino-esp32 are you running? AFAIK, there is no code to blink the LED during connection. Does it do this if you don't have the print statements in there (maybe the led is connected to the serial line rather than wifi)?

jhsrennie
Posts: 10
Joined: Sat Jul 13, 2024 6:16 pm

Re: Built in LED blinks when using WiFi.h

Postby jhsrennie » Thu Aug 08, 2024 5:01 am

Thanks, but the LED flashes in the same way even if I use this program:

Code: Select all

#include <WiFi.h>

#define MY_SSID "foo"
#define MY_PWD  "bar"

void setup() {
  WiFi.begin(MY_SSID, MY_PWD);
}

void loop() {
    delay(1000);
}
I have uploaded a video to show how the LED starts flashing as soon as WiFi.begin() is called:
https://www.youtube.com/watch?v=-DX8NYNSpMk

This is clearly some form of diagnostics because if I disable my wireless access point the LED immediately stops flashing and shows a solid blue. Then when I re-enable the WAP the LED starts flashing again after a few seconds. So somewhere some code in the WiFi class is using the LED to indicate the status of the connection. This is software not hardware because if I use the ESP-IDF code shown above the LED does not flash.

I'm using the Arduino IDE v2.3.2 and the board is "esp32 by Espressif Systems" version 3.0.4

ESP32_WROOM_32D
Posts: 1
Joined: Sun Nov 10, 2024 10:09 pm

Re: Built in LED blinks when using WiFi.h

Postby ESP32_WROOM_32D » Sun Nov 10, 2024 10:14 pm

I found a solution on a youtube comment to solve the blue led flickering:
Switch the board from ESP32-WROOM-DA to ESP32 Dev Module

In Arduino IDE
Tools > Board > Esp32 > ESP32 Dev Module

Nicola31
Posts: 1
Joined: Sat Nov 23, 2024 9:15 am

Re: Built in LED blinks when using WiFi.h

Postby Nicola31 » Sat Nov 23, 2024 9:35 am

ESP32_WROOM_32D wrote:
Sun Nov 10, 2024 10:14 pm
I found a solution on a youtube comment to solve the blue led flickering:
Switch the board from ESP32-WROOM-DA to ESP32 Dev Module

In Arduino IDE
Tools > Board > Esp32 > ESP32 Dev Module
Changing board settings from ESP32-WROOM-DA is an ESP32 Dev Module that can solve the problem of blinking blue LEDs. This will adjust your configuration more closely to the specific modules you use. This is a useful tip to ensure proper functionality. and avoid normal connection or power Related errors

Who is online

Users browsing this forum: No registered users and 79 guests