-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
executable file
·69 lines (54 loc) · 2.26 KB
/
connection.py
File metadata and controls
executable file
·69 lines (54 loc) · 2.26 KB
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
import board, busio
import neopixel
import json
from secrets import secrets
from digitalio import DigitalInOut
import requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
class Connection:
#__status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
def __connect(self, spi, cs, ready, reset, log):
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, cs, ready, reset)
requests.set_socket(socket, esp)
if log:
print("MAC addr:", [hex(i) for i in esp.MAC_address])
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets['ssid'], secrets['password'])
except RuntimeError as e:
if log:
print("could not connect to AP, retrying: ",e)
continue
if log:
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))
self.__wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
def connect(self, spi, log = False):
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.__connect(spi, esp32_cs, esp32_ready, esp32_reset, log)
return self.__wifi
def get_time(self, log = False):
url = 'http://worldtimeapi.org/api/ip'
has_time = False
while not has_time:
try:
response = requests.get(url)
response_json = json.loads(response.text)
time_string = response_json['unixtime']
has_time = True
if log:
print('time:', time_string)
return int(time_string)
except RuntimeError as e:
if log:
print("could not connect to AP, retrying: ",e)
continue