ESP32/ESP8266 WiFi setup - what are the rules? #10152
Unanswered
davefes
asked this question in
Core Development
Replies: 2 comments 3 replies
-
I use this script to connect on ESPx, Pyboard D, Pico W... def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(False) # deactivate the interface
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('SSID', 'PASSWORD') # EDIT here
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
a = sta_if.config('mac')
print('MAC {:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(a[0],a[1],a[2],a[3],a[4])) |
Beta Was this translation helpful? Give feedback.
1 reply
-
Hi, Maybe this helps you a bit. I have written a short class to handle the connection with a wifi. Im working with this class on an esp32. It first connects through dhcp and then you can reset the IP-Address you've got to the address you want to have: import network
class Esp32_wifi:
# Constructor
def __init__(self, ssid: str, ssid_password: str):
# handover arguments to atributes
self.__ssid = ssid
self.__password = ssid_password
#create instance of wifi
self.station = network.WLAN(network.STA_IF)
# initialize instance atributes
self.__ipAddress = ""
self.__netmask = ""
self.__gateway = ""
self.__dns = ""
# Connect to an existing WIFI
def connect(self,_IP='0', _netmask='0', _gateway='0', _dns='0'):
#switch wifi on and handover ssid & password
self.station.active(True)
self.station.connect(self.__ssid, self.__password)
# Wait until connection is made
while self.station.isconnected() == False:
print('Wait WIFI Connection...')
pass
#succesfully connected
print('Connection successful')
self.setNetworkParam(_IP,_netmask,_gateway,_dns)
#return connection status
return self.station.isconnected()
def _getParam(self):
"""Method returns current wifi parameters"""
self.__ipAddress, self.__netmask, self.__gateway, self.__dns = self.station.ifconfig()
return self.station.isconnected()
def setNetworkParam(self, _IP ='0', _netmask='0', _gateway='0', _dns='0'):
"""Through this method you can set new parameters after connected to wifi"""
self._getParam()
if _IP == '0':
_IP = self.__ipAddress
if _netmask == '0':
_netmask = self.__netmask
if _gateway == '0':
_gateway = self.__gateway
if _dns == '0':
_dns = self.__dns
self.station.ifconfig((_IP,_netmask,_gateway,_dns))
self._getParam()
#function to switch wifi off
def disconnect(self):
return self.station.active(False)
@property
def ipAddress(self):
return self.__ipAddress
@property
def netmask(self):
return self.__netmask
@property
def gateway(self):
return self.__gateway
@property
def dns(self):
return self.__dns |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have making making STA_active before doing any configuration. Just on the off-chance that all my work-arounds to achieve reliable WiFi have been to no benefit ... should one do any configuration, ie setting a static IP before or after making the interface ACTIVE.
Hopefully, the preferred approach is valid for all ESP32 variants AND for the ESP8266, but if not please state the difference(s).
Thanks heaps,
Dave
Beta Was this translation helpful? Give feedback.
All reactions