Wifi connection on xiao esp32c6 takes >10 minutes

hello!

been working with the XIAO esp32-c6 board to establish MQTT communication between the XIAO and a Raspberry Pi 5 but connecting to wifi alone takes nearly 10 minutes to be successful. i’ve attached my code in hopes of finding some clarity on what I’m doing wrong:

#include <PubSubClient.h>
#include <WiFi.h>

// network credentials
const char* ssid = "my ssid";
const char* password = "my password";

// MQTT credentials
const char *mqtt_broker = "[Raspberry Pi IP address]";
const int mqtt_port = 1883; 

#define LED D10
//#define SENSE A0
// bool ledState = false;
// subscribed topics
#define sub1 "output"

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

//connecting to WiFi

void initWiFi() {
  delay(10);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to WiFi ...");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(5000);
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("]");
  Serial.print(" Message: ");
  String messageTemp;

  if (strstr(topic, sub1)) {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();

    // switch on LED if a 1 was received as first character
    if ((char)payload[0] == 'f') {
      digitalWrite(LED, HIGH);
    }
    else {
      digitalWrite(LED, LOW);
    }
  }
  else {
    Serial.println("unsubcribed topic");
  }
}

// connecting to MQTT broker
void reconnect() {
  // loop until reconnected
  while (!client.connected()) {
    Serial.print("Attempting to connect to MQTT broker...");
    // create random client ID
    String clientID = "ESP32C6_Client-";
    clientID += String(random(0xffff), HEX);

    // attempt to connect
    if (client.connect(clientID.c_str())) {
      Serial.println("Connected!");
      client.subscribe(sub1);
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds ");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  initWiFi();
  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);
  pinMode(LED, OUTPUT);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

}
1 Like

Test with the standard sketch

It takes 10 minutes to compile for sure!

You may nave to use the external Antenna if your wifi router is not close

HI there,

A suggestion if I may, also is to try the WPS example in the ESP examples. I tested a known sketch with it and subbed in a Xiao ESP32C6 (no external antenna) just onboard one. It takes longer than the C3 in the same setup. My AP is out in the carport media closet over 100’ away.
The C3 connects in seconds and doesn’t throw any errors, (BSP 2.1.7) Get’s an IP address easy peasy.

The C6 connects in under 1 minutes but does show some errors with the handshaking (BSP 3.0.0 or 3.0.1) the serial output is posted below

Station Mode Started
Starting WPS
Disconnected from station, attempting reconnection
E (33194) wifi:sta is connecting, return error
E (33908) wifi:sta is connecting, return error
Disconnected from station, attempting reconnection
E (33909) wifi:sta is connecting, return error
WPS Successfull, stopping WPS and connecting to: 
Connected to :GlassSurf-2.4
Got IP: 192.168.1.172

It appears to me the BSP is having some effect on the connection, you can look up the Error’s,
I would roll back to the 3.0.0 and try you setup again.

HTH
GL :slight_smile: PJ
:v:

same setup with C3 , here’s the output , no errors and connects fast in seconds compared to C6

Station Mode Started
Starting WPS
Disconnected from station, attempting reconnection
WPS Successfull, stopping WPS and connecting to: 
Disconnected from station, attempting reconnection
Connected to :GlassSurf-2.4
Got IP: 192.168.1.167

i think the onboard antenna is a little weak… i got 3 routers detected, but a sensitive device will detect like 12 networks in my neighborhood… lots of spectrum routers still in setup mode… what da?

I doubt this is related to your issue, but I will mention this otherwise. I have just recently purchased a few units but haven’t done anything RF related just yet.

The XIAO ESP32C6 features an RF switch for selecting between the onboard antenna and the external RF port. The switch has two GPIO used for enabling the RF switch and selecting the RF output. These can be seen in the schematic - Page 5 section B2

XIAO ESP32 C6.pdf (seeedstudio.com)

I am not sure if these are switched ‘automatically’, but something to keep in mind if you have communication/reception issues. Also try assigning a static IP address and see if that helps.

1 Like

Regarding switching antennas, the following link may be helpful.
XIAO_ESP32C6 Switching between builtin and external antenna

1 Like

Yeah, to be honest, I am not a fan of chip antenna types. They are fine for testing on the bench and ‘short’ range applications. Take note of the KH5220-A36 datasheet, specifically the test PCB, specifically the size and distance (or rather lack of nearby components).

Most antennae datasheets provide ‘keep away’ zones of all metallic components to avoid interference losses. In the case of the XIAO ESP32C6, the antenna is located quite close to the metal shield casing; difficult, if not impossible to avoid when designing small devices. Fortunately, the external RF port is a good alternative with a suitable antenna. The RF switch is very useful as most other dev boards require a jumper resistor be to be resoldered to a different position which can be tricky and time consuming. This does come at a cost of extra energy consumption and losses but can be negligible depending on your use case.

It is also possible to adjust the RF TX power, this doesn’t help with RX sensitivity of the ESP, but can be useful provided that you don’t break any rules regarding transmit power/gain when including the antenna type, etc.

Yes static IP is also important

@zuleen27 Have you solved your problem?

If you try your code with version 3.0.4 of the ESP32 Arduino core released just a few days ago, it may very well work. Until that version, the RF switch was not enabled.

I have tried your initWifi() function and it connects within 6 seconds every time to an access point 15 metres away with two walls in between. That’s with the on-board ceramic antenna. A Wi-Fi connection cannot be established in a reasonable amount of time if the RF switch is disabled which was the default in EP32 v3.0.2 and v3.0.3.

By the way, with a much shorter delay in the while loop, a connection can be made within half a second.

Using an external antenna does not reduce the connection time very much, but it can improve the signal strength. Details here Connection Time vs Transmit Power if you are interested.

2 Likes

Thanks for the update!

Thank you all so much for the help! I think it might’ve just been an initial set-up issue; now it’s doing much better! I’ve attached the little antenna that came with a ESP32-S3 and it worked a little better.

1 Like