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() { }
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