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:
wifi.sta.getap( function (t) local k = '' local v = '' if t == nil then print('no end points') else print() for k,v in pairs(t) do print(string.format('%-30s: %s', k, v)) end end end )
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() 5
and I could see the IP information:
> =wifi.sta.getip() 184.108.40.206 255.255.255.0 220.127.116.11
Finally, from my PC I can verified connectivity by pinging the ESP8266:
[C:\]ping 18.104.22.168 Pinging 22.214.171.124 with 32 bytes of data: Reply from 126.96.36.199: bytes=32 time=119ms TTL=255 Reply from 188.8.131.52: bytes=32 time=131ms TTL=255 Reply from 184.108.40.206: bytes=32 time=156ms TTL=255 Reply from 220.127.116.11: bytes=32 time=6ms TTL=255 Ping statistics for 18.104.22.168: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 6ms, Maximum = 156ms, Average = 103ms [C:\]
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.