Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

3 Steps to Connect an ESP8266 to WiFi for IoT Projects

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

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.

SaleBestseller No. 1
ELEGOO 120pcs Multicolored Dupont Wire 40pin Male...
Each cable length: about 20 cm /8-inch; Packing in a color box.
$9.99 −$3.01 $6.98
Bestseller No. 2
ELEGOO UNO Project Super Starter Kit with Tutorial...
Free PDF tutorial(more than 22 lessons) and clear listing in a nice package; Lcd1602 module with pin header (not need to be soldered by yourself)
$44.99

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.

[et_bloom_inline optin_id=optin_25]

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
KEYESTUDIO 37 in 1 Sensor Kit 37 Sensors Modules...
Packed well in nice box, also each item packed well separately.
$35.99
Bestseller No. 2
ELEGOO UNO Project Super Starter Kit with Tutorial...
Free PDF tutorial(more than 22 lessons) and clear listing in a nice package; Lcd1602 module with pin header (not need to be soldered by yourself)
$44.99

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.

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES