Skip to content

Commit ec03d18

Browse files
authored
Merge pull request #2124 from kattni/micropython-wifi
Adding MicroPython WiFi example.
2 parents 654c5ce + 0bbf674 commit ec03d18

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

MicroPython_WiFi/main.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)