Built-in project · ESP32-C3-DevKitM-1

WiFi: join the network, on an ESP32-C3

The connect loop every ESP32 tutorial opens with, on a simulated network: no 802.11, and the address a simulated DHCP hands out. The whole build, an ESP32-C3, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $13. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Joining WiFi with WiFi.begin()
  • Connection status codes
  • Waiting for a result with a timeout
  • Reading the IP address and signal strength
  • How a wrong password differs from a wrong network name
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: ESP32-C3-DevKitM-1
  • Parts: 2

How it works

This sketch joins a WiFi network the way most ESP32 tutorials begin. In Mokxi the radio is simulated: there is no real 802.11. The network name and password are settings on the board, and the sketch uses Mokxi-Lab and breadboard.

It sets station mode, starts the connection and prints a dot every 250 ms while waiting, for up to 12 seconds. When it ends, it prints the status. If connected, it also prints how long it took, the IP address, the gateway, the RSSI and the MAC address.

Parts list

2 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP32-C3-DevKitM-1
/*
 * WiFi: join the network.
 *
 * The radio here is SIMULATED. There is no 802.11, no real access point and no
 * real internet: the network this joins is Mokxi's own, and the SSID and
 * password it wants are properties of the board (click the board, look for
 * wifiSsid and wifiPassword). Everything else (the calls, the status codes,
 * the one to three seconds it takes) is what an ESP32-C3 on a desk does.
 *
 * Try it wrong on purpose: change the password below and watch WL_CONNECT_FAILED
 * arrive five seconds later, which is what a real board does too.
 */
#include <WiFi.h>

const char *SSID = "Mokxi-Lab";
const char *PASSWORD = "breadboard";

void printStatus(wl_status_t status) {
  switch (status) {
    case WL_CONNECTED: Serial.println("connected"); break;
    case WL_CONNECT_FAILED: Serial.println("connect failed: wrong password"); break;
    case WL_NO_SSID_AVAIL: Serial.println("no network with that name"); break;
    case WL_DISCONNECTED: Serial.println("still connecting"); break;
    default: Serial.println("idle"); break;
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("WiFi on Mokxi: the radio is simulated, there is no 802.11.");
  Serial.print("joining ");
  Serial.println(SSID);

  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PASSWORD);

  unsigned long started = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - started < 12000) {
    Serial.print(".");
    delay(250);
    if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL) break;
  }
  Serial.println();

  Serial.print("status: ");
  printStatus(WiFi.status());

  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("took ");
    Serial.print((long)(millis() - started));
    Serial.println(" ms");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.print("gateway: ");
    Serial.println(WiFi.gatewayIP());
    Serial.print("RSSI: ");
    Serial.print(WiFi.RSSI());
    Serial.println(" dBm (a setting on the board, not a measurement)");
    Serial.print("MAC: ");
    Serial.println(WiFi.macAddress());
  }
}

void loop() {
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

printStatus sketch.ino

Turns a status code into a plain message with a switch. Connected prints connected. A failed connection says wrong password, no SSID says no network with that name, and disconnected says still connecting. Anything else says idle.

void printStatus(wl_status_t status) {
  switch (status) {
    case WL_CONNECTED: Serial.println("connected"); break;
    case WL_CONNECT_FAILED: Serial.println("connect failed: wrong password"); break;
    case WL_NO_SSID_AVAIL: Serial.println("no network with that name"); break;
    case WL_DISCONNECTED: Serial.println("still connecting"); break;
    default: Serial.println("idle"); break;
  }
}

setup sketch.ino

Sets station mode and calls WiFi.begin(SSID, PASSWORD). A loop prints dots every 250 ms until connected, 12 seconds pass, or a failure code appears. Then it prints the status and, if connected, the time taken, IP, gateway, RSSI and MAC.

void setup() {
  Serial.begin(115200);
  Serial.println("WiFi on Mokxi: the radio is simulated, there is no 802.11.");
  Serial.print("joining ");
  Serial.println(SSID);

  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PASSWORD);

  unsigned long started = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - started < 12000) {
    Serial.print(".");
    delay(250);
    if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL) break;
  }
  Serial.println();

  Serial.print("status: ");
  printStatus(WiFi.status());

  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("took ");
    Serial.print((long)(millis() - started));
    Serial.println(" ms");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.print("gateway: ");
    Serial.println(WiFi.gatewayIP());
  // ... 7 more lines in the editor

loop sketch.ino

Only waits one second, over and over. All the work happens once in setup.

void loop() {
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The status says connect failed: wrong password This project
    Make PASSWORD match the password set on the board.
  • The status says no network with that name This project
    Make SSID match the network name set on the board.
  • Upload fails or the board is not found by the computer. ESP32-C3-DevKitM-1
    Pick the ESP32-C3 board in your tools and the right port. If it still fails, hold BOOT, tap RESET, then release BOOT to enter download mode and try again.
  • GPIO pins act strangely or the board resets. ESP32-C3-DevKitM-1
    GPIO is 3.3 V only, so never feed it 5 V. GPIO 18 and 19 are USB, and GPIO 2, 8 and 9 are boot strapping pins. Avoid loading them at reset.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.