Last active
August 25, 2018 09:31
-
-
Save kamilion/f61079493238f36ab0eba26f0bad89d0 to your computer and use it in GitHub Desktop.
universal wireless manager for micropython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc, network, uos, os, machine, json, time, sys, micropython, display | |
# Start display using the Odroid-GO pins. | |
#tft=display.TFT() | |
#tft.init(tft.ILI9341, width=240, height=320, miso=19, clk=18, mosi=23, dc=21, cs=5, backl_pin=14, backl_on=1, rot=tft.LANDSCAPE_FLIP) | |
# Mount SD using Odroid-GO pins... | |
uos.sdconfig(uos.SDMODE_SPI, clk=18, mosi=23, miso=19, cs=22) | |
os.mountsd() | |
# So that we can get the configuration to bring up the network. | |
from uwm import uwm | |
uwm.setup_network() | |
network.ftp.start() | |
network.telnet.start() | |
# Finally, bring some useful commandline macros in. | |
from upysh import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"timesync": { | |
"hostname": "copper.sllabs.com" | |
}, | |
"configuration": { | |
"fallback": 2 | |
}, | |
"services": { | |
"mdns": true, | |
"workstation": true, | |
"ftp": true, | |
"telnet": true, | |
"web": false, | |
"password": "micropy" | |
}, | |
"access_point": { | |
"essid": "k-mpy-dev", | |
"channel": 11, | |
"hidden": false, | |
"password": "micropy" | |
}, | |
"known_networks": [ | |
["AccessPointA", "PASSw0RD"], | |
["AccessPointB", "w0RDPASS"] | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import utime | |
import os | |
import machine | |
reboot = machine.reset | |
from micropython import const | |
import network | |
START_AP_ALWAYS = const(0) | |
START_AP_FALLBACK = const(1) | |
START_AP_NEVER = const(2) | |
class uwm: | |
current_ssid = None | |
current_ifconfig = None | |
start_ap = START_AP_NEVER | |
@classmethod | |
def mdns(cls): | |
return network.mDNS() | |
@classmethod | |
def getssid(cls): | |
return cls.current_ssid | |
@classmethod | |
def getifconfig(cls): | |
return cls.wlan().ifconfig() | |
@classmethod | |
def wlan(cls): | |
return network.WLAN(network.STA_IF) | |
@classmethod | |
def accesspoint(cls): | |
return network.WLAN(network.AP_IF) | |
@classmethod | |
def wants_accesspoint(cls) -> bool: | |
if cls.start_ap == START_AP_ALWAYS: | |
return True | |
elif cls.start_ap == START_AP_FALLBACK: | |
return not cls.wlan().isconnected() | |
else: | |
return False | |
@classmethod | |
def setup_network(cls, config_file='/sd/mpy/networks.json') -> bool: | |
try: | |
with open(config_file, "r") as f: | |
config = json.loads(f.read()) | |
preferred_networks = config['known_networks'] | |
ap_config = config["access_point"] | |
services = config["services"] | |
start_ap = config["configuration"]["fallback"] | |
except OSError: | |
print('ODROID-GO had error loading config file, no known networks selected.') | |
preferred_networks = [] | |
cls.wlan().active(True) | |
available_networks = {} | |
print("ODROID-GO searching for networks...") | |
for network in cls.wlan().scan(): | |
available_networks[network[0].decode("utf-8")] = network[1:] | |
for preference in [p for p in preferred_networks if p[0] in available_networks]: | |
print("ODROID-GO connecting to network {0}...".format(preference[0])) | |
if cls.connect_to(network_ssid=preference[0], password=preference[1]): | |
break | |
if services["mdns"]: | |
utime.sleep_ms(1500) | |
print("ODROID-GO enabling your mdns...") | |
_ = cls.mdns().start("ODROID-GO", "MicroPython-ESP32 with mDNS") | |
_ = cls.mdns().addService('_workstation', '_tcp', 9, "MicroPython", {"board": "ESP32", "service": "discard any characters (Port 9)", "closed": "True", "thodin": "True"}) | |
if cls.wants_accesspoint(): | |
print("ODROID-GO enabling your access point...") | |
cls.accesspoint().config(**ap_config) | |
cls.accesspoint().active(cls.wants_accesspoint()) | |
return cls.wlan().isconnected() | |
@classmethod | |
def connect_to(cls, *, network_ssid: str, password: str) -> bool: | |
cls.wlan().connect(network_ssid, password) | |
for check in range(0, 10): | |
if cls.wlan().isconnected(): | |
cls.current_ssid = network_ssid | |
cls.current_ifconfig = cls.wlan().ifconfig() | |
return True | |
utime.sleep_ms(500) | |
return False | |
# Define the WiFi events callback function | |
@classmethod | |
def wifi_print_cb(cls, info): | |
print("[WiFi] event: {} ({})".format(info[0], info[1])) | |
if (info[2]): | |
print(" info: {}".format(info[2])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment