Skip to content

Commit ca5513a

Browse files
committed
Add MPY Network demo.
1 parent 710e02f commit ca5513a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import network
2+
import urequests
3+
import time
4+
import neopixel
5+
import machine
6+
7+
# Set up the NeoPixel LED
8+
pin = machine.Pin("NEOPIXEL")
9+
led = neopixel.NeoPixel(pin, 1)
10+
11+
# Set the LED to red while connecting to WiFi
12+
led[0] = (255, 0, 0)
13+
led.write()
14+
15+
# Connect to a WiFi network
16+
wlan = network.WLAN(network.STA_IF)
17+
wlan.active(True)
18+
wlan.connect('WIFI_NETWORK', 'WIFI_PASSWORD')
19+
20+
# Wait for the connection to complete
21+
print("Waiting for connection...")
22+
while not wlan.isconnected():
23+
time.sleep(1)
24+
print('.', sep='', end='', )
25+
print('Connected!')
26+
27+
# Set the LED to blue while downloading data
28+
led[0] = (0, 0, 255)
29+
led.write()
30+
31+
# Download the list of astronauts currently in space
32+
print('Downloading list of astronauts currently in space...')
33+
astronauts = urequests.get("https://corquaid.github.io/international-space-station-APIs/JSON/people-in-space.json").json()
34+
35+
# Print the number of astronauts and their names
36+
number = astronauts['number']
37+
print('There are', number, 'astronauts in space.')
38+
for i in range(number):
39+
print(i+1, astronauts['people'][i]['name'])
40+
41+
# Set the LED to green to indicate success
42+
led[0] = (0, 255, 0)
43+
led.write()

0 commit comments

Comments
 (0)