|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +""" |
| 24 | +`adafruit_esp32spi_wifimanager` |
| 25 | +================================================================================ |
| 26 | +
|
| 27 | +WiFi Manager for making ESP32 SPI as WiFi much easier |
| 28 | +
|
| 29 | +* Author(s): Melissa LeBlanc-Williams |
| 30 | +""" |
| 31 | + |
| 32 | +import neopixel |
| 33 | +import adafruit_esp32spi.adafruit_esp32spi_requests as requests |
| 34 | + |
| 35 | +class ESPSPI_WiFiManager: |
| 36 | + """ |
| 37 | + A class to help manage the Wifi connection |
| 38 | + """ |
| 39 | + def __init__(self, esp, settings, status_neopixel=None): |
| 40 | + # Read the settings |
| 41 | + self._esp = esp |
| 42 | + self.debug = False |
| 43 | + self.ssid = settings['ssid'] |
| 44 | + self.password = settings['password'] |
| 45 | + requests.set_interface(self._esp) |
| 46 | + if status_neopixel: |
| 47 | + self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2) |
| 48 | + else: |
| 49 | + self.neopix = None |
| 50 | + self.neo_status(0) |
| 51 | + |
| 52 | + def connect(self): |
| 53 | + if self.debug: |
| 54 | + if self._esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
| 55 | + print("ESP32 found and in idle mode") |
| 56 | + print("Firmware vers.", self._esp.firmware_version) |
| 57 | + print("MAC addr:", [hex(i) for i in self._esp.MAC_address]) |
| 58 | + for ap in self._esp.scan_networks(): |
| 59 | + print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi'])) |
| 60 | + while not self._esp.is_connected: |
| 61 | + try: |
| 62 | + if self.debug: |
| 63 | + print("Connecting to AP...") |
| 64 | + self.neo_status((100, 0, 0)) |
| 65 | + self._esp.connect_AP(bytes(self.ssid,'utf-8'), bytes(self.password,'utf-8')) |
| 66 | + self.neo_status((0, 100, 0)) |
| 67 | + except (ValueError, RuntimeError) as e: |
| 68 | + if self.debug: |
| 69 | + print("Failed to connect, retrying\n", e) |
| 70 | + continue |
| 71 | + |
| 72 | + def get(self, url, **kw): |
| 73 | + self.neo_status((100, 100, 0)) |
| 74 | + return_val = requests.get(url, **kw) |
| 75 | + self.neo_status((0, 0, 100)) |
| 76 | + return return_val |
| 77 | + |
| 78 | + def post(self, url, **kw): |
| 79 | + self.neo_status((100, 100, 0)) |
| 80 | + return_val = requests.post(url, **kw) |
| 81 | + self.neo_status((0, 0, 100)) |
| 82 | + return return_val |
| 83 | + |
| 84 | + def ping(self, host): |
| 85 | + #Blink the LED Green |
| 86 | + #Send Stuff to Requests |
| 87 | + #stop Blinking LED |
| 88 | + #Return Result |
| 89 | + return None |
| 90 | + |
| 91 | + def neo_status(self, value): |
| 92 | + if self.neopix: |
| 93 | + self.neopix.fill(value) |
0 commit comments