Merge pull request #12 from pimoroni/examples/ppp2w

add wireless examples
This commit is contained in:
Hel Gibbons 2024-10-11 14:52:07 +01:00 committed by GitHub
commit a0c2397e04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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 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'])

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

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 @@
WIFI_SSID = 'ssid_goes_here'
WIFI_PASSWORD = 'password_goes_here'