ESP8266 / NodeMCU: Establishing a Network Connection

(click here to see index of all ESP8266 posts)

Before I could experiment with network connectivity, I needed to get the ESP8266 connected to my wireless LAN.

So far I have used the ESP8266 as a network connected device (e.g. NOT an Access Point), so I put it into station mode:


Next, I wanted to see a list of access points, so I used this code:

    function (t)
        local k = ''
        local v = ''
        if t == nil then
            print('no end points')
            for k,v in pairs(t) do
                print(string.format('%-30s: %s', k, v))

The wifi.sta.getap function has proven to be a bit flaky for me. It has worked fine on my ESP8266-01 fine so far, but on my nodeMCU devkit, it quite often finds nothing and then reboots the chip. The ESP8266-01 is running a slightly older version of nodeMCU, so perhaps the devkit’s newer firmware has a bug?

Even though wifi.sta.getap() may act up, wifi.sta.config() still works fine.

The above code lists the APs like this:

Chameleon                     : 4,-61,6c:fd:b9:4c:e9:be,11
702Crew                       : 3,-84,80:ea:96:ed:d5:f6,11
Wind Wireless 10              : 0,-88,00:15:6d:63:a1:74,9
HP-Print-56-Photosmart 7520   : 3,-92,c4:34:6b:40:79:56,11
SMOKEY                        : 2,-61,6e:fd:b9:4c:e9:be,11
xfinitywifi                   : 0,-93,06:3e:0c:a6:7e:f0,11

To connect to the wireless LAN, I used the wifi.sta.config() function:


This function connects to the network and assigns a DHCP-based IP address to the ESP8266.

I then verified connectivity (5 indicates an IP address has been assigned):

> =wifi.sta.status()

and I could see the IP information:

> =wifi.sta.getip()

Finally, from my PC I can verified connectivity by pinging the ESP8266:


Pinging with 32 bytes of data:

Reply from bytes=32 time=119ms TTL=255
Reply from bytes=32 time=131ms TTL=255
Reply from bytes=32 time=156ms TTL=255
Reply from bytes=32 time=6ms TTL=255

Ping statistics for
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 6ms, Maximum = 156ms, Average = 103ms


Once the connection is made, it will be persistent across reboots and power recycles of the ESP8266. However, it doesn’t seem to last forever. I manually made the connection like this and experimented for about 2 weeks before I was forced to reconnect because it finally lost the connection.




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

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

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