kopia lustrzana https://github.com/pimoroni/pimoroni-pico
PicoW: Add common files and refactor.
rodzic
afea765b71
commit
4ffb94e1ad
|
@ -7,7 +7,7 @@ import uasyncio
|
|||
class NetworkManager:
|
||||
_ifname = ("Client", "Access Point")
|
||||
|
||||
def __init__(self, country="GB", client_timeout=30, access_point_timeout=5, status_handler=None):
|
||||
def __init__(self, country="GB", client_timeout=30, access_point_timeout=5, status_handler=None, error_handler=None):
|
||||
rp2.country(country)
|
||||
self._ap_if = network.WLAN(network.AP_IF)
|
||||
self._sta_if = network.WLAN(network.STA_IF)
|
||||
|
@ -16,11 +16,27 @@ class NetworkManager:
|
|||
self._client_timeout = client_timeout
|
||||
self._access_point_timeout = access_point_timeout
|
||||
self._status_handler = status_handler
|
||||
self._error_handler = error_handler
|
||||
self.UID = ("{:02X}" * 8).format(*machine.unique_id())
|
||||
|
||||
def isconnected(self):
|
||||
return self._sta_if.isconnected() or self._ap_if.isconnected()
|
||||
|
||||
def config(self, var):
|
||||
if self._sta_if.active():
|
||||
return self._sta_if.config(var)
|
||||
else:
|
||||
if var == "password":
|
||||
return self.UID
|
||||
return self._ap_if.config(var)
|
||||
|
||||
def mode(self):
|
||||
if self._sta_if.isconnected():
|
||||
return self._ifname[0]
|
||||
if self._ap_if.isconnected():
|
||||
return self._ifname[1]
|
||||
return None
|
||||
|
||||
def ifaddress(self):
|
||||
if self._sta_if.isconnected():
|
||||
return self._sta_if.ifconfig()[0]
|
||||
|
@ -40,18 +56,23 @@ class NetworkManager:
|
|||
await uasyncio.sleep_ms(1000)
|
||||
|
||||
def _handle_status(self, mode, status):
|
||||
if self._status_handler is not None:
|
||||
if callable(self._status_handler):
|
||||
self._status_handler(self._ifname[mode], status, self.ifaddress())
|
||||
|
||||
def _handle_error(self, mode, msg):
|
||||
if callable(self._error_handler):
|
||||
if self._error_handler(self._ifname[mode], msg):
|
||||
return
|
||||
raise RuntimeError(msg)
|
||||
|
||||
async def client(self, ssid, psk):
|
||||
if self._sta_if.isconnected():
|
||||
self._handle_status(network.STA_IF, True)
|
||||
return
|
||||
|
||||
self._ap_if.disconnect()
|
||||
self._ap_if.active(False)
|
||||
self._ap_if.deinit()
|
||||
|
||||
self._sta_if = network.WLAN(network.STA_IF)
|
||||
self._sta_if.active(True)
|
||||
self._sta_if.connect(ssid, psk)
|
||||
|
||||
|
@ -62,17 +83,18 @@ class NetworkManager:
|
|||
except uasyncio.TimeoutError:
|
||||
self._sta_if.active(False)
|
||||
self._handle_status(network.STA_IF, False)
|
||||
raise RuntimeError("WIFI Client Failed")
|
||||
self._handle_error(network.STA_IF, "WIFI Client Failed")
|
||||
|
||||
async def access_point(self):
|
||||
if self._ap_if.isconnected():
|
||||
self._handle_status(network.AP_IF, True)
|
||||
return
|
||||
|
||||
self._sta_if.disconnect()
|
||||
self._sta_if.active(False)
|
||||
self._sta_if.deinit()
|
||||
|
||||
self._ap_if = network.WLAN(network.AP_IF)
|
||||
self._ap_if.ifconfig(("10.10.1.1", "255.255.255.0", "10.10.1.1", "10.10.1.1"))
|
||||
self._ap_if.config(password=self.UID)
|
||||
self._ap_if.active(True)
|
||||
|
||||
try:
|
||||
|
@ -82,4 +104,4 @@ class NetworkManager:
|
|||
except uasyncio.TimeoutError:
|
||||
self._sta_if.active(False)
|
||||
self._handle_status(network.AP_IF, False)
|
||||
raise RuntimeError("WIFI AP Failed")
|
||||
self._handle_error(network.AP_IF, "WIFI Client Failed")
|
|
@ -62,7 +62,7 @@ Plug a [BME680](https://shop.pimoroni.com/products/bme680-breakout) or [BME688](
|
|||
|
||||
## Wireless Examples
|
||||
|
||||
The wireless examples need `network_manager.py` and `WIFI_CONFIG.py` to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
|
||||
The wireless examples need `network_manager.py` and `WIFI_CONFIG.py` from the `common` directory to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
|
||||
|
||||
You'll also need to install the `micropython-urllib.urequest` library using Thonny's 'Tools' > 'Manage Packages'.
|
||||
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
SSID = ""
|
||||
PSK = ""
|
|
@ -14,9 +14,6 @@ If you wire up a four legged RGB LED to GP0, GP1 and GP2 (and the long leg to gr
|
|||
it will change the colour of that too!
|
||||
"""
|
||||
|
||||
# WIFI settings
|
||||
WIFI_COUNTRY = "GB" # Changeme!
|
||||
|
||||
URL = "http://api.thingspeak.com/channels/1417/field/2/last.json"
|
||||
UPDATE_INTERVAL = 120 # refresh interval in secs. Be nice to free APIs!
|
||||
|
||||
|
@ -47,7 +44,7 @@ def hex_to_rgb(hex):
|
|||
|
||||
|
||||
# set up wifi
|
||||
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
|
||||
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
|
||||
|
||||
# set up the display
|
||||
display = PicoGraphics(display=DISPLAY_PICO_EXPLORER)
|
||||
|
|
|
@ -29,7 +29,7 @@ Demonstrates a fast update speed.
|
|||
|
||||
## Wireless Examples
|
||||
|
||||
The wireless examples need `network_manager.py` and `WIFI_CONFIG.py` to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
|
||||
The wireless examples need `network_manager.py` and `WIFI_CONFIG.py` from the `common` directory to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
|
||||
|
||||
You'll also need to install the `micropython-urllib.urequest` library using Thonny's 'Tools' > 'Manage Packages'.
|
||||
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
SSID = ""
|
||||
PSK = ""
|
|
@ -12,8 +12,6 @@ from pimoroni import Button
|
|||
Simple demo to get a random activity from BoredAPI.com
|
||||
"""
|
||||
|
||||
WIFI_COUNTRY = "GB" # Changeme!
|
||||
|
||||
|
||||
button_a = Button(12)
|
||||
button_b = Button(13)
|
||||
|
@ -44,7 +42,7 @@ def status_handler(mode, status, ip):
|
|||
graphics.update()
|
||||
|
||||
|
||||
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
|
||||
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
|
||||
|
||||
|
||||
def update():
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
import rp2
|
||||
import network
|
||||
import machine
|
||||
import uasyncio
|
||||
|
||||
|
||||
class NetworkManager:
|
||||
_ifname = ("Client", "Access Point")
|
||||
|
||||
def __init__(self, country="GB", client_timeout=30, access_point_timeout=5, status_handler=None):
|
||||
rp2.country(country)
|
||||
self._ap_if = network.WLAN(network.AP_IF)
|
||||
self._sta_if = network.WLAN(network.STA_IF)
|
||||
|
||||
self._mode = network.STA_IF
|
||||
self._client_timeout = client_timeout
|
||||
self._access_point_timeout = access_point_timeout
|
||||
self._status_handler = status_handler
|
||||
self.UID = ("{:02X}" * 8).format(*machine.unique_id())
|
||||
|
||||
def isconnected(self):
|
||||
return self._sta_if.isconnected() or self._ap_if.isconnected()
|
||||
|
||||
def ifaddress(self):
|
||||
if self._sta_if.isconnected():
|
||||
return self._sta_if.ifconfig()[0]
|
||||
if self._ap_if.isconnected():
|
||||
return self._ap_if.ifconfig()[0]
|
||||
return '0.0.0.0'
|
||||
|
||||
def disconnect(self):
|
||||
if self._sta_if.isconnected():
|
||||
self._sta_if.disconnect()
|
||||
if self._ap_if.isconnected():
|
||||
self._ap_if.disconnect()
|
||||
|
||||
async def wait(self, mode):
|
||||
while not self.isconnected():
|
||||
self._handle_status(mode, None)
|
||||
await uasyncio.sleep_ms(1000)
|
||||
|
||||
def _handle_status(self, mode, status):
|
||||
if self._status_handler is not None:
|
||||
self._status_handler(self._ifname[mode], status, self.ifaddress())
|
||||
|
||||
async def client(self, ssid, psk):
|
||||
if self._sta_if.isconnected():
|
||||
return
|
||||
|
||||
self._ap_if.disconnect()
|
||||
self._ap_if.active(False)
|
||||
self._ap_if.deinit()
|
||||
|
||||
self._sta_if = network.WLAN(network.STA_IF)
|
||||
self._sta_if.active(True)
|
||||
self._sta_if.connect(ssid, psk)
|
||||
|
||||
try:
|
||||
await uasyncio.wait_for(self.wait(network.STA_IF), self._client_timeout)
|
||||
self._handle_status(network.STA_IF, True)
|
||||
|
||||
except uasyncio.TimeoutError:
|
||||
self._sta_if.active(False)
|
||||
self._handle_status(network.STA_IF, False)
|
||||
raise RuntimeError("WIFI Client Failed")
|
||||
|
||||
async def access_point(self):
|
||||
if self._ap_if.isconnected():
|
||||
return
|
||||
|
||||
self._sta_if.disconnect()
|
||||
self._sta_if.active(False)
|
||||
self._sta_if.deinit()
|
||||
|
||||
self._ap_if = network.WLAN(network.AP_IF)
|
||||
self._ap_if.active(True)
|
||||
|
||||
try:
|
||||
await uasyncio.wait_for(self.wait(network.AP_IF), self._access_point_timeout)
|
||||
self._handle_status(network.AP_IF, True)
|
||||
|
||||
except uasyncio.TimeoutError:
|
||||
self._sta_if.active(False)
|
||||
self._handle_status(network.AP_IF, False)
|
||||
raise RuntimeError("WIFI AP Failed")
|
|
@ -15,8 +15,6 @@ Optionally cache to flash, if you need the RAM
|
|||
for something else.
|
||||
"""
|
||||
|
||||
WIFI_COUNTRY = "GB" # Changeme!
|
||||
|
||||
|
||||
graphics = PicoGraphics(DISPLAY_INKY_PACK)
|
||||
|
||||
|
@ -46,7 +44,7 @@ def status_handler(mode, status, ip):
|
|||
graphics.set_font("bitmap8")
|
||||
graphics.set_update_speed(1)
|
||||
|
||||
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
|
||||
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
|
||||
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
|
||||
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ from picographics import PicoGraphics, DISPLAY_INKY_PACK
|
|||
Grab the quote of the day from Wikipedia.
|
||||
"""
|
||||
|
||||
WIFI_COUNTRY = "GB" # Changeme!
|
||||
|
||||
graphics = PicoGraphics(DISPLAY_INKY_PACK)
|
||||
|
||||
|
@ -49,7 +48,7 @@ def status_handler(mode, status, ip):
|
|||
graphics.update()
|
||||
|
||||
|
||||
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
|
||||
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
|
||||
|
||||
while True:
|
||||
graphics.set_font("bitmap8")
|
||||
|
|
Ładowanie…
Reference in New Issue