WiPy USB connectivity and Automatic Home Network Configuration

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
DWiskow
Posts: 11
Joined: Sun Apr 24, 2016 1:23 pm

WiPy USB connectivity and Automatic Home Network Configuration

Post by DWiskow » Sun May 22, 2016 10:04 pm

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

rodrigob
Posts: 15
Joined: Sun Aug 30, 2015 7:41 am

Re: WiPy USB connectivity and Automatic Home Network Configuration

Post by rodrigob » Tue Jun 07, 2016 8:03 am

Great, thanks for sharing the example !

Post Reply