|
| 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, ladyada |
| 30 | +""" |
| 31 | + |
| 32 | +# pylint: disable=no-name-in-module |
| 33 | + |
| 34 | +import neopixel |
| 35 | +import adafruit_esp32spi |
| 36 | +import adafruit_esp32spi.adafruit_esp32spi_requests as requests |
| 37 | + |
| 38 | +class ESPSPI_WiFiManager: |
| 39 | + """ |
| 40 | + A class to help manage the Wifi connection |
| 41 | + """ |
| 42 | + def __init__(self, esp, settings, status_neopixel=None): |
| 43 | + """ |
| 44 | + :param ESP_SPIcontrol esp: The ESP object we are using |
| 45 | + :param dict settings: The WiFi and Adafruit IO Settings (See examples) |
| 46 | + :param status_neopixel: (Pptional) The neopixel pin - Usually board.NEOPIXEL (default=None) |
| 47 | + :type status_neopixel: Pin |
| 48 | + """ |
| 49 | + # Read the settings |
| 50 | + self._esp = esp |
| 51 | + self.debug = False |
| 52 | + self.ssid = settings['ssid'] |
| 53 | + self.password = settings['password'] |
| 54 | + requests.set_interface(self._esp) |
| 55 | + if status_neopixel: |
| 56 | + self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2) |
| 57 | + else: |
| 58 | + self.neopix = None |
| 59 | + self.neo_status(0) |
| 60 | + |
| 61 | + def connect(self): |
| 62 | + """ |
| 63 | + Attempt to connect to WiFi using the current settings |
| 64 | + """ |
| 65 | + if self.debug: |
| 66 | + if self._esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
| 67 | + print("ESP32 found and in idle mode") |
| 68 | + print("Firmware vers.", self._esp.firmware_version) |
| 69 | + print("MAC addr:", [hex(i) for i in self._esp.MAC_address]) |
| 70 | + for access_pt in self._esp.scan_networks(): |
| 71 | + print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi'])) |
| 72 | + while not self._esp.is_connected: |
| 73 | + try: |
| 74 | + if self.debug: |
| 75 | + print("Connecting to AP...") |
| 76 | + self.neo_status((100, 0, 0)) |
| 77 | + self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8')) |
| 78 | + self.neo_status((0, 100, 0)) |
| 79 | + except (ValueError, RuntimeError) as error: |
| 80 | + print("Failed to connect, retrying\n", error) |
| 81 | + continue |
| 82 | + |
| 83 | + def get(self, url, **kw): |
| 84 | + """ |
| 85 | + Pass the Get request to requests and update Status NeoPixel |
| 86 | +
|
| 87 | + :param str url: The URL to retrieve data from |
| 88 | + :param dict data: (Optional) Form data to submit |
| 89 | + :param dict json: (Optional) JSON data to submit. (Data must be None) |
| 90 | + :param dict header: (Optional) Header data to include |
| 91 | + :param bool stream: (Optional) Whether to stream the Response |
| 92 | + :return: The response from the request |
| 93 | + :rtype: Response |
| 94 | + """ |
| 95 | + if not self._esp.is_connected: |
| 96 | + self.connect() |
| 97 | + self.neo_status((100, 100, 0)) |
| 98 | + return_val = requests.get(url, **kw) |
| 99 | + self.neo_status(0) |
| 100 | + return return_val |
| 101 | + |
| 102 | + def post(self, url, **kw): |
| 103 | + """ |
| 104 | + Pass the Post request to requests and update Status NeoPixel |
| 105 | +
|
| 106 | + :param str url: The URL to post data to |
| 107 | + :param dict data: (Optional) Form data to submit |
| 108 | + :param dict json: (Optional) JSON data to submit. (Data must be None) |
| 109 | + :param dict header: (Optional) Header data to include |
| 110 | + :param bool stream: (Optional) Whether to stream the Response |
| 111 | + :return: The response from the request |
| 112 | + :rtype: Response |
| 113 | + """ |
| 114 | + if not self._esp.is_connected: |
| 115 | + self.connect() |
| 116 | + self.neo_status((100, 100, 0)) |
| 117 | + return_val = requests.post(url, **kw) |
| 118 | + self.neo_status(0) |
| 119 | + return return_val |
| 120 | + |
| 121 | + def ping(self, host, ttl=250): |
| 122 | + """ |
| 123 | + Pass the Ping request to the ESP32, update Status NeoPixel, return response time |
| 124 | +
|
| 125 | + :param str host: The hostname or IP address to ping |
| 126 | + :param int ttl: (Optional) The Time To Live in milliseconds for the packet (default=250) |
| 127 | + :return: The response time in milliseconds |
| 128 | + :rtype: int |
| 129 | + """ |
| 130 | + if not self._esp.is_connected: |
| 131 | + self.connect() |
| 132 | + self.neo_status((100, 100, 0)) |
| 133 | + response_time = self._esp.ping(host, ttl=ttl) |
| 134 | + self.neo_status(0) |
| 135 | + return response_time |
| 136 | + |
| 137 | + def neo_status(self, value): |
| 138 | + """ |
| 139 | + Change Status NeoPixel if it was defined |
| 140 | +
|
| 141 | + :param value: The value to set the Board's Status NeoPixel to |
| 142 | + :type value: int or 3-value tuple |
| 143 | + """ |
| 144 | + if self.neopix: |
| 145 | + self.neopix.fill(value) |
0 commit comments