Arduino build error

fatal error: WiFi.h: No such file or directory

WiFi.h is the WiFi library of boards that have a radio: the ESP32 family, the Pico W and the Uno R4 WiFi.

  • Build error
  • 2 common causes
The message

What the Arduino IDE prints

Output window
sketch.ino:1:10: fatal error: WiFi.h: No such file or directory
    1 | #include <WiFi.h>
      |          ^~~~~~~~~~
compilation terminated.
exit status 1

An Uno, a Nano or a Mega has no WiFi chip and no such library, so a sketch that includes it cannot build for them.

Mokxi's compiler, clang, words it as 'WiFi.h' file not found.

Causes

The 2 usual causes with the fix

1The board has no WiFi

Choose a board with WiFi, such as an ESP32. In Mokxi the board joins a network written in software, so WiFi.begin() works with no radio.

Broken
#include <WiFi.h>

void setup() {
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);
}

void loop() {
}
Fixed
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  Serial.println("wifi ready");
}

void loop() {
}

Mokxi’s build tab says: WiFi.h needs a board with Wi-Fi, and the Arduino Uno has none

Try this fix in Mokxi

2The ESP32 board package is not installed

With an ESP32 chosen but the package missing, the IDE falls back or stops. WiFi.h comes with the package.

  1. In the Arduino IDE, open Tools, Board, Boards Manager, search for esp32 and install the one by Espressif Systems.
  2. Pick the ESP32 board you have under Tools, Board, and compile again.
  3. On an ESP8266, include ESP8266WiFi.h instead of WiFi.h.
FAQ

Questions and answers

Which boards have WiFi.h?

ESP32 boards, the Raspberry Pi Pico W, and the Arduino Uno R4 WiFi. The ESP8266 uses ESP8266WiFi.h.

Can I add WiFi to an Uno?

Yes, with a WiFi module such as an ESP-01 over serial. It is a different library with its own commands.

Does WiFi work in Mokxi?

Yes, on a network written in software, with the name and password set on the board. There is no radio.