Connecting ESP32-C3 to WiFi

With the following code tested on two different ESP32-C3 boards, the boards did not connect to WiFi. How can that be resolved?

#include <WiFi.h>

#define SECRET_SSID "ssid"
#define SECRET_PASS "password"

char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status

WiFiClient client;

void setup() {
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  delay(3000);

  if (WiFi.status() == WL_IDLE_STATUS) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.print("You're connected to the network");
}

void loop() { }

Is your SSID and password correct?

Yes, both are correct

Can you connect to the same access point with a different device using the exact same credentials?

There was a slight error in the sketch above. This sketch works with an Uno R4 Wifi:

#include <WiFi.h>

#define SECRET_SSID "ssid"
#define SECRET_PASS "password"

char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status

WiFiClient client;

void setup() {
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  delay(3000);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.print("You're connected to the network");
}

void loop() { }

But, when compiling for the ESP32-C3, I get an error:

error: ‘WL_NO_MODULE’ was not declared in this scope
21 |   if (WiFi.status() == WL_NO_MODULE) {
|                        ^~~~~~~~~~~~
exit status 1

Compilation error: ‘WL_NO_MODULE’ was not declared in this scope

That is because the Wife-sketch example is not made for the ESP32 . It is MUCH better to use the ESP32 examples for WIFI as it has many more options and shows usage of those

Just above setup() include to get your current code compiled

#ifndef WL_NO_MODULE

#define WL_NO_MODULE WL_NO_SHIELD

#endif
1 Like

The sketch compiles, but the board still isn’t connecting. Could you provide an example sketch for a client side WiFi configuration?

There should be no reason why this would not work.

  • Are you sure (as in previous post) that the ssid and password are correct. It is sensitive to capitals.

  • Is the monitor set to the same speed as the sketch (9600)

  • check that network is available :In the ArduinoIDE :

  1. make sure you have selected the right ESP32 board.
  2. File→ Examples→ scroll to the examples for the ESP32 Board
  3. scroll down to WiFi
  4. Now select one of WifiScan
  • Alternative try WiFiClient client example to connect