ESP8266 WiFi client

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
davef
Posts: 811
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.active(True)
#        sta_if.ifconfig(('192.168.10.99', '255.255.255.0', '192.168.10.1', '8.8.8.8'))
        sta_if.connect('TP-LINK_4A88F3', 'xyz') 

        while (count < 5):
            utime.sleep(5)
            count += 1

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

    while not sta_if.isconnected():
        machine.reset()
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.

enzo
Posts: 40
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.
Thanks.

enzo
Posts: 40
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):
    print(ubinascii.hexlify(network.WLAN().config('mac'),':').decode())
    client = network.WLAN(network.STA_IF)
    client.active(True)
    print(client.scan())
    if not client.isconnected():
        print("Connecting to network...")
        client.connect(SSID, PW)
        while not client.isconnected():
            time.sleep_ms(100)
    print('Connection successful')
    return(client)    
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