Page 1 of 1

WiPy USB connectivity and Automatic Home Network Configuration

Posted: Sun May 22, 2016 10:04 pm
by DWiskow
I configured my WiPy to auto connect to my home WiFi when in range and automatically setup up its default AP when not at home using the following code in 'main.py'

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()
I also configured 'boot.py' to duplicate the terminal console via the UART to the USB on the expansion board and auto mount its micro SD card

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')
The code in 'main.py' enables soft resets [using CTRL-D] without disconnecting a Telnet or FTP session

Re: WiPy USB connectivity and Automatic Home Network Configuration

Posted: Tue Jun 07, 2016 8:03 am
by rodrigob
Great, thanks for sharing the example !