STANNUM
Open the simulator

Tutorials / Connectivity

Wi-Fi and the router

The ESP32 only connects if there is a router block with the right name and password — and then it reaches the real internet.

Parts in the example:
  • OLED display
  • LED
  • Wi-Fi router
The Wi-Fi router block as it appears on the canvas.

What it is

The ESP32 has Wi-Fi, and WiFi.begin(ssid, password) is where almost every connected project starts. On a bench, what happens next depends on the network around you: the name must exist, the password must match, and only then does DHCP hand out an address and the internet becomes reachable.

In the simulator

The Wi-Fi router block is that network. It has no pins — it is on the air — and its Settings are the router's: network name, password (empty for an open network), channel and whether the name is hidden. The block shows the name and “WPA2” or “open”. Without a router, or with the wrong name or password, the sketch does not connect, and the reason shows in the serial monitor with a [STANNUM] line explaining it. Connected, the firmware has a real network: HTTP, MQTT, NTP — whatever the code does, the bytes really travel.

Wiring

The circuit below is the example WiFi: fetch a page from the internet from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
Wi-Fi router pinGoes to

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the Wi-Fi router.
SettingAcceptsDefaultNotes
Network name (SSID)text (up to 32 characters)MyNetworkWhat WiFi.begin() must ask for, exactly — case matters.
Passwordtext (up to 63 characters)secret123Leave empty for an open network. 8 to 63 characters otherwise.
Channel1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9 · 10 · 11 · 12 · 136
VisibilityBroadcasts its name (normal) · Hidden networkBroadcasts its name (normal)A hidden network does not show up in scanNetworks(), but accepts connections with the right name and password.

Code

sketch.cpp
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 tela(128, 64, &Wire, -1);
const int LED = 2;

// These two must match the "Wi-Fi router" block in the circuit — name and
// password, exactly — or the connection never comes, as on a real board.
const char *REDE  = "MyNetwork";
const char *SENHA = "secret123";

void escrever(int linha, const String &texto) {
  tela.setCursor(0, linha * 10);
  tela.print(texto);
}

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  tela.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  tela.setTextSize(1);
  tela.setTextColor(SSD1306_WHITE);

  tela.clearDisplay();
  escrever(0, "connecting...");
  escrever(1, REDE);
  tela.display();

  WiFi.begin(REDE, SENHA);
  // begin() does NOT wait for the connection: this loop is what waits. Same on
  // the real board — that is why every WiFi example starts with it.
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  digitalWrite(LED, HIGH);
  Serial.println();
  Serial.print("IP: ");
  Serial.println(WiFi.localIP());

  tela.clearDisplay();
  escrever(0, "connected!");
  escrever(1, "IP " + WiFi.localIP().toString());
  escrever(2, "RSSI " + String(WiFi.RSSI()) + " dBm");
  tela.display();
  delay(1500);
}

void loop() {
  HTTPClient http;
  http.begin("http://example.com/");
  int codigo = http.GET();             // negative = it never even replied

  tela.clearDisplay();
  escrever(0, "GET example.com");
  if (codigo > 0) {
    String corpo = http.getString();
    Serial.printf("HTTP %d, %u bytes\n", codigo, corpo.length());
    escrever(1, "HTTP " + String(codigo));
    escrever(2, String(corpo.length()) + " bytes");
    escrever(4, "IP " + WiFi.localIP().toString());
  } else {
    Serial.printf("failed: %s\n", http.errorToString(codigo).c_str());
    escrever(1, "failed");
    escrever(2, http.errorToString(codigo));
  }
  tela.display();
  http.end();                          // always close it: or the socket gets stuck

  delay(8000);
}

WiFi.begin() does not wait: the loop on WiFi.status() != WL_CONNECTED is what waits, on a board and here. Then HTTPClient fetches a page from the internet, and the length of what came back goes to the OLED display and the serial monitor. The library list is for the display; Wi-Fi needs nothing extra.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Dots while it connects, then the IP address, then one HTTP request every 8 seconds with the status code and the number of bytes. Now change the router's password in Settings and press restart: the dots never end, and the serial monitor says why.

How the simulation models it

Try this

  1. Add a second router with another name and switch the sketch between the two.
  2. Make the router's network open (empty password) and keep a password in the code: read what the serial monitor reports.
  3. Fetch a JSON API instead of a page and print one field of it.

See also