Can access point executes .connect?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
lcarnevale
Posts: 1
Joined: Thu Dec 23, 2021 9:08 am
Contact:

Can access point executes .connect?

Post by lcarnevale » Mon Aug 08, 2022 10:58 am

Dear all,

I am creating a network of ESP32 devices that set up access points with name, i.e., "something-network", and connect to another network with the same name.
I tried to configure both STA_IF and AP_IF modes. However, the IP address of STA node and the IP address of AP gateway are in conflict. Furthermore, I tried to redefine the subnet of access points, but devices see each other with a cluster of star topologies. My intent is instead to create a network as closely as possible to mesh topology.

I thought to use access point only, but my idea seems to have issues.
I would like to know if the configuration of an ESP32 in the access point WLAN mode brings also the opportunity to establish a connection.

Look at the snippet below:

Code: Select all

nic = network.WLAN(network.AP_IF)
nic.active(True)
ssid, passwd = 'something-network', 'secret-password'
nic.config(essid=ssid,password=passwd,authmode=3,hidden=False)
nic.conect(ssid,passwd)
This generates the following error:

Code: Select all

OSError: Wifi Invalid Mode

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Can access point executes .connect?

Post by DeaD_EyE » Mon Aug 08, 2022 2:13 pm

You need two different WLAN objects. One for STA mode and the other for AP mode.
Then you can create two sockets. If you bind the socked (also UDP), the socket should use only the interface, which is in the same network as the socket and should only receive messages from the interface which is bound to the socket.

Example code:

Code: Select all

from network import WLAN, AP_IF, STA_IF, AUTH_OPEN
from usocket import socket, AF_INET, SOCK_DGRAM
from utime import sleep

sta_ssid = "xxx"
sta_pw   = "xxx"

ap_ssid = "ESP32_AP"
ap_pw   = ""

sta = WLAN(STA_IF)
sta.active(False)
sta.active(True)
sta.connect(sta_ssid, sta_pw)

while (sta_ip := sta.ifconfig()[0]) == "0.0.0.0":
    print("Waiting for connection")
    sleep(1)

print("Connected to WLAN with IP:", sta_ip)

ap  = WLAN(AP_IF)
ap.active(False)
ap.active(True)
ap.config(ssid=ap_ssid, key=ap_pw, security=AUTH_OPEN)
# usually ap takes 192.168.4.1 as default ip
# setting a different ip and the internal DHCP should recognize this
ap.ifconfig(("192.168.1.1", "255.255.255.0", "192.168.1.1", "192.168.1.1"))
ap_ip  = ap.ifconfig()[0]

# example with udp sockets
sta_sock = socket(AF_INET, SOCK_DGRAM)
sta_sock.bind((sta_ip, 0))

ap_sock = socket(AF_INET, SOCK_DGRAM)
ap_sock.bind((ap_ip, 0))

# my host
sta_sock.sendto(b"Hello World\n", ("192.168.33.11", 8888))

# smartphone connected to ESP32
ap_sock.sendto(b"Hello World\n", ("192.168.1.2", 8888))


User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Can access point executes .connect?

Post by jimmo » Thu Aug 11, 2022 1:29 am

lcarnevale wrote:
Mon Aug 08, 2022 10:58 am
However, the IP address of STA node and the IP address of AP gateway are in conflict.
You will need to use interface.ifconfig to set an address explicitly (for both AP and STA interfaces).

Post Reply