-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp32connection.py
116 lines (88 loc) · 3.69 KB
/
esp32connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# The MIT License (MIT)
#
# Copyright (c) 2020 Jim Bennett
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
circuitpython_esp32connection
================================================================================
A WiFi connection helper for ESP32-based boards
* Author(s): Jim Bennett
Implementation Notes
--------------------
**Hardware:**
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
* Adafruit's Bus Device library:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""
# imports
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/jimbobbennett/CircuitPython_ESP32Connection.git"
import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_minimqtt as MQTT
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi.adafruit_esp32spi_wifimanager import ESPSPI_WiFiManager
from adafruit_ntp import NTP
import adafruit_logging as logging
def __connect(cs_pin, ready_pin, reset_pin, secrets) -> ESPSPI_WiFiManager:
logger = logging.getLogger("log")
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, cs_pin, ready_pin, reset_pin)
wifi = ESPSPI_WiFiManager(esp, secrets, attempts=5)
# MQTT.set_socket(socket, esp)
logger.debug("MAC addr: " + ", ".join([hex(i) for i in esp.MAC_address]))
logger.debug("Connecting to AP...")
wifi.connect()
logger.info("Connected to " + str(esp.ssid, "utf-8") + "\tRSSI: " + str(esp.rssi))
logger.debug("My IP address is " + esp.pretty_ip(esp.ip_address))
logger.debug("Setting time")
ntp = NTP(esp)
while not ntp.valid_time:
ntp.set_time()
logger.debug("Failed to obtain time, retrying in 1 second...")
time.sleep(1)
logger.info("Time: " + str(time.time()))
return wifi
class Connection:
"""
A WiFi connection helper for ESP32-based boards
"""
def __init__(self):
self.wifi = None
def connect(self, secrets) -> ESPSPI_WiFiManager:
"""
Connects to WiFi.
This currently supports the PyBadge with Airlift Featherwing, and PyPortals
"""
try:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
except AttributeError:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)
self.wifi = __connect(esp32_cs, esp32_ready, esp32_reset, secrets)
return self.wifi