From 77eefea8dd0306376b8bdeb627d62273efdc5ad0 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Wed, 19 Aug 2020 09:39:37 +0200 Subject: [PATCH] added extra wifi examples --- content/tutorials/networks/wlan.md | 62 ++++++++++++++++-------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/content/tutorials/networks/wlan.md b/content/tutorials/networks/wlan.md index 177869c..0cf964f 100644 --- a/content/tutorials/networks/wlan.md +++ b/content/tutorials/networks/wlan.md @@ -12,7 +12,13 @@ The WLAN (WiFi) is a system feature of all Pycom devices, therefore it is enable On this page, we cover: 1. [Connecting to the Device](#connecting-to-the-device) -2. [Connecting to a Router](#connecting-to-a-router) +2. [Connecting to a home Router](#connecting-to-a-router) + * [Scanning for networks](#scan-for-wifi-networks) + * [Assigning a static IP Address](#Assigning-a-Static-IP-Address) + * [Connecting to multiple networks](#Multiple-Networks-using-a-Static-IP-Address) + * [Connecting to a WPA2 Enterprise Network](#Connecting-to-a-WPA2-Enterprise-network) +3. [Using an external Antenna](#using-an-external-antenna) + >Note: Generally, code in either sections is applicable to both WLAN modes. @@ -48,7 +54,7 @@ print(wlan.ifconfig()) >Note: If the device hangs in the `while` loop, probably your network is out of reach, or you made a mistake entering your WiFi credentials. -### Scan +## Scan for WiFi networks If you are not always in reach of your network (maybe you have a moving device), you can also scan for the network using the example below. ```python @@ -67,17 +73,14 @@ for net in nets: break ``` -### Assigning a Static IP Address at Boot Up - -If you want to connect your device to a wireless router, and access it from a telnet terminal, you can use the following example to assign a fixed ip address (`192.168.1.10`, check with your wifi network for possibilities), use the following script as `/boot.py`: +# Assigning a Static IP Address +If you want to connect your device to a wireless router, and access it from a telnet terminal, you can use the following example to assign a fixed ip address (192.168.1.10, check with your wifi network for possibilities), use the following script: ```python - import machine from network import WLAN wlan = WLAN() # get current object, without changing the mode - -if machine.reset_cause() != machine.SOFT_RESET: #do not reset the wireless connection on soft reset. +if machine.reset_cause() != machine.SOFT_RESET: wlan.init(mode=WLAN.STA) # configuration below MUST match your home router settings!! wlan.ifconfig(config=('192.168.178.107', '255.255.255.0', '192.168.1.10', '8.8.8.8')) # (ip, subnet_mask, gateway, DNS_server) @@ -85,20 +88,18 @@ if machine.reset_cause() != machine.SOFT_RESET: #do not reset the wireless conne if not wlan.isconnected(): # change the line below to match your network ssid, security and password wlan.connect('mywifi', auth=(WLAN.WPA2, 'mywifikey'), timeout=5000) + print("connecting",end='') while not wlan.isconnected(): - machine.idle() # save power while waiting + time.sleep(1) + print(".",end='') + print("connected") ``` - -{{% hint style="info" %}} -Notice how we check for the reset cause and the connection status, this is crucial in order to be able to soft reset the LoPy during a telnet session without breaking the connection. -{{% /hint %}} +> Notice how we check for the reset cause and the connection status, this is crucial in order to be able to soft reset the LoPy during a telnet session without breaking the connection. ### Multiple Networks using a Static IP Address - -The following script holds a list with nets and an optional list of `wlan_config` to set a fixed IP +The following script holds a list with nets and an optional list of wlan_config to set a fixed IP ```python - import os import machine @@ -113,13 +114,13 @@ known_nets = { if machine.reset_cause() != machine.SOFT_RESET: from network import WLAN - wl = WLAN() - wl.mode(WLAN.STA) - original_ssid = wl.ssid() - original_auth = wl.auth() + wlan = WLAN() + wlan.mode(WLAN.STA) + original_ssid = wlan.ssid() + original_auth = wlan.auth() print("Scanning for known wifi nets") - available_nets = wl.scan() + available_nets = wlan.scan() nets = frozenset([e.ssid for e in available_nets]) known_nets_names = frozenset([key for key in known_nets]) @@ -130,18 +131,17 @@ if machine.reset_cause() != machine.SOFT_RESET: pwd = net_properties['pwd'] sec = [e.sec for e in available_nets if e.ssid == net_to_use][0] if 'wlan_config' in net_properties: - wl.ifconfig(config=net_properties['wlan_config']) - wl.connect(net_to_use, (sec, pwd), timeout=10000) - while not wl.isconnected(): + wlan.ifconfig(config=net_properties['wlan_config']) + wlan.connect(net_to_use, (sec, pwd), timeout=10000) + while not wlan.isconnected(): machine.idle() # save power while waiting - print("Connected to "+net_to_use+" with IP address:" + wl.ifconfig()[0]) + print("Connected to "+net_to_use+" with IP address:" + wlan.ifconfig()[0]) except Exception as e: print("Failed to connect to any known network, going into AP mode") - wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT) + wlan.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT) ``` - -### Connecting to a WPA2-Enterprise network +## Connecting to a WPA2-Enterprise network * **Connecting with EAP-TLS:** Before connecting, obtain and copy the public and private keys to the device, e.g. under location `/flash/cert`. If it is required to validate the server's public key, an appropriate CA certificate (chain) must also be provided. @@ -164,3 +164,9 @@ wlan.connect(ssid='mywifi', auth=(WLAN.WPA2_ENT, 'username', 'password'), identi ``` +## Using an external antenna + +Connect a WiFi antenna to this U.FL connector on your development board. +![](/gitbook/assets/wifi_pigtail_ant_lopy4.png) + +