ESP8266/Arduino IDE: Connecting to a Network with WIFI

After success with yesterday’s project, ESP8266 and Using the Arduino IDE, I was ready to try to get the ESP8266 to connect to my WIFI network. That is today’s project.

My hardware setup is exactly the same as yesterday, namely using a ESP-01 mounted on a breadboard adapter.

Serial Port Debugging

Before I could really go any further I needed to determine if Serial.print() works properly for the ESP8266 or if I need to do something else.

It works fine and exactly like it would have for an Arduino – just bring up whichever serial port monitor you prefer and attach to the proper COM port.

There is one minor glitch: When you connect to an Arduino with a serial monitor, it causes DTR to toggle and the Arduino will start. This means when you connect to the Arduino, you get to watch the code start executing from the beginning.

In the case of the ESP8266, this doesn’t work. Between the time the upload completes and you get the serial monitor connected, you may well loose some debugging.

This is a problem for the Teensy and Leonardo as well. Teensy implements Serial.dtr() which you can monitor and Leonardo uses the test if (!Serial) to detect when the monitor connects. I can find nothing like that for the ESP8266.

Not all is lost though. The breadboard adapter has a reset button on it (as do many of the various ESP8266 breakout boards I’ve seen). Once you get the serial monitor running, just manually press the restart button.

WIFI Header File

There is a WiFi.h file in the normal Arduino libraries, BUT you do not use that one. It is for an Arduino WiFi shield, not for the ESP8266. The include file you want is

#include <ESP8266WiFi.h>

I assumed this file would be in the Arduino directory (installed by there by the ESP8266 add-on), but it is not. You will find it in


I’ve looked at several of the header files and they are well documented.

You can also find all of the libraries at along with some documentation.

Program/Sketch to Make a Wifi Connection

OK, I had the preliminaries figured out. Time to write some real code. Here is my program.

#include             <ESP8266WiFi.h>

const int             ledPin                 = 0;

// ---------------------------------------------------------------------------
void setup() {

char                pass[]                = "1234";
char                ssid[]                = "AP101";
int                 status;

pinMode(ledPin, OUTPUT);

Serial.print("Trying to connect to ");

// attempt to connect to Wifi network:
status = WiFi.begin(ssid, pass);

status = WiFi.waitForConnectResult();
if (status != WL_CONNECTED) {
    Serial.println("Connection Failed");
    while (true) {}

Serial.print("MAC Addr: ");
Serial.print("IP Addr:  ");
Serial.print("Subnet:   ");
Serial.print("Gateway:  ");
Serial.print("DNS Addr: ");
Serial.print("Channel:  ");
Serial.print("Status: ");

} // setup

// ---------------------------------------------------------------------------
void loop() {

int                    k;

for (k = 0; k <= 100; k = k + 1) {

    digitalWrite(ledPin, HIGH);

    digitalWrite(ledPin, LOW);


while (true) {}


As you can see, there isn’t much to it. WiFi.begin specifies the SSID and password. SSID is CASE SIGNIFICANT.

I then simply wait for the status result. If we are connected, I print information about the connection like this:


I left the blinky code in because I will make use of it again down the road.

After I blink 100 times, I close the network connection with WiFi.disconnect(true) which disconnects and turns the radio off.

Note that after I disconnect, I put the ESP8266 in an infinite loop to stop all processing. This has an interesting side effect:


Evidently (and not surprisingly), the ESP8266 implements a watchdog timer. I have not researched this WDT yet, but I am fairly certain you must allow control to return to some unseen routine that manages the WiFi connection. Perhaps I’ll research that a bit as well.


This entry was posted in c-esp8266 and tagged . Bookmark the permalink.

One Response to ESP8266/Arduino IDE: Connecting to a Network with WIFI

  1. Pingback: ESP8266/Arduino IDE: Communicating with TCP | Big Dan the Blogging Man

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s