1+ # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
2+ # SPDX-License-Identifier: MIT
3+
14import board
25import busio
36from digitalio import DigitalInOut
4-
5- from adafruit_esp32spi import adafruit_esp32spi
67import adafruit_requests as requests
8+ import adafruit_esp32spi .adafruit_esp32spi_socket as socket
9+ from adafruit_esp32spi import adafruit_esp32spi
10+
11+ # Get wifi details and more from a secrets.py file
12+ try :
13+ from secrets import secrets
14+ except ImportError :
15+ print ("WiFi secrets are kept in secrets.py, please add them there!" )
16+ raise
717
8- print ("Raspberry Pi 2040 - ESP32SPI hardware test" )
18+ print ("Raspberry Pi RP2040 - ESP32 SPI webclient test" )
919
20+ TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
21+ JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
22+
23+ # Raspberry Pi RP2040 Pinout
1024esp32_cs = DigitalInOut (board .GP13 )
1125esp32_ready = DigitalInOut (board .GP14 )
1226esp32_reset = DigitalInOut (board .GP15 )
1327
14- spi = busio .SPI (clock = board .GP10 , MOSI = board .GP11 , MISO = board .GP12 )
28+ spi = busio .SPI (board .GP10 , board .GP11 , board .GP12 )
1529esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
1630
31+ requests .set_socket (socket , esp )
32+
1733if esp .status == adafruit_esp32spi .WL_IDLE_STATUS :
1834 print ("ESP32 found and in idle mode" )
1935print ("Firmware vers." , esp .firmware_version )
2036print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
2137
2238for ap in esp .scan_networks ():
23- print ("\t %s\t \t RSSI: %d" % (str (ap ['ssid' ], 'utf-8' ), ap ['rssi' ]))
39+ print ("\t %s\t \t RSSI: %d" % (str (ap ["ssid" ], "utf-8" ), ap ["rssi" ]))
40+
41+ print ("Connecting to AP..." )
42+ while not esp .is_connected :
43+ try :
44+ esp .connect_AP (secrets ["ssid" ], secrets ["password" ])
45+ except RuntimeError as e :
46+ print ("could not connect to AP, retrying: " , e )
47+ continue
48+ print ("Connected to" , str (esp .ssid , "utf-8" ), "\t RSSI:" , esp .rssi )
49+ print ("My IP address is" , esp .pretty_ip (esp .ip_address ))
50+ print (
51+ "IP lookup adafruit.com: %s" % esp .pretty_ip (esp .get_host_by_name ("adafruit.com" ))
52+ )
53+ print ("Ping google.com: %d ms" % esp .ping ("google.com" ))
54+
55+ # esp._debug = True
56+ print ("Fetching text from" , TEXT_URL )
57+ r = requests .get (TEXT_URL )
58+ print ("-" * 40 )
59+ print (r .text )
60+ print ("-" * 40 )
61+ r .close ()
62+
63+ print ()
64+ print ("Fetching json from" , JSON_URL )
65+ r = requests .get (JSON_URL )
66+ print ("-" * 40 )
67+ print (r .json ())
68+ print ("-" * 40 )
69+ r .close ()
2470
25- print ("Done!" )
71+ print ("Done!" )
0 commit comments