-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathwifi.py
More file actions
57 lines (48 loc) · 1.7 KB
/
wifi.py
File metadata and controls
57 lines (48 loc) · 1.7 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
import time
from configuration import Configuration
from util import singleton
import network
from rtc import RTC
import time
import ntptime
import localPTZtime
@singleton
class WLAN:
def __init__(self, scheduler):
self.scheduler = scheduler
self.configuration = Configuration().wifi_config
self.wlan = None
self.rtc = RTC()
if self.configuration.enabled and self.wlan is None:
self.connect_to_wifi()
def wifi_connected(self):
if self.wlan is not None and self.wlan.status() == 3:
return True
return False
def connect_to_wifi(self):
import network
print("Connecting to WiFi")
#network.hostname(self.configuration.hostname)
self.wlan = network.WLAN(network.STA_IF)
self.wlan.active(True)
self.wlan.config(pm=0xa11140) # type: ignore - Disable powersave mode
self.wlan.connect(self.configuration.ssid,
self.configuration.passphrase)
# Wait for connect or fail
max_wait = 20
while max_wait > 0:
if self.wlan.status() < 0 or self.wlan.status() >= 3:
break
max_wait -= 1
print('Waiting for connection...')
time.sleep(1)
# Handle connection error
if self.wlan.status() != 3:
raise RuntimeError('WiFi connection failed')
else:
status = self.wlan.ifconfig()
print('IP = ' + status[0])
if self.configuration.ntp_enabled:
ntptime.settime()
local_time= localPTZtime.tztime(time.time(), self.configuration.ntp_ptz)
self.rtc.save_time(local_time[:8])