Mode STA...no work

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
madeskjet
Posts: 4
Joined: Fri May 01, 2020 5:27 pm

Mode STA...no work

Post by madeskjet » Fri May 01, 2020 5:38 pm

Impossible!

NodeMCU Amica V1.0
MicroPython v1.12 on 2019-12-20; ESP module with ESP8266

Code: Select all

import machine 
import network  
import time     

led = machine.Pin(16, machine.Pin.OUT) 

led.value(0)

print ("LED Encendido")

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF) 

if (ap_if.active()): 
	if (ap_if.active()):
    		ap_if.active(False)

if not sta_if.isconnected():
    print ("Conectando a la red WiFi")
    sta_if.active(True) 
    sta_if.connect('SSID','PASS')
    while not sta_if.isconnected():
                pass
    print ("Conectado!")

print ("Configuracion de red: ", sta_if.ifconfig())
I tried different AP, with/without security... :roll:

What happen?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Mode STA...no work

Post by Roberthh » Fri May 01, 2020 6:06 pm

Busy waiting is sometimes a problem. Try:

Code: Select all

import machine 
import network  
import time     

led = machine.Pin(16, machine.Pin.OUT) 

led.value(0)

print ("LED Encendido")

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF) 

if (ap_if.active()): 
    ap_if.active(False)

if not sta_if.isconnected():
    print ("Conectando a la red WiFi")
    sta_if.active(True) 
    sta_if.connect('SSID','PASS')
    while not sta_if.isconnected():
        time.sleep(0.2)
    print ("Conectado!")

print ("Configuracion de red: ", sta_if.ifconfig())

madeskjet
Posts: 4
Joined: Fri May 01, 2020 5:27 pm

Re: Mode STA...no work

Post by madeskjet » Fri May 01, 2020 7:19 pm

Yes! I do it! :D

Thanks!

jomas
Posts: 59
Joined: Mon Dec 25, 2017 1:48 pm
Location: Netherlands

Re: Mode STA...no work

Post by jomas » Fri May 01, 2020 7:33 pm

madeskjet wrote:
Fri May 01, 2020 5:38 pm
Impossible!

NodeMCU Amica V1.0
MicroPython v1.12 on 2019-12-20; ESP module with ESP8266

Code: Select all

import machine 
import network  
import time     

led = machine.Pin(16, machine.Pin.OUT) 

led.value(0)

print ("LED Encendido")

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF) 

if (ap_if.active()): 
	if (ap_if.active()):
    		ap_if.active(False)

if not sta_if.isconnected():
    print ("Conectando a la red WiFi")
    sta_if.active(True) 
    sta_if.connect('SSID','PASS')
    while not sta_if.isconnected():
                pass
    print ("Conectado!")

print ("Configuracion de red: ", sta_if.ifconfig())
I tried different AP, with/without security... :roll:

What happen?
You MUST put a delay between defining your sta and ap like:

Code: Select all

sta_if = network.WLAN(network.STA_IF)
time.sleep(0.1)  # <---- IMPORTANT
ap_if = network.WLAN(network.AP_IF) 
Then it will work
Last edited by jomas on Fri May 01, 2020 8:19 pm, edited 1 time in total.

madeskjet
Posts: 4
Joined: Fri May 01, 2020 5:27 pm

Re: Mode STA...no work

Post by madeskjet » Fri May 01, 2020 7:46 pm

Roberthh wrote:
Fri May 01, 2020 6:06 pm
Busy waiting is sometimes a problem. Try:

Code: Select all

import machine 
import network  
import time     

led = machine.Pin(16, machine.Pin.OUT) 

led.value(0)

print ("LED Encendido")

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF) 

if (ap_if.active()): 
    ap_if.active(False)

if not sta_if.isconnected():
    print ("Conectando a la red WiFi")
    sta_if.active(True) 
    sta_if.connect('SSID','PASS')
    while not sta_if.isconnected():
        time.sleep(0.2)
    print ("Conectado!")

print ("Configuracion de red: ", sta_if.ifconfig())
Works! Just one time... :shock:
Upload again main.py and...not works.

I don´t get it.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Mode STA...no work

Post by Roberthh » Fri May 01, 2020 7:56 pm

The ESP8266 will remember the WiFi status between reboots. So if you have disabled AP once, you do not have to repeat that.
I'm personally using STA mode since the ESP8266 port exists w/o problems. The connect code is called from main.py.

jomas
Posts: 59
Joined: Mon Dec 25, 2017 1:48 pm
Location: Netherlands

Re: Mode STA...no work

Post by jomas » Fri May 01, 2020 8:31 pm

madeskjet wrote:
Fri May 01, 2020 7:46 pm



Works! Just one time... :shock:
Upload again main.py and...not works.

I don´t get it.
@madeskjet , did you read my post?
You have you put time.sleep(0.1) as I suggested because that is the problem.

Edit:
Maybe it is neccessary to put de delay after "sta_if.active(True)"

Anyway it 100% is a (reproducable) bug in either Micropython (or in de SDK of Espressif) which I mentioned already in viewtopic.php?f=16&t=7577

madeskjet
Posts: 4
Joined: Fri May 01, 2020 5:27 pm

Re: Mode STA...no work

Post by madeskjet » Fri May 01, 2020 10:29 pm

jomas wrote:
Fri May 01, 2020 8:31 pm
madeskjet wrote:
Fri May 01, 2020 7:46 pm



Works! Just one time... :shock:
Upload again main.py and...not works.

I don´t get it.
@madeskjet , did you read my post?
You have you put time.sleep(0.1) as I suggested because that is the problem.

Edit:
Maybe it is neccessary to put de delay after "sta_if.active(True)"

Anyway it 100% is a (reproducable) bug in either Micropython (or in de SDK of Espressif) which I mentioned already in viewtopic.php?f=16&t=7577
Another Micropython firmware w/o bugs?
Thanks

kr-g
Posts: 48
Joined: Sun Dec 01, 2019 7:52 pm
Contact:

Re: Mode STA...no work

Post by kr-g » Thu May 28, 2020 6:14 am

sometimes it takes longer to (re-)connect.
and having then only a "one-flow" logic doesnt work then.

have a look here at the base modules (wlan, softap, and ntp modules)
which does it in a "loop" manner (the impl keeps an eye on state)

https://github.com/kr-g/mpymodcore

Post Reply