Created
March 20, 2021 17:12
-
-
Save lesp/001da8c2aea7d43777dd01f8f775c955 to your computer and use it in GitHub Desktop.
Wifi on a Raspberry Pi Pico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import busio | |
from digitalio import DigitalInOut | |
import adafruit_requests as requests | |
import adafruit_esp32spi.adafruit_esp32spi_socket as socket | |
from adafruit_esp32spi import adafruit_esp32spi | |
from secrets import secrets | |
print("Raspberry Pi Pico WiFi Weather Station") | |
JSON_URL = "http://api.openweathermap.org/data/2.5/weather?q=LOCATION&appid=API KEY&units=metric" | |
esp32_cs = DigitalInOut(board.GP13) | |
esp32_ready = DigitalInOut(board.GP14) | |
esp32_reset = DigitalInOut(board.GP15) | |
spi = busio.SPI(board.GP10, board.GP11, board.GP12) | |
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) | |
requests.set_socket(socket, esp) | |
while not esp.is_connected: | |
try: | |
esp.connect_AP(secrets["ssid"], secrets["password"]) | |
except RuntimeError as e: | |
print("could not connect to AP, retrying: ", e) | |
continue | |
print("Fetching weather data") | |
r = requests.get(JSON_URL) | |
print(r.status_code) | |
print("-" * 40) | |
print("The current temperature is",r.json()['main']['temp_max'],"C") | |
print("-" * 40) | |
r.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment