Adsense HTML/JavaScript

Tuesday, March 16, 2021

ESP32/MicroPython: get wifi network info, scan networks

 Get WiFi MAC address, network info such as ip, netmask...

from os import uname
from sys import implementation
import network
import ubinascii

ssid = "ssid"
password = "password"

print(implementation.name)
print(uname()[3])
print(uname()[4])
print()

mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC: " + mac)
print()

def do_connect():
    print('connect to network...')
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    
    print()
    print('network config:')
    print("interface's IP/netmask/gw/DNS addresses")
    print(wlan.ifconfig())
    
do_connect()

print('- bye -')


Scan WiFi networks:


from os import uname
from sys import implementation
import network
import ubinascii
import utime

ssid = "ssid"
password = "password"

print(implementation.name)
print(uname()[3])
print(uname()[4])
print()

mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC: " + mac)
print()

#init ESP32 as STA
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
utime.sleep(1)

def do_connect():
    global wlan
    print('connect to network...')
    
    wlan.active(True)
    if not wlan.isconnected():
        print('...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    
    print()
    print('network config:')
    print("interface's IP/netmask/gw/DNS addresses")
    print(wlan.ifconfig())
    
def do_scan():
    global wlan
    print('scan network...')
    wlan.active(True)
    for network in wlan.scan():
        print(network)
    
do_scan()

print('\n- bye -')



ref:
~ Docs > Quick reference for the ESP32 > Networking


No comments:

Post a Comment