mirror of
https://github.com/c0de-archive/pixoPython.git
synced 2024-12-22 05:12:41 +00:00
76 lines
5.4 KiB
Python
76 lines
5.4 KiB
Python
# Setup network
|
|
import network
|
|
import creds
|
|
import time
|
|
|
|
from pixels import Pixo
|
|
from machine import Timer
|
|
|
|
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)]
|
|
|
|
time_connected = time.time()
|
|
|
|
# We will attempt connections for this many seconds
|
|
MAX_WAIT = 15
|
|
|
|
# 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)
|
|
|
|
def connect(interface, interface_name, ssid, password):
|
|
print("Configuring %s interface with credentials from creds.py" % interface_name)
|
|
start_time = time.time()
|
|
|
|
# Start the interface if it's not already
|
|
interface.active(True)
|
|
|
|
# Connect to the network
|
|
if interface == STA_IF:
|
|
STA_IF.connect(ssid, password)
|
|
selected_color = (200, 40, 40)
|
|
elif interface == AP_IF:
|
|
AP_IF.config(essid=ssid, password=password)
|
|
selected_color = (20, 10, 230)
|
|
|
|
while not interface.isconnected():
|
|
Pixo().blink_image(WIFI_IMG, selected_color)
|
|
|
|
if time.time() - start_time >= MAX_WAIT:
|
|
print("Took too much time to connect to %s interface" % interface_name)
|
|
break
|
|
|
|
if is_connected(interface):
|
|
time_connected = time.time()
|
|
Pixo().fill_image(WIFI_IMG, False).force_color((40, 200, 40))
|
|
|
|
if not STA_IF.isconnected():
|
|
for network in creds.WIFI_NETWORKS:
|
|
if STA_IF.isconnected():
|
|
break
|
|
connect(STA_IF, 'Standard', network[0], network[1])
|
|
|
|
# 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)
|
|
|
|
connect(AP_IF, 'Access Point', creds.AP_SSID, creds.AP_PASSWORD)
|
|
|
|
# Clear the board with dissolve animation after 15 seconds
|
|
# We'll assume that the board is showing a green wifi logo
|
|
Timer(-1).init(
|
|
period=15000,
|
|
mode=Timer.ONE_SHOT,
|
|
callback=lambda t: Pixo().transition_image_dissolve())
|