From 686e623326d6a5bf0ecb3d707d6a75322740b417 Mon Sep 17 00:00:00 2001 From: Hel Gibbons Date: Tue, 1 Oct 2024 15:35:47 +0100 Subject: [PATCH] add wireless examples --- .../examples/pico_plus_2_w/astronauts.py | 25 +++++++++++++++++++ micropython/examples/pico_plus_2_w/button.py | 13 ++++++++++ .../examples/pico_plus_2_w/catfacts.py | 22 ++++++++++++++++ .../examples/pico_plus_2_w/onboard_led.py | 14 +++++++++++ micropython/examples/pico_plus_2_w/secrets.py | 2 ++ 5 files changed, 76 insertions(+) create mode 100644 micropython/examples/pico_plus_2_w/astronauts.py create mode 100644 micropython/examples/pico_plus_2_w/button.py create mode 100644 micropython/examples/pico_plus_2_w/catfacts.py create mode 100644 micropython/examples/pico_plus_2_w/onboard_led.py create mode 100644 micropython/examples/pico_plus_2_w/secrets.py 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..f064365 --- /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 SSID, PASSWORD +from time import sleep + +# connect to wifi +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(SSID, 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..026f188 --- /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 SSID, PASSWORD +from time import sleep + +# connect to wifi +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(SSID, 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..6f048a5 --- /dev/null +++ b/micropython/examples/pico_plus_2_w/secrets.py @@ -0,0 +1,2 @@ +SSID = 'ssid_goes_here' +PASSWORD = 'password_goes_here'