ESP32 WiFi.h Constantly Blinking Onboard LED

LordFly
Posts: 1
Joined: Sat Jul 08, 2023 6:16 pm

ESP32 WiFi.h Constantly Blinking Onboard LED

Postby LordFly » Sat Jul 08, 2023 6:21 pm

I have a project that has been working for years. ESP32 connected to Adafruit that controls the raise/lower buttons for my projector screen. It works great, I love it. But last time I needed to make a minor code change, I updated everything and something has now decided that the onboard LED (which I was previously using for various status indicators) should flash like a crazy little bastard whenever it's connected to my wifi. It's super annoying trying to watch a movie with a little blue light show going off right beside it :evil:

I've tried downgrading everything (libraries, board managers, IDE software) to older versions, but none of it is doing it. I've tried going through the WiFi.h code to find what is using the onboard LED, but I'm not good enough with the code to find it. All I've been able to determine is that it doesn't happen until I give the WiFi.begin() command. If anyone knows a way to stop the WiFi.h library from overriding my pin 2 control, you will forever be my hero!

As a last resort, I might remove the onboard LED and wire it to a separate pin, but I'd REALLY like to avoid that if possible, since I'm certain this can be fixed in software.

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 WiFi.h Constantly Blinking Onboard LED

Postby ESP_Sprite » Sun Jul 09, 2023 1:29 am

What hardware do you have, as in do you have any idea what that LED is connected to?

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

Re: ESP32 WiFi.h Constantly Blinking Onboard LED

Postby jhsrennie » Wed Aug 07, 2024 3:58 pm

A rather late reply, but I have a similar problem. I haven't found any way to stop WiFi.begin() from messing with the builtin LED, but I managed to get the WiFi enabled using just the ESP API. This code enables the WiFi without affecting the built in LED:

Code: Select all

#include <WiFi.h>
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "esp_event.h"

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

// *****************************************************
// 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");
}

// *****************************************************
// Setup and loop
// *****************************************************

void setup() {
  Serial.begin(115200);
  Serial.println("Starting WiFi test");

  wifi_init_sta();
  Serial.println("WiFi connection done");
}

void loop() {
  // Get status using ESP-IDF API
  wifi_ap_record_t ap;
  esp_wifi_sta_get_ap_info(&ap);
  Serial.printf("WiFi signal = %d\n", ap.rssi);

  delay(10000);
}

Who is online

Users browsing this forum: No registered users and 162 guests