ESP8266 WiFi client

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Posts: 142
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP8266 WiFi client

Post by davef » Tue Jan 12, 2021 9:35 pm

This is where I have got after 100's of attempts:

Code: Select all

import network
import machine

def connect():
    count = 0

    sta_if = network.WLAN(network.STA_IF)

    if not sta_if.isconnected():
        print('trying to connect to hotspot...')
#        sta_if.ifconfig(('', '', '', ''))
        sta_if.connect('TP-LINK_4A88F3', 'xyz') 

        while (count < 5):
            count += 1

            if sta_if.isconnected():
                print('connected to the hotspot')
                count = 0
                print('not connected to the hotspot')

    while not sta_if.isconnected():
I don't think it ever got to the machine.reset() stage, is that the same as a power cycle?

A frustration was that every so often a new IP would be assigned to the device by the hotspot ... which made using WebREPL hit and miss. A static IP sorts that.

Posts: 23
Joined: Thu Jun 11, 2020 1:03 am

Re: ESP8266 WiFi client

Post by enzo » Tue Jan 12, 2021 11:00 pm

Forget it: it is my router's DHCP pool not releasing unconnected devices. There were no more IP addresses left for the new boards: once deleted the unused ones the new boards connect.

Posts: 23
Joined: Thu Jun 11, 2020 1:03 am

Re: ESP8266 WiFi client

Post by enzo » Wed Jan 13, 2021 3:45 pm

Thanks, I'll do so.
Roberthh wrote:
Tue Jan 12, 2021 8:43 pm
Two notes. I do not recommend a busy wait in the loop for wait connected. better add a small delay, like:

Code: Select all

import time
def connectToWifi(SSID, PW):
    client = network.WLAN(network.STA_IF)
    if not client.isconnected():
        print("Connecting to network...")
        client.connect(SSID, PW)
        while not client.isconnected():
    print('Connection successful')
Since a while I have the issue with the ESP8266 that they only connect to the WiFi STA after a power cycle. Pushing reset is not sufficient. I cannot exactly tell the time or version at which that started, but it could be the change from v1.12 to v1.13.

Post Reply