本文整理汇总了Python中network.WLAN类的典型用法代码示例。如果您正苦于以下问题:Python WLAN类的具体用法?Python WLAN怎么用?Python WLAN使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WLAN类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
class Theta:
def __init__(self):
self.wlan = WLAN(WLAN.STA)
pass
def log(self, msg):
print(msg)
def findWifi(self):
wlans = self.wlan.scan()
# TODO: Return all visible Thetas
for w in wlans:
if w.ssid.startswith('THETA'):
self.log('Found Theta WiFi: %s' % w.ssid)
# THETAXL12345678 = Theta (original model) - PTP/IP
# THETAXN12345678 = Theta m15 - PTP/IP
# THETAXS12345678.OSC = Theta S - OSC
return w.ssid
return False
def connectWifi(self, ssid):
password = ssid[-8:]
return self.wlan.connect(ssid, auth=(WLAN.WPA, password))
# convenience - might get removed
def connect(self):
wifi = self.findWifi()
if not wifi:
return False
self.connectWifi(wifi)
self.ptpip = ptpip.PTPIP('192.168.1.1')
return self.ptpip
def initPTP(self):
answer = self.ptpip.initCommand('1234567812345678', 'WiPy')
if not answer:
print("Init failed!")
return False
(session_id, guid, name) = answer
pass2 = self.ptpip.initEvent(session_id)
if not pass2:
print("Init stage 2 failed!")
return False
return (session_id, guid, name)
def openSession(self):
answer = self.ptpip.createCommand(0x1002, [])
return answer
def closeSession(self):
answer = self.ptpip.createCommand(0x1003, [])
return answer
def shoot(self):
answer = self.ptpip.createCommand(0x100e, [0x0, 0x0])
return answer
def getPTPIP(self):
return self.ptpip
示例2: connect
def connect(ssid, key):
""" Scans for and connects to the specified wifi network using key as password """
wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
if net.ssid == ssid:
wlan.connect(net.ssid, auth=(net.sec, key), timeout=5000)
while not wlan.isconnected():
machine.idle() # save power while waiting
break
示例3: disconnectWLAN
def disconnectWLAN(self):
try:
from network import WLAN
wlan=None
if self.isWipy1():
wlan=WLAN()
else:
wlan=WLAN(mode=WLAN.STA)
wlan.disconnect()
except:
pass
示例4: enable_ap_mode
def enable_ap_mode(essid=None, password=None):
from network import AP_IF, AUTH_WPA_WPA2_PSK, WLAN
from ubinascii import hexlify
wifi_interface = WLAN(AP_IF)
if not essid:
essid = b"micropython-esp8266-%s" % hexlify(wifi_interface.config("mac")[-3:])
if not password:
password = b'micropybootconfig'
wifi_interface.config(essid=essid, authmode=AUTH_WPA_WPA2_PSK, password=password)
del hexlify
示例5: wlan_connect
def wlan_connect():
wifi = WLAN(mode=WLAN.STA)
wifi.ifconfig(config=(__myip, __netmask, __gateway, __dns))
wifi.scan() # scan for available networks
wifi.connect(ssid=__ssid, auth=(WLAN.WPA2, __nwpass))
while not wifi.isconnected():
pass
syslog('WiPy is up and running')
wifi.irq(trigger=WLAN.ANY_EVENT, wake=machine.SLEEP)
machine.sleep()
示例6: connect_to_wifi_wipy
def connect_to_wifi_wipy(ssid, password, retries=10):
"""
Connect to a WIFI network
"""
wlan = WLAN(mode=WLAN.STA)
print("Connecting to wifi network '%s'" % ssid)
wlan.connect(ssid=ssid, auth=(WLAN.WPA2, password))
retry_count = 0
while not wlan.isconnected():
sleep(1)
retry_count += 1
if retry_count > retries:
return False
print('Connected', wlan.ifconfig())
return True
示例7: disconnectWLAN
def disconnectWLAN(self):
# disconnect wlan because it spams debug messages that disturb the monitor protocol
try:
from network import WLAN
wlan = None
if self.isWipy1():
wlan = WLAN()
else:
wlan = WLAN(mode=WLAN.STA)
wlan.disconnect()
except:
# if wifi disconnect fails for whatever reason, let it continue to sync
# often this is the 'OSError: the requested oparation is not possible' thrown by the wlan.disconnect line
pass
示例8: start
def start(self):
# Change WiFi to STA mode and connect
self.wlan = WLAN(mode=WLAN.STA)
self._connect_to_wifi()
# Get a time Sync
self.rtc = machine.RTC()
self.rtc.ntp_sync(self.ntp, update_period=self.ntp_period)
# Get the server IP and create an UDP socket
self.server_ip = socket.getaddrinfo(self.server, self.port)[0][-1]
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setblocking(False)
# Push the first time immediatelly
self._push_data(self._make_stat_packet())
# Create the alarms
self.stat_alarm = Timer.Alarm(handler=lambda t: self._push_data(self._make_stat_packet()), s=60, periodic=True)
self.pull_alarm = Timer.Alarm(handler=lambda u: self._pull_data(), s=25, periodic=True)
# Start the UDP receive thread
_thread.start_new_thread(self._udp_thread, ())
# Initialize LoRa in LORA mode
self.lora = LoRa(mode=LoRa.LORA, frequency=self.frequency, bandwidth=LoRa.BW_125KHZ, sf=self.sf,
preamble=8, coding_rate=LoRa.CODING_4_5, tx_iq=True)
# Create a raw LoRa socket
self.lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
self.lora_sock.setblocking(False)
self.lora_tx_done = False
self.lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=self._lora_cb)
示例9: __init__
def __init__(self):
self.sock = None
self.connected = False
self.wlan = WLAN(mode=WLAN.STA)
self.riders = {} # dictionary of riders
# initialize LoRa as a Gateway (with Tx IQ inversion)
self.lora = LoRa(tx_iq=True, rx_iq=False)
示例10: do_connect
def do_connect():
from network import WLAN
sta_if = WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(WIFISSID, WIFIPASS)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
示例11: connectLocalBox
def connectLocalBox(configFilePath):
f = open(configFilePath, 'r')
config=ujson.load(f)
f.close()
wlan = WLAN(mode=WLAN.STA)
wlan.ifconfig(config=(config["ip"], config["mask"],config["gateway"], config["dns"]))
wlan.scan()
wlan.connect(config["ssid"], auth=(WLAN.WPA2, config["password"]))
while not wlan.isconnected():
pass
return wlan;
示例12: start
def start(self):
"""
Starts the LoRaWAN nano gateway.
"""
self._log('Starting LoRaWAN nano gateway with id: {}', self.id)
# setup WiFi as a station and connect
self.wlan = WLAN(mode=WLAN.STA)
self._connect_to_wifi()
# get a time sync
self._log('Syncing time with {} ...', self.ntp_server)
self.rtc.ntp_sync(self.ntp_server, update_period=self.ntp_period)
while not self.rtc.synced():
utime.sleep_ms(50)
self._log("RTC NTP sync complete")
# get the server IP and create an UDP socket
self.server_ip = usocket.getaddrinfo(self.server, self.port)[0][-1]
self._log('Opening UDP socket to {} ({}) port {}...', self.server, self.server_ip[0], self.server_ip[1])
self.sock = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM, usocket.IPPROTO_UDP)
self.sock.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
self.sock.setblocking(False)
# push the first time immediatelly
self._push_data(self._make_stat_packet())
# create the alarms
self.stat_alarm = Timer.Alarm(handler=lambda t: self._push_data(self._make_stat_packet()), s=60, periodic=True)
self.pull_alarm = Timer.Alarm(handler=lambda u: self._pull_data(), s=25, periodic=True)
# start the UDP receive thread
self.udp_stop = False
_thread.start_new_thread(self._udp_thread, ())
# initialize the LoRa radio in LORA mode
self._log('Setting up the LoRa radio at {:.1f} Mhz using {}', self._freq_to_float(self.frequency), self.datarate)
self.lora = LoRa(
mode=LoRa.LORA,
frequency=self.frequency,
bandwidth=self.bw,
sf=self.sf,
preamble=8,
coding_rate=LoRa.CODING_4_5,
tx_iq=True
)
# create a raw LoRa socket
self.lora_sock = usocket.socket(usocket.AF_LORA, usocket.SOCK_RAW)
self.lora_sock.setblocking(False)
self.lora_tx_done = False
self.lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=self._lora_cb)
self._log('LoRaWAN nano gateway online')
示例13: wlan
def wlan():
"""Connect in STA mode, fallback to AP"""
try:
import wlanconfig
except ImportError:
print("WLAN: no wlanconfig.py")
wlanconfig = None
wlan = WLAN(mode=WLAN.AP)
except Exception as e:
print("WLAN: error in wlanconfig.py: {}".format(e))
wlanconfig = None
wlan = WLAN(mode=WLAN.AP)
else:
try:
# configure the WLAN subsystem in station mode (the default is AP)
wlan = WLAN(mode=WLAN.STA)
print("WLAN: connecting to network (AP)...")
wlan.connect(wlanconfig.ssid, auth=(WLAN.WPA2, wlanconfig.password), timeout=5000)
print("WLAN: waiting for IP...")
for tries in range(50):
if wlan.isconnected():
print(
"""\
WLAN: connected!
WiPy IP: {}
NETMASK: {}
GATEWAY: {}
DNS: {}""".format(
*wlan.ifconfig()
)
)
break
time.sleep_ms(100)
except OSError:
print("WLAN: found no router, going into AP mode instead")
wlanconfig = None
except Exception as e:
print("WLAN: error: {}".format(e))
wlanconfig = None
if wlanconfig is None:
wlan.init(mode=WLAN.AP, ssid="wipy-wlan", auth=(WLAN.WPA2, "www.wipy.io"), channel=7, antenna=WLAN.INT_ANT)
示例14: wlan
def wlan():
"""Connect in STA mode, fallback to AP"""
log = ulog.Logger('WLAN: ')
try:
import wlanconfig
except ImportError:
log.notice('no wlanconfig.py')
wlanconfig = None
wlan = WLAN(mode=WLAN.AP)
except Exception as e:
log.error('error in wlanconfig.py: {}'.format(e))
wlanconfig = None
wlan = WLAN(mode=WLAN.AP)
else:
try:
# configure the WLAN subsystem in station mode (the default is AP)
wlan = WLAN(mode=WLAN.STA)
log.info('connecting to network (AP)...')
wlan.connect(wlanconfig.ssid, auth=(WLAN.WPA2, wlanconfig.password), timeout=5000)
log.info('waiting for IP...')
for tries in range(50):
if wlan.isconnected():
log.notice('''connected!
WiPy IP: {}
NETMASK: {}
GATEWAY: {}
DNS: {}'''.format(*wlan.ifconfig()))
break
time.sleep_ms(100)
except OSError:
log.error('found no router, going into AP mode instead')
wlanconfig = None
except Exception as e:
log.error('error: {}'.format(e))
wlanconfig = None
if wlanconfig is None:
wlan.init(mode=WLAN.AP, ssid='wipy-wlan', auth=(WLAN.WPA2,'www.wipy.io'), channel=7, antenna=WLAN.INT_ANT)
示例15: connect_wifi
def connect_wifi(self):
from network import WLAN
if not self.cfg:
raise ValueError("Can't initialise wifi, no config")
self.log('Starting WLAN, attempting to connect to ' + ','.join(self.cfg.wifi.keys()))
wlan = WLAN(0, WLAN.STA)
wlan.ifconfig(config='dhcp')
while not wlan.isconnected():
nets = wlan.scan()
for network in nets:
if network.ssid in self.cfg.wifi.keys():
self.log('Connecting to ' + network.ssid)
self.feed_wdt() # just in case
wlan.connect(ssid=network.ssid, auth=(network.sec, self.cfg.wifi[network.ssid]))
while not wlan.isconnected():
idle()
break
self.feed_wdt() # just in case
sleep_ms(2000)
self.log('Connected as %s' % wlan.ifconfig()[0])