Adding MicroPython WiFi example.
This commit is contained in:
parent
3b82052b0b
commit
0bbf674460
1 changed files with 41 additions and 0 deletions
41
MicroPython_WiFi/main.py
Normal file
41
MicroPython_WiFi/main.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# 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)
|
||||
Loading…
Reference in a new issue