41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
# SPDX-FileCopyrightText: 2022 Ladyada for Adafruit Industries
|
|
# SPDX-License-Identifier: MIT
|
|
"""MicroPython simple WiFi connection demo"""
|
|
import time
|
|
import network
|
|
import urequests
|
|
|
|
station = network.WLAN(network.STA_IF)
|
|
station.active(True)
|
|
|
|
# Network settings
|
|
wifi_ssid = "MY_SSID"
|
|
wifi_password = "MY_PASSWORD"
|
|
url = "http://wifitest.adafruit.com/testwifi/index.html"
|
|
|
|
print("Scanning for WiFi networks, please wait...")
|
|
authmodes = ['Open', 'WEP', 'WPA-PSK', 'WPA2-PSK4', 'WPA/WPA2-PSK']
|
|
for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan():
|
|
print("* {:s}".format(ssid))
|
|
print(" - Channel: {}".format(channel))
|
|
print(" - RSSI: {}".format(RSSI))
|
|
print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
|
|
print(" - Auth: {}".format(authmodes[authmode]))
|
|
print()
|
|
|
|
# Continually try to connect to WiFi access point
|
|
while not station.isconnected():
|
|
# Try to connect to WiFi access point
|
|
print("Connecting...")
|
|
station.connect(wifi_ssid, wifi_password)
|
|
time.sleep(10)
|
|
|
|
# Display connection details
|
|
print("Connected!")
|
|
print("My IP Address:", station.ifconfig()[0])
|
|
|
|
# Perform HTTP GET request on a non-SSL web
|
|
response = urequests.get(url)
|
|
|
|
# Display the contents of the page
|
|
print(response.text)
|