Skip to content

Expose ESP object in WiFiManager #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def __init__(self, esp, secrets, status_pixel=None, attempts=2):
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
"""
# Read the settings
self._esp = esp
self.esp = esp
self.debug = False
self.ssid = secrets['ssid']
self.password = secrets['password']
self.attempts = attempts
requests.set_interface(self._esp)
requests.set_interface(self.esp)
self.statuspix = status_pixel
self.pixel_status(0)

Expand All @@ -63,26 +63,26 @@ def reset(self):
"""
if self.debug:
print("Resetting ESP32")
self._esp.reset()
self.esp.reset()

def connect(self):
"""
Attempt to connect to WiFi using the current settings
"""
if self.debug:
if self._esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
if self.esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware vers.", self._esp.firmware_version)
print("MAC addr:", [hex(i) for i in self._esp.MAC_address])
for access_pt in self._esp.scan_networks():
print("Firmware vers.", self.esp.firmware_version)
print("MAC addr:", [hex(i) for i in self.esp.MAC_address])
for access_pt in self.esp.scan_networks():
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
failure_count = 0
while not self._esp.is_connected:
while not self.esp.is_connected:
try:
if self.debug:
print("Connecting to AP...")
self.pixel_status((100, 0, 0))
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
self.esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
failure_count = 0
self.pixel_status((0, 100, 0))
except (ValueError, RuntimeError) as error:
Expand All @@ -105,7 +105,7 @@ def get(self, url, **kw):
:return: The response from the request
:rtype: Response
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
return_val = requests.get(url, **kw)
Expand All @@ -124,7 +124,7 @@ def post(self, url, **kw):
:return: The response from the request
:rtype: Response
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
return_val = requests.post(url, **kw)
Expand All @@ -142,7 +142,7 @@ def put(self, url, **kw):
:return: The response from the request
:rtype: Response
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
return_val = requests.put(url, **kw)
Expand All @@ -161,7 +161,7 @@ def patch(self, url, **kw):
:return: The response from the request
:rtype: Response
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
return_val = requests.patch(url, **kw)
Expand All @@ -180,7 +180,7 @@ def delete(self, url, **kw):
:return: The response from the request
:rtype: Response
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
return_val = requests.delete(url, **kw)
Expand All @@ -196,22 +196,22 @@ def ping(self, host, ttl=250):
:return: The response time in milliseconds
:rtype: int
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
response_time = self._esp.ping(host, ttl=ttl)
response_time = self.esp.ping(host, ttl=ttl)
self.pixel_status(0)
return response_time

def ip_address(self):
"""
Returns a formatted local IP address, update status pixel.
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
self.pixel_status((0, 0, 100))
self.pixel_status(0)
return self._esp.pretty_ip(self._esp.ip_address)
return self.esp.pretty_ip(self.esp.ip_address)

def pixel_status(self, value):
"""
Expand All @@ -230,6 +230,6 @@ def signal_strength(self):
"""
Returns receiving signal strength indicator in dBm
"""
if not self._esp.is_connected:
if not self.esp.is_connected:
self.connect()
return self._esp.rssi()
return self.esp.rssi()