This commit is contained in:
Manuel Kamper 2023-04-09 16:46:10 +00:00
parent f692cc532f
commit d376d1a97f
1 changed files with 94 additions and 79 deletions

View File

@ -1,79 +1,94 @@
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 = " + str(mac))
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 " "
import network
import ubinascii
import Logger
import machine
import time
import socket
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 = " + str(mac))
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 " "
def HTTP_GET(url):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(100)
if data:
print(str(data, 'utf8'), end='') #todo add to logger??
else:
break
s.close()