Skip to content

Commit 76c161a

Browse files
committed
updated content
1 parent 47eb391 commit 76c161a

4 files changed

Lines changed: 78 additions & 21 deletions

File tree

docs/basics/06-wireless.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can read more about the capabilities of the WiFi/Bluetooth chip by reading t
1616

1717
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.
1818

19-
```python title="blink.py"
19+
```python
2020
from machine import Pin, Timer
2121

2222
led = Pin("LED", Pin.OUT)
@@ -63,7 +63,8 @@ By importing the secrets.py file you can then reference your network name like t
6363
print('Connecting to WiFi Network Name:', secrets.SSID)
6464
```
6565

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.
6768

6869
```python
6970
import network
@@ -72,22 +73,35 @@ from utime import sleep
7273

7374
print('Connecting to WiFi Network Name:', secrets.SSID)
7475
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
7779
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.')
7987
```
8088

8189
Returns:
8290

8391
```
8492
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
8697
```
8798

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.
89102

90103
## Waiting for a Valid Access Point Connection
104+
91105
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.
92106

93107
```python
@@ -112,26 +126,27 @@ if not wlan.isconnected():
112126

113127
delta = ticks_diff(ticks_ms(), start)
114128
print("Connect Time:", delta, 'milliseconds')
115-
print(wlan.ifconfig())
129+
print('IP Address:', wlan.ifconfig()[0])
116130
```
117131

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.
119133

120134
First run upon power on might take several seconds:
121135
```
122136
>>> %Run -c $EDITOR_CONTENT
123-
Connecting to WiFi Network Name: anndan-2.4
137+
Connecting to WiFi Network Name: MY_NETWORK_NAME
124138
Waiting for connection...
125139
0.1.2.3.Connect Time: 4640
126-
('10.0.0.70', '255.255.255.0', '10.0.0.1', '75.75.75.75')
140+
IP Address: 10.0.0.70
127141
```
128142

129143
The second and consecutive runs will use a cached connection.
144+
130145
```
131146
>>> %Run -c $EDITOR_CONTENT
132-
Connecting to WiFi Network Name: anndan-2.4
147+
Connecting to WiFi Network Name: MY_NETWORK_NAME
133148
Connect Time: 0 milliseconds
134-
('10.0.0.70', '255.255.255.0', '10.0.0.1', '75.75.75.75')
149+
IP Address: 10.0.0.70
135150
>>>
136151
```
137152

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import network
2+
import secrets
3+
from utime import sleep
4+
5+
print('Connecting to WiFi Network Name:', secrets.SSID)
6+
wlan = network.WLAN(network.STA_IF)
7+
wlan.active(True) # power up the WiFi chip
8+
print('Waiting for wifi chip to power up')
9+
sleep(3) # wait three seconds for the chip to power up and initialize
10+
wlan.connect(secrets.SSID, secrets.PASSWORD)
11+
print('Waiting for access point to log us in.')
12+
sleep(2)
13+
if wlan.isconnected():
14+
print('Success! We have connected to your access point!')
15+
print('Try to ping the device at', wlan.ifconfig()[0])
16+
else:
17+
print('Failure! We have not connected to your access point! Check your secrets.py file for errors.')

src/network/05-get-mac-address-timed.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22
import secrets
33
from utime import sleep, ticks_us, ticks_diff
44

5-
print('Getting Mac Address for device.')
5+
print('Getting MAC/Ethernet Address for this device.')
66

77
start = ticks_us() # start a millisecond counter
88
wlan = network.WLAN(network.STA_IF)
9+
wlan.active(True) # power up the chip
10+
11+
# This returns a byte array of hex numbers
912
mac_addess = wlan.config('mac')
1013
print('Time in microseconds:', ticks_diff(ticks_us(), start))
11-
print(mac_addess)
14+
# each MAC address is 6 bytes or 48 bits
15+
print("Hex byte array:", mac_addess, 'length:', len(mac_addess))
1216

17+
# This should be in hex per the Notational Conventions
18+
# https://en.wikipedia.org/wiki/MAC_address#Notational_conventions
19+
# b'(\xcd\xc1\x015X'
20+
# 28:cd:c1:1:35:58
21+
# format in MAC Notational Convention
22+
for digit in range(0,5):
23+
print(str(hex(mac_addess[digit]))[2:4], ':', sep='', end = '')
24+
print(str(hex(mac_addess[5]))[2:4] )

src/network/05-get-mac-address.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import network
22
import secrets
3-
from utime import sleep, ticks_ms, ticks_diff
3+
from utime import sleep, ticks_us, ticks_diff
44

5-
ticks_diff(ticks_ms(), start)
5+
print('Getting MAC/Ethernet Address for this device.')
66

7-
print('Connecting to WiFi Network Name:', secrets.SSID)
7+
start = ticks_us() # start a millisecond counter
88
wlan = network.WLAN(network.STA_IF)
9-
print(wlan.config('mac'))
10-
delta = ticks_diff(ticks_ms(), start)
11-
print(delta)
9+
wlan.active(True) # power up the chip
10+
11+
# This returns a byte array of hex numbers
12+
mac_addess = wlan.config('mac')
13+
print('Time in microseconds:', ticks_diff(ticks_us(), start))
14+
# each MAC address is 6 bytes or 48 bits
15+
print("Hex byte array:", mac_addess, 'length:', len(mac_addess))
16+
17+
# This should be in hex per the Notational Conventions
18+
# https://en.wikipedia.org/wiki/MAC_address#Notational_conventions
19+
# b'(\xcd\xc1\x015X'
20+
# 28:cd:c1:1:35:58
21+
# format in MAC Notational Convention
22+
for digit in range(0,5):
23+
print(str(hex(mac_addess[digit]))[2:4], ':', sep='', end = '')
24+
print(str(hex(mac_addess[5]))[2:4] )

0 commit comments

Comments
 (0)