|
1 | | -# Presto WiFi |
| 1 | +# EzWiFi <!--omit in toc--> |
| 2 | + |
| 3 | +- [EzWiFi ](#ezwifi-) |
| 4 | + - [Easy EzWiFi](#easy-ezwifi) |
| 5 | + - [EzWiFi Class](#ezwifi-class) |
| 6 | + - [Asyncio](#asyncio) |
| 7 | + - [Connect Options](#connect-options) |
| 8 | + - [Handlers](#handlers) |
| 9 | + - [Other Functions](#other-functions) |
| 10 | + |
| 11 | +EzWiFi, or Easy WiFi, is a helper module to get you connected to wireless networks. |
| 12 | + |
| 13 | +It's based around the use of `secrets.py`, a Python file that very simply tucks |
| 14 | +your wireless SSID and password away for use across multiple scripts. |
| 15 | + |
| 16 | +`secrets.py` looks like this: |
| 17 | + |
| 18 | +```python |
| 19 | +WIFI_SSID = "your_ssid" |
| 20 | +WIFI_PASSWORD = "your_password" |
| 21 | +``` |
| 22 | + |
| 23 | +## Easy EzWiFi |
| 24 | + |
| 25 | +The easiest way to use EzWiFi is with the blocking `connect` method, like so: |
| 26 | + |
| 27 | +```python |
| 28 | +import ezwifi |
| 29 | + |
| 30 | +ezwifi.connect() |
| 31 | +``` |
| 32 | + |
| 33 | +This will load login details from `secrets.py` and quietly connect to your |
| 34 | +wireless network. It will try ten times by default with an overall timeout |
| 35 | +of 60 seconds. |
| 36 | + |
| 37 | +If you need a little more debugging information you can |
| 38 | +supply a log handler like so: |
| 39 | + |
| 40 | +```python |
| 41 | +import ezwifi |
| 42 | + |
| 43 | +ezwifi.connect(verbose=True) |
| 44 | +``` |
| 45 | + |
| 46 | +If you need specific log messages, want to perform an action or display a message |
| 47 | +on screen when connected/failed then you can supply: |
| 48 | + |
| 49 | +* `connected` - Called when a connection is established (with no message). |
| 50 | +* `failed` - Called when the connection fails. |
| 51 | +* `info` - Called with basic info messages. |
| 52 | +* `warning` - Called when a connection attempt fails (and in other cases in future). |
| 53 | +* `error` - Called when a connection totally fails (and in other cases in future). |
| 54 | +* `failed` - Called when a connection fails (with no message). |
| 55 | + |
| 56 | +For example, this will call a function if/when the connection succeeds or fails: |
| 57 | + |
| 58 | +```python |
| 59 | +import ezwifi |
| 60 | + |
| 61 | + |
| 62 | +def connect_handler(wifi): |
| 63 | + pass |
| 64 | + |
| 65 | + |
| 66 | +def failed_handler(wifi): |
| 67 | + pass |
| 68 | + |
| 69 | + |
| 70 | +ezwifi.connect(connected=connect_handler, failed=failed_handler) |
| 71 | +``` |
| 72 | + |
| 73 | +## EzWiFi Class |
| 74 | + |
| 75 | +EzWiFi is also available as a class if you need to integrate with async. |
| 76 | + |
| 77 | +Create an instance with: |
| 78 | + |
| 79 | +```python |
| 80 | +from ezwifi import EzWiFi |
| 81 | + |
| 82 | +wifi = EzWiFi() |
| 83 | +``` |
| 84 | + |
| 85 | +You can then use the async `connect()` method, if you're using the class and |
| 86 | +want this to run in synchronous code you'll need to: |
| 87 | + |
| 88 | +```python |
| 89 | +import asyncio |
| 90 | + |
| 91 | +asyncio.get_event_loop().run_until_complete(wifi.connect()) |
| 92 | +``` |
| 93 | + |
| 94 | +### Asyncio |
| 95 | + |
| 96 | +With asyncio you can do other things while waiting for WiFi to connect, like so: |
| 97 | + |
| 98 | +```python |
| 99 | +import asyncio |
| 100 | +from ezwifi import EzWiFi |
| 101 | + |
| 102 | +wifi = EzWiFi() |
| 103 | + |
| 104 | + |
| 105 | +@wifi.on("connected") |
| 106 | +async def handle_connect(wifi): |
| 107 | + print("Connected!") |
| 108 | + |
| 109 | + |
| 110 | +@wifi.on("failed") |
| 111 | +async def handle_connect(wifi): |
| 112 | + print("Failed!") |
| 113 | + |
| 114 | + |
| 115 | +async def main(): |
| 116 | + wifi_task = asyncio.create_task(wifi.connect()) |
| 117 | + while True: |
| 118 | + print("Main loop...") |
| 119 | + await asyncio.sleep_ms(1000) |
| 120 | + |
| 121 | + |
| 122 | +asyncio.run(main()) |
| 123 | +``` |
| 124 | + |
| 125 | +### Connect Options |
| 126 | + |
| 127 | +You can supply an optional `ssid`, `password`, `timeout` and number of retries to |
| 128 | +`connect()`: |
| 129 | + |
| 130 | +### Handlers |
| 131 | + |
| 132 | +If you need specific log messages, want to perform an action or display a message |
| 133 | +on screen when connected/failed then you use the following handlers: |
| 134 | + |
| 135 | +* `connected` - Called when a connection is established (with no message). |
| 136 | +* `failed` - Called when the connection fails. |
| 137 | +* `info` - Called with basic info messages. |
| 138 | +* `warning` - Called when a connection attempt fails (and in other cases in future). |
| 139 | +* `error` - Called when a connection totally fails (and in other cases in future). |
| 140 | +* `failed` - Called when a connection fails (with no message). |
| 141 | + |
| 142 | +These can be supplied to EzWiFi as an argument, eg: |
| 143 | + |
| 144 | +```python |
| 145 | +import asyncio |
| 146 | +from ezwifi import EzWiFi |
| 147 | + |
| 148 | + |
| 149 | +async def info_handler(wifi, message): |
| 150 | + print(message) |
| 151 | + |
| 152 | + |
| 153 | +wifi = EzWiFi(info=info_handler) |
| 154 | + |
| 155 | + |
| 156 | +async def main(): |
| 157 | + wifi_task = asyncio.create_task(wifi.connect()) |
| 158 | + while True: |
| 159 | + print("Main loop...") |
| 160 | + await asyncio.sleep_ms(1000) |
| 161 | + |
| 162 | + |
| 163 | +asyncio.run(main()) |
| 164 | +``` |
| 165 | + |
| 166 | +Or by using the `wifi.on` decorator: |
| 167 | + |
| 168 | +```python |
| 169 | +import asyncio |
| 170 | +from ezwifi import EzWiFi |
| 171 | + |
| 172 | +wifi = EzWiFi() |
| 173 | + |
| 174 | + |
| 175 | +@wifi.on("info") |
| 176 | +async def info_handler(wifi, message): |
| 177 | + print(message) |
| 178 | + |
| 179 | + |
| 180 | +async def main(): |
| 181 | + wifi_task = asyncio.create_task(wifi.connect()) |
| 182 | + while True: |
| 183 | + print("Main loop...") |
| 184 | + await asyncio.sleep_ms(1000) |
| 185 | + |
| 186 | + |
| 187 | +asyncio.run(main()) |
| 188 | +``` |
| 189 | + |
| 190 | +### Other Functions |
| 191 | + |
| 192 | +* `ipv4` - returns the ipv4 address, shortcut for `if.ipconfig("addr4")[0]` |
| 193 | +* `ipvv6` - returns the ipv4 address, shortcut for `if.ipconfig("addr6")[0]` |
| 194 | +* `isconnected` - returns the connection status |
0 commit comments