|
| 1 | +# SPDX-FileCopyrightText: 2022 Ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""MicroPython simple WiFi connection demo""" |
| 4 | +import time |
| 5 | +import network |
| 6 | +import urequests |
| 7 | + |
| 8 | +station = network.WLAN(network.STA_IF) |
| 9 | +station.active(True) |
| 10 | + |
| 11 | +# Network settings |
| 12 | +wifi_ssid = "MY_SSID" |
| 13 | +wifi_password = "MY_PASSWORD" |
| 14 | +url = "http://wifitest.adafruit.com/testwifi/index.html" |
| 15 | + |
| 16 | +print("Scanning for WiFi networks, please wait...") |
| 17 | +authmodes = ['Open', 'WEP', 'WPA-PSK', 'WPA2-PSK4', 'WPA/WPA2-PSK'] |
| 18 | +for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan(): |
| 19 | + print("* {:s}".format(ssid)) |
| 20 | + print(" - Channel: {}".format(channel)) |
| 21 | + print(" - RSSI: {}".format(RSSI)) |
| 22 | + print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid)) |
| 23 | + print(" - Auth: {}".format(authmodes[authmode])) |
| 24 | + print() |
| 25 | + |
| 26 | +# Continually try to connect to WiFi access point |
| 27 | +while not station.isconnected(): |
| 28 | + # Try to connect to WiFi access point |
| 29 | + print("Connecting...") |
| 30 | + station.connect(wifi_ssid, wifi_password) |
| 31 | + time.sleep(10) |
| 32 | + |
| 33 | +# Display connection details |
| 34 | +print("Connected!") |
| 35 | +print("My IP Address:", station.ifconfig()[0]) |
| 36 | + |
| 37 | +# Perform HTTP GET request on a non-SSL web |
| 38 | +response = urequests.get(url) |
| 39 | + |
| 40 | +# Display the contents of the page |
| 41 | +print(response.text) |
0 commit comments