Code: Select all
#
# main.py -- auto connect to specified WiFi if available, otherwise fires up 'wipy-wlan-xxxx' Access Point
# -- enables CTRL-D soft resets without loss of connection when using Telnet or FTP
#
import time
import machine
from network import WLAN
wlan = WLAN()
print()
if (wlan.isconnected() and machine.reset_cause() == machine.SOFT_RESET):
print(wlan.ifconfig())
print()
else:
if machine.reset_cause() != machine.SOFT_RESET:
wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
if net.ssid == 'MY SSID':
wlan.ifconfig(config=('192.168.1.255', '255.255.255.0', '192.168.1.1', '8.8.8.8'))
if not wlan.isconnected():
wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
if net.ssid == 'MY SSID':
wlan.ifconfig(config=('192.168.1.255', '255.255.255.0', '192.168.1.1', '8.8.8.8'))
wlan.connect(ssid='MY SSID', auth=(WLAN.WPA2, 'MY PASSWORD'))
currTime = time.time()
while not wlan.isconnected():
if (time.time()-currTime > 15):
break
time.sleep_ms(250)
if wlan.isconnected():
print("WLAN connection to 'MY SSID' WiFi succeeded!")
if not wlan.isconnected():
wlan = WLAN(mode=WLAN.AP)
print("WLAN '"+wlan.ssid()+"' AP WiFi Available!")
print(wlan.ifconfig())
print()
Code: Select all
# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal
from machine import UART
from machine import SD
import os
uart = UART(0, 115200)
os.dupterm(uart)
# clock pin, cmd pin, data0 pin
sd = SD(pins=('GP10', 'GP11', 'GP15'))
sd = SD()
os.mount(sd, '/sd')