1
+ # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
2
+ # SPDX-License-Identifier: MIT
3
+
1
4
import board
2
5
import busio
3
6
from digitalio import DigitalInOut
4
-
5
- from adafruit_esp32spi import adafruit_esp32spi
6
7
import 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
7
17
8
- print ("Raspberry Pi 2040 - ESP32SPI hardware test" )
18
+ print ("Raspberry Pi RP2040 - ESP32 SPI webclient test" )
9
19
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
10
24
esp32_cs = DigitalInOut (board .GP13 )
11
25
esp32_ready = DigitalInOut (board .GP14 )
12
26
esp32_reset = DigitalInOut (board .GP15 )
13
27
14
- spi = busio .SPI (clock = board .GP10 , MOSI = board .GP11 , MISO = board .GP12 )
28
+ spi = busio .SPI (board .GP10 , board .GP11 , board .GP12 )
15
29
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
16
30
31
+ requests .set_socket (socket , esp )
32
+
17
33
if esp .status == adafruit_esp32spi .WL_IDLE_STATUS :
18
34
print ("ESP32 found and in idle mode" )
19
35
print ("Firmware vers." , esp .firmware_version )
20
36
print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
21
37
22
38
for 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 ()
24
70
25
- print ("Done!" )
71
+ print ("Done!" )
0 commit comments