You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/basics/06-wireless.md
+28-13Lines changed: 28 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ You can read more about the capabilities of the WiFi/Bluetooth chip by reading t
16
16
17
17
The Pico W code is very similar to prior versions of the Pico with a few small exceptions. One of these is the fact that we must now use a symbolic label called an **alias* such as ```Pin("LED")``` instead of ```Pin(25)``` to access the LED pin, not a hardwired PIN number. This allows us to keep our code more portable as the underlying hardware changes.
18
18
19
-
```python title="blink.py"
19
+
```python
20
20
from machine import Pin, Timer
21
21
22
22
led = Pin("LED", Pin.OUT)
@@ -63,7 +63,8 @@ By importing the secrets.py file you can then reference your network name like t
63
63
print('Connecting to WiFi Network Name:', secrets.SSID)
64
64
```
65
65
66
-
## Testing Your Connection
66
+
## Testing Your WiFi Access Point Connection
67
+
Here is a very simple script to test see if your network name and password are correct. This script may work, but as we will see, it is both slow and potentially unreliable.
67
68
68
69
```python
69
70
import network
@@ -72,22 +73,35 @@ from utime import sleep
72
73
73
74
print('Connecting to WiFi Network Name:', secrets.SSID)
74
75
wlan = network.WLAN(network.STA_IF)
75
-
sleep(1) # wait a second to wait for a connection
76
-
wlan.active(True)
76
+
wlan.active(True) # power up the WiFi chip
77
+
print('Waiting for wifi chip to power up...')
78
+
sleep(3) # wait three seconds for the chip to power up and initialize
77
79
wlan.connect(secrets.SSID, secrets.PASSWORD)
78
-
print(wlan.isconnected())
80
+
print('Waiting for access point to log us in.')
81
+
sleep(2)
82
+
if wlan.isconnected():
83
+
print('Success! We have connected to your access point!')
84
+
print('Try to ping the device at', wlan.ifconfig()[0])
85
+
else:
86
+
print('Failure! We have not connected to your access point! Check your secrets.py file for errors.')
79
87
```
80
88
81
89
Returns:
82
90
83
91
```
84
92
Connecting to WiFi Network Name: MY_WIFI_NETWORK_NAME
85
-
True
93
+
Waiting for wifi chip to power up...
94
+
Waiting for access point to log us in...
95
+
Success! We have connected to your access point!
96
+
Try to ping the device at 10.0.0.70
86
97
```
87
98
88
-
If the value is ```False``` you should check the name of the network and the password and that you are getting a strong WiFi signal where you are testing.
99
+
If the result is a ```Failure``` you should check the name of the network and the password and that you are getting a strong WiFi signal where you are testing.
100
+
101
+
Note that we are using the ```sleep()``` function to insert delays into our code. However, the results may actually be faster or slower than our sleep times. Our next step is to add logic that will test to see if the networking device is ready and if our local access point allows us to login correctly.
89
102
90
103
## Waiting for a Valid Access Point Connection
104
+
91
105
Sometimes we want to keep checking if our access point is connected before we begin using our connection. To do this we can create a while loop and continue in the loop while we are not connected.
92
106
93
107
```python
@@ -112,26 +126,27 @@ if not wlan.isconnected():
112
126
113
127
delta = ticks_diff(ticks_ms(), start)
114
128
print("Connect Time:", delta, 'milliseconds')
115
-
print(wlan.ifconfig())
129
+
print('IP Address:', wlan.ifconfig()[0])
116
130
```
117
131
118
-
This code also supports a timer that will display the number of milliseconds for the access point to become valid. The first time after you power on, this may take several seconds. After you are connected the connection will be cached and the time will be 0 milliseconds.
132
+
This code also supports a timer that will display the number of seconds for the access point to become valid in the console. The first time after you power on, this may take several seconds. After you are connected the connection will be cached and the time will be 0 milliseconds.
119
133
120
134
First run upon power on might take several seconds:
0 commit comments