Call or Text us: 1 (470) 231-7659
✈ Free Domestic Shipping on all Engineering Bundles!
How to connect ESP8266 WiFi in 3 easy steps for Wemos D1 Mini NodeMCU

3 Steps to Connect an ESP8266 to WiFi for IoT Projects

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

One of the biggest steps after learning how to program devices and sensors with Arduino is to make your device “Internet Enabled.”

That way if you want to create a custom prototype with network capabilities, you can collect and publish data from the cloud, your home, and practically anywhere.

WiFi connectivity is also important if you want to add voice control, like Siri, to an Arduino device.

In this article, I’ll show you the steps to connect an ESP8266 controller to WiFi so that you can add Internet connectivity to custom prototypes.

Connect the ESP8266 to the Internet

This tutorial is intended for more Intermediate learners. We won’t be covering the basics of Arduino in this tutorial.

If you don’t have a foundation in electronics, coding, and Arduino, I recommend taking a look at my Arduino for Beginners course and learning those skills first.

However, if you’ve already built a project using the ESP8266 and can’t quite get it “online” for monitoring or cloud access, then this tutorial will guide you through that process.

Hardware You’ll Need

You’ll need some basic hardware for this project. I recommend using the Wemos D1 Mini or NodeMCU as your WiFi-enabled chip.

The Arduino Kit and Sensors are optional. We won’t get into specifics on how to wire a circuit or publish and collect data from the Internet in this article.

However, we have an online Special Topics Robotics Course that covers these topics in more detail. You can sign up for that course, here.

The purpose of this tutorial is to show you the three steps on how to take an Arduino controller and get it connected to your home network (and WWW) so that you can use it as an IoT device.

We won’t cover how to add the ESP8266 to the Boards Manager or basic configuration in this tutorial. If you haven’t programmed an ESP8266 (NodeMCU or Wemos D1 Mini) then I recommend checking out our getting started guide before reading this tutorial.

Step 1. Setup the Arduino IDE for ESP8266

First, use the Arduino Board Manager to configure the ESP8266. More detailed instructions are available below:

Once you have the board configured, include the ESP8266WiFi header.

#include <ESP8266WiFi.h>

Next, create two global variables to store your network SSID and password. This is your WiFi Network’s name and password, respectively.

/*ADD YOUR PASSWORD BELOW*/
const char *ssid = SECRET_SSID;
const char *password = SECRET_PASS;

For example, if I wanted to connect to the network, LearnRoboticsWIFI and the password is RoboticsR0ck$ (not real btw), then you’d write the following lines:

/*ADD YOUR PASSWORD BELOW*/
const char *ssid = LearnRoboticsWIFI;
const char *password = RoboticsR0ck$;

Then, initialize a WiFiClient called client.

WiFiClient client;

Now that we have our globals taken care of, we can write a method that connects the controller to the WiFi network we defined using the SSID and password above.

Step 2. Connect the ESP8266 to your WiFi Network (SSID)

The next step is to create a connectToWiFi() method. We can use the Serial Monitor to print out messages as we attempt to connect to our WiFi Network.

/*
* Connect your controller to WiFi
*/
void connectToWiFi() {
//Connect to WiFi Network
   Serial.println();
   Serial.println();
   Serial.print("Connecting to WiFi");
   Serial.println("...");
   WiFi.begin(ssid, password);
   int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) {
   retries++;
   delay(500);
   Serial.print(".");
}
if (retries > 14) {
    Serial.println(F("WiFi connection FAILED"));
}
if (WiFi.status() == WL_CONNECTED) {
    Serial.println(F("WiFi connected!"));
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}
    Serial.println(F("Setup ready"));
}

This code will try to connect to the network 15 times. If it can’t connect, then it will fail and print out a failure message in the Serial Monitor.

Otherwise, you’ll see that the controller is connected.

After the method is written, don’t forget to call it within your setup() routine. We only want to run the connectToWiFi() method once.

/*
 * call connectToWifi() in setup()
 */
void setup(){
 //...other setup code here...
 connectToWiFi();
}

And that’s it! Once you have this code added to your sketch, you can test it out.

Step 3. Test the ESP8266 Network WiFi Connection

Before testing this program, ensure that you’ve added the correct network SSID (name) and password.

Then, upload your full sketch (with the WiFi connection code from Steps 1 and 2) to your ESP8266.

Next, open the Serial Monitor and watch the initialization happen. You should see a few lines that say “Connecting to WiFi.”

The controller will attempt to connect 15 times. If it fails, you’ll see the message “WiFi connection FAILED.”

Ideally, you’ll see a series of messages print out on the Serial Monitor:

  • “WiFi connected!”
  • “IP address: ” plus the assigned IP address from your router
  • and “Setup ready”

Once you see the “Setup ready” message, you’re ready to start using your ESP8266 on WiFi. You can also access (or ping) your device at the IP address that it was assigned from the router.

Bestseller No. 1
SaleBestseller No. 2
Arduino Starter Kit [K000007] (English Projects...
The kit comes with a 170-page project guide book and includes over 100 components.
$99.95 −$10.05 $89.90

What can you do with a WiFi-enabled Arduino?

The best part about connecting an ESP8266 to WiFi is that you can now use these projects on your internal (home) network or send data to a cloud service.

This is useful for remotely monitoring sensors or devices, making IoT projects, and for custom home automation systems.

After you add your ESP8266 to the internet, the possibilities on what you can build, track, and create, become endless.

Looking for Arduino projects to make?

Here are some of our most popular IoT projects you can try:

If you enjoyed this article, please share it with a friend! You can also support our online publication when you buy us a coffee.

You May Also Like

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying me a coffee. If you need immediate support, you can schedule an appointment. Providing customized information for your individual scenario takes a lot of my time. I’m happy to help, but I can no longer provide individualized advice pro bono.
Thank you for understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Structured Learning

Control your robotics journey

Industry Innovation

Learn alongside engineering professionals

Make Advancements

Designed for all backgrounds

Learn Robotics Botly Favicon

Become a Learn Robotics Insider

 

Be the first to know when we launch new robotics content. Plus gain FREE robotics tips to your inbox.

Awesome! Check your inbox for a surprise!

Remote Robotics Courses for High School, Middle School, or Pre-College Training

Join the Powered by Learn Robotics Program

Teach robotics anywhere with our turnkey robotics program!

Download our free program brochure!

Success! Check your email for the download!

Pin It on Pinterest