-
Notifications
You must be signed in to change notification settings - Fork 0
/
Networking.py
79 lines (71 loc) · 2.34 KB
/
Networking.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
import network
import ubinascii
import Logger
import machine
import time
wlan = network.WLAN(network.STA_IF)
led = machine.Pin('LED', machine.Pin.OUT)
class Networking():
def __init__(self, Logger, ssid, pw):
self.ssid = ssid
self.pw = pw
self.Logger = Logger
def Connect(self, disable_wifi_powersavingmode):
global wlan
wlan.active(True)
#wlan.config(hostname=configs['hostname'])
if (disable_wifi_powersavingmode):
wlan.config(pm = 0xa11140)
self.Logger.LogMessage("mac = " + self.GetMACAddress())
self.Logger.LogMessage("channel = " + str(wlan.config('channel')))
self.Logger.LogMessage("essid = " + str(wlan.config('essid')))
self.Logger.LogMessage("txpower = " + str(wlan.config('txpower')))
wlan.connect(self.ssid, self.pw)
timeout = 30
while timeout > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
timeout -= 1
self.Logger.LogMessage('Waiting for connection...')
time.sleep(1)
# Wlan Status on LED
# Handle connection error
# Error meanings
# 0 Link Down
# 1 Link Join
# 2 Link NoIp
# 3 Link Up
# -1 Link Fail
# -2 Link NoNet
# -3 Link BadAuth
def Status(self):
global wlan
wlan_status = wlan.status()
self.BlinkOnboardLED(wlan_status)
if wlan_status != 3:
#raise RuntimeError('Wi-Fi connection failed')
return False
else:
self.Logger.LogMessage('Connected')
status = wlan.ifconfig()
self.Logger.LogMessage('ip = ' + status[0])
return True
# Blinkfunktion der Onboard-LED für Errorcodes
def BlinkOnboardLED(self, num_blinks):
global led
for i in range(num_blinks):
led.on()
time.sleep(.2)
led.off()
time.sleep(.2)
def GetMACAddress(self):
return ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
def GetIPAddress(self):
global wlan
return wlan.ifconfig()[0]
def IsWifiConnected(self):
global wlan
if wlan.status() == 3:
return "W"
else:
return " "