Engineers - Reinvent Your Career with Robotics in the Next 90 Days
Consult with Experts in our Career Advancement Program for the Experienced Engineer & Tech Professional. Gain clarity & community. Apply Now!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

3 Steps to Connect an ESP8266 to WiFi for IoT Projects

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, I 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.

Liz Miller Learn Robotics

Attention: Engineer Looking for a New Career in Robotics

Liz here 👋, Engineer,  Robotics Expert & founder of this website.

If I offered to help you advance your 6-Figure+ Robotics, Automation, or AI Career with a $10,000 sign-on bonus in the next 90 days or less…

…without wasting time on unnecessary certifications…
…without studying another coding language or tactic…
…without needing to get another degree and wasting years…

…would you take me up on that offer?

LIMITED SPACE: I have 6 more client spots open this week!

Want a spot? Click the button to access my 12-min training below and then apply if it makes sense.

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.

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
AITRIP 6PCS 2A USB 18650 Lithium Li-ion Battery...
Can be used for multimeter, converted to lithium battery charging; Discharging current: Max. 2A
$9.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.

Liz Miller Learn Robotics

Attention: Engineer Looking for a New Career in Robotics

Liz here 👋, Engineer,  Robotics Expert & founder of this website.

If I offered to help you advance your 6-Figure+ Robotics, Automation, or AI Career with a $10,000 sign-on bonus in the next 90 days or less…

…without wasting time on unnecessary certifications…
…without studying another coding language or tactic…
…without needing to get another degree and wasting years…

…would you take me up on that offer?

LIMITED SPACE: I have 6 more client spots open this week!

Want a spot? Click the button to access my 12-min training below and then apply if it makes sense.

6 Responses

  1. Thanks for your article. The password and SSID needed to be enclosed in quotation marks to work successfully.

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.

Did this content help you? 🤖
...say thanks and send us a coffee!

Experience Engineers & Tech Pro’s:

Gain personalized guidance and direct mentorship from Liz in her Robotics Mentorship Program.

This 12-week professional advancement program will help you soar to the next level in your engineering career. My clients earn $100k+ with a $10k+ sign on bonus.

You’ll uncover strategies they *don’t* teach you in engineering school to help you earn more and live a meaningful life, all while working in high-impact robotics, automation, and AI roles.

Are you next? Check out the 12-min training I put together and then apply if it makes sense.

WHAT IS LEARN ROBOTICS?

Learn Robotics helps self-starters, hobbyists, and tech professionals get started in the robotics industry. 

We leverage a simple formula to make it easy to gain highly marketable and high-value 6-figure robotics & automation skills. 

Learn Robotics was founded by Robotics Engineer, Liz Miller, and leverages the Robotics Success Equation, our unique methodology that helps people learn to build robots while enhancing their careers for the future.

liz's featured robotics PROJECT ON YOUTUBE

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES

Apply to Work with Liz in her Robotics Mentorship Program for Tech Professionals

Liz Miller Learn Robotics