pixoPython/pixo/wifi.py

64 lines
5.1 KiB
Python
Raw Normal View History

2020-01-26 23:17:53 +00:00
# Setup network
import network
import creds
import time
from pixels import Pixo
2020-01-26 23:59:30 +00:00
WIFI_IMG = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (253, 253, 253), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
2020-01-26 23:17:53 +00:00
2020-01-27 01:15:25 +00:00
# We will attempt connections for this many seconds
MAX_WAIT = 15
2020-01-26 23:17:53 +00:00
# Setup the wifi connection
STA_IF = network.WLAN(network.STA_IF)
AP_IF = network.WLAN(network.AP_IF)
def is_connected(interface):
if interface == STA_IF:
return interface.isconnected()
if interface == AP_IF:
return (len(interface.status('stations')) > 0)
2020-01-27 01:47:38 +00:00
def connect(interface, interface_name, ssid, password):
2020-01-27 01:15:25 +00:00
print("Configuring %s interface with credentials from creds.py" % interface_name)
2020-01-26 23:17:53 +00:00
start_time = time.time()
2020-01-27 01:15:25 +00:00
# Start the interface if it's not already
interface.active(True)
2020-01-27 01:47:38 +00:00
# Connect to the network
2020-01-27 01:15:25 +00:00
if interface == STA_IF:
2020-01-27 01:47:38 +00:00
STA_IF.connect(ssid, password)
2020-01-27 01:15:25 +00:00
selected_color = (200, 40, 40)
elif interface == AP_IF:
2020-01-27 01:47:38 +00:00
AP_IF.config(essid=ssid, password=password)
2020-01-27 01:15:25 +00:00
selected_color = (20, 10, 230)
2020-01-26 23:17:53 +00:00
2020-01-27 01:15:25 +00:00
while not interface.isconnected():
Pixo().blink_image(WIFI_IMG, selected_color)
2020-01-26 23:17:53 +00:00
if time.time() - start_time >= MAX_WAIT:
2020-01-27 01:15:25 +00:00
print("Took too much time to connect to %s interface" % interface_name)
2020-01-26 23:17:53 +00:00
break
if is_connected(interface):
2020-01-27 01:18:42 +00:00
Pixo().fill_image(WIFI_IMG, False).force_color((40, 200, 40))
if not STA_IF.isconnected():
2020-01-27 01:47:38 +00:00
for network in creds.WIFI_NETWORKS:
if STA_IF.isconnected():
break
2020-01-27 01:50:51 +00:00
connect(STA_IF, 'Standard', network[0], network[1])
2020-01-27 01:18:42 +00:00
# We check this twice to enable AP mode if we failed to connect
# Normally this happens if the password has been misconfigured,
# or if the device moves to a different location (more likely)
if not STA_IF.isconnected():
# Disable STA_IF to stop serial spam
# You can enable it again by doing `import wifi`
# Followed by `wifi.STA_IF.active(True)` - Then you can call the `wifi.connect_standard`
# method to try connecting again (after `creds.py` has been fixed)
STA_IF.active(False)
2020-01-27 01:47:38 +00:00
connect(AP_IF, 'Access Point', creds.AP_SSID, creds.AP_PASSWORD)