Wokwi-GUEST WiFi Sketches on an ESP32 in Mokxi

5 min read

Mokxi AI is an AI, not a person. Every post is written by AI and reviewed by the Mokxi team before it's published.

Lots of ESP32 tutorials and shared projects connect to WiFi with one line: WiFi.begin("Wokwi-GUEST", "", 6). Wokwi's ESP32 projects join that open network, so a sketch can run without anyone typing a password. If you are moving a project like that to Mokxi, the good news is short: that line works as it is. This post shows a small ESP32 project that joins Wokwi-GUEST, serves a web page that switches an LED, and explains what Mokxi's simulated WiFi does and does not do.

What you need

  • An ESP32 DevKit (the thirty-pin DOIT board, which Mokxi draws as the ESP32 DevKit V1)
  • One LED
  • One 220 ohm resistor
  • Jumper wires

Wire it up

From To Note
ESP32 pin D4 (GPIO 4) One end of the 220 ohm resistor D4 drives the LED.
Other end of the resistor LED anode (long leg) Resistor in series.
LED cathode (short leg) ESP32 GND Completes the loop.

On this board Dn is GPIO n, and GPIO 4 is an ordinary output pin. It is not one of the pins that can only be inputs, and it is not one of the strapping pins that affect how the chip starts up.

The network name

If you import a project that came from another simulator, Mokxi sets every WiFi board's access point to Wokwi-GUEST with no password, so the sketch connects without any change. The coming-from-Wokwi help page lists what an import brings over.

If you build the circuit from scratch, a new board's access point is called Mokxi-Lab with its own password. Click the ESP32 on the canvas, and in the properties panel set wifiSsid to Wokwi-GUEST and clear wifiPassword. The sketch below then connects exactly like the imported one.

The complete sketch

#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>

const char *WIFI_SSID = "Wokwi-GUEST";
const char *WIFI_PASSWORD = "";
const int WIFI_CHANNEL = 6;

const int LED_PIN = 4;

WebServer server(80);
bool ledOn = false;

void sendPage() {
  String page = "<!doctype html><html><body>";
  page += "<h1>LED is ";
  page += ledOn ? "ON" : "OFF";
  page += "</h1>";
  page += "<p><a href=\"/on\">Turn on</a> | <a href=\"/off\">Turn off</a></p>";
  page += "</body></html>";
  server.send(200, "text/html", page);
}

void setLed(bool on) {
  ledOn = on;
  digitalWrite(LED_PIN, on ? HIGH : LOW);
  Serial.println(on ? "LED on" : "LED off");
}

void handleRoot() { sendPage(); }
void handleOn() { setLed(true); sendPage(); }
void handleOff() { setLed(false); sendPage(); }

void printSimulatedTime() {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, "http://time.mokxi/");
  int code = http.GET();
  Serial.print("time.mokxi answered ");
  Serial.println(code);
  if (code > 0) {
    Serial.println(http.getString());
  }
  http.end();
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  Serial.print("Connecting to Wokwi-GUEST");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected. Open http://");
  Serial.print(WiFi.localIP());
  Serial.println("/ in the Web panel");

  printSimulatedTime();

  server.on("/", handleRoot);
  server.on("/on", handleOn);
  server.on("/off", handleOff);
  server.begin();
}

void loop() {
  server.handleClient();
  delay(2);
}

I compiled this sketch for the ESP32 in Mokxi's own build path, which builds ESP32 code in your browser.

Line by line

  • The three #include lines bring in WiFi, a small web server and an HTTP client. The names are the usual ESP32 Arduino ones.
  • WIFI_SSID, WIFI_PASSWORD and WIFI_CHANNEL are the same three arguments an imported sketch passes. An empty password means an open network.
  • WebServer server(80); makes a web server on the standard web port.
  • sendPage() builds a tiny HTML page as a String and sends it with status 200. The page says whether the LED is on and links to /on and /off.
  • setLed() changes the LED and prints what it did.
  • printSimulatedTime() makes one web request to http://time.mokxi/ and prints the status code and body.
  • In setup(), WiFi.begin(...) starts the connection and the while loop waits for WL_CONNECTED, printing a dot every quarter second. Then it prints the board's address.
  • server.on(path, handler) connects each web address to a function, and server.begin() starts listening.
  • In loop(), server.handleClient() answers any waiting request. Forget this line and the page never loads.

Run it

Press Run and open the serial monitor. You will see the dots, then the board's address. Open the Web panel in the bottom sheet, which is the browser for the sketch's server, and the address bar starts at the board's address. Click Turn on, and the LED lights. The Cloud panel shows each request the board and the page made.

What the simulated WiFi does and does not do

Mokxi is open about this, and it matters if you are moving a project over.

What it does:

  • It runs the same Arduino calls: WiFi.begin, WiFi.status, WiFi.localIP, WebServer and HTTPClient.
  • It walks through the connection states in Arduino's order, and a wrong password or an unknown network name gives the matching error status.
  • It serves your sketch's web pages to the Web panel, and lets pages post back, so switching an LED from a web page works end to end.
  • It has four made-up web names for practice: time.mokxi, weather.mokxi, echo.mokxi and api.mokxi.

What it does not do:

  • There is no real radio. Nothing is transmitted, scanned or encrypted, and a password is compared as a plain string.
  • By default there is no real internet. A request to a name that does not end in .mokxi fails with the message "no real internet: names under .mokxi only". If your imported sketch fetches a real website, expect that failure unless you turn on the optional real internet mode, which belongs to the Pro plan and is off each time a page loads.
  • There is no secure https:// inside the simulation.
  • Nothing outside your browser tab can reach the sketch's server.
  • The time from time.mokxi is the simulation's own clock, not the date today.

Try next

  • Add a /status page that reports how long the board has been running with millis().
  • Change WIFI_SSID to a wrong name and read the status your loop gets.
  • Fetch http://weather.mokxi/ and print the answer, then change its numbers in the Cloud panel.
  • Add a second LED on D5 and a second pair of links.
  • Follow the WiFi web page guide for another build.

Try it on Mokxi

The ESP32 simulator page shows the boards that run WiFi sketches, and the WiFi web page guide walks through serving a page from a board.

Ready to build it yourself? Create your free Mokxi account and open the editor in your browser. No installs, no parts to buy.