diff --git a/micropython/examples/pico_plus_2_w/astronauts.py b/micropython/examples/pico_plus_2_w/astronauts.py new file mode 100644 index 0000000..7e7eed8 --- /dev/null +++ b/micropython/examples/pico_plus_2_w/astronauts.py @@ -0,0 +1,25 @@ +""" +List all the humans who are currently in space! +You will need to add your wireless SSID and password to secrets.py (and save this file to your Pico) +This example was adapted from one written by Les Pounder for Tom's Hardware: https://www.tomshardware.com/how-to/connect-raspberry-pi-pico-w-to-the-internet +""" + +import network +import requests +from secrets import WIFI_SSID, WIFI_PASSWORD +from time import sleep + +# connect to wifi +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(WIFI_SSID, WIFI_PASSWORD) +while wlan.isconnected() is False: + print('Waiting for connection...') + sleep(1) + +astronauts = requests.get("http://api.open-notify.org/astros.json").json() +number = astronauts['number'] + +print(f'There are currently {number} humans in space:') +for i in range(number): + print(astronauts['people'][i]['name']) diff --git a/micropython/examples/pico_plus_2_w/button.py b/micropython/examples/pico_plus_2_w/button.py new file mode 100644 index 0000000..d6ffc74 --- /dev/null +++ b/micropython/examples/pico_plus_2_w/button.py @@ -0,0 +1,13 @@ +from machine import Pin + +# Setup the LED and Button pin. +led = Pin('LEDW', Pin.OUT) +button = Pin(45, Pin.IN) + +# Light the LED when the button is pressed! +while True: + + if button.value() == 0: + led.value(1) + else: + led.value(0) diff --git a/micropython/examples/pico_plus_2_w/catfacts.py b/micropython/examples/pico_plus_2_w/catfacts.py new file mode 100644 index 0000000..da61367 --- /dev/null +++ b/micropython/examples/pico_plus_2_w/catfacts.py @@ -0,0 +1,22 @@ +""" +Get a cat fact from t'internet! +You will need to add your wireless SSID and password to secrets.py (and save this file to your Pico) +""" + +import network +import requests +from secrets import WIFI_SSID, WIFI_PASSWORD +from time import sleep + +# connect to wifi +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(WIFI_SSID, WIFI_PASSWORD) +while wlan.isconnected() is False: + print('Waiting for connection...') + sleep(1) + +request = requests.get('http://catfact.ninja/fact').json() +fact = request['fact'] +print('Cat fact!') +print(fact) diff --git a/micropython/examples/pico_plus_2_w/onboard_led.py b/micropython/examples/pico_plus_2_w/onboard_led.py new file mode 100644 index 0000000..6b1f528 --- /dev/null +++ b/micropython/examples/pico_plus_2_w/onboard_led.py @@ -0,0 +1,14 @@ +import time +from machine import Pin + +# Setup the LED pin. +led = Pin('LEDW', Pin.OUT) + +# Blink the LED! +while True: + + led.value(1) + time.sleep(1) + + led.value(0) + time.sleep(1) diff --git a/micropython/examples/pico_plus_2_w/secrets.py b/micropython/examples/pico_plus_2_w/secrets.py new file mode 100644 index 0000000..1b9c92f --- /dev/null +++ b/micropython/examples/pico_plus_2_w/secrets.py @@ -0,0 +1,2 @@ +WIFI_SSID = 'ssid_goes_here' +WIFI_PASSWORD = 'password_goes_here'