ESP 8266 WiFi Libraries

The WiFi library is written for the Arduino WiFi Shield, which is completely different than the ESP8266. You can't use the WiFi library with the ESP8266. The ESP8266WiFi library is part of ESP8266 core for Arduino:

which is used to directly program the ESP8266 via the Arduino IDE. The ESP8266WiFi library is similar to the WiFi library in that it attempts to use the same API but the actual code inside the libraries is different.

It appears that you are instead using the ESP8266 as a WiFi module. Likely it is connected to your Mega via serial and the ESP8266 is running the AT firmware. For that usage I recommend this library:

It also uses the standardized API. When you are using the ESP8266 as a module you will not want to use the ESP8266 core for Arduino as you will be programming the Mega rather than the ESP8266.

1 Like