add wireless examples

This commit is contained in:
Hel Gibbons 2024-10-01 15:35:47 +01:00
parent b96cbf7364
commit 686e623326
5 changed files with 76 additions and 0 deletions

View File

@ -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'])

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,2 @@
SSID = 'ssid_goes_here'
PASSWORD = 'password_goes_here'