66from adafruit_esp32spi import adafruit_esp32spi
77import adafruit_requests as requests
88
9- print ("ESP32 SPI webclient test" )
10-
11- TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
12- JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
13-
14-
159# If you are using a board with pre-defined ESP32 Pins:
1610esp32_cs = DigitalInOut (board .ESP_CS )
1711esp32_ready = DigitalInOut (board .ESP_BUSY )
2418
2519spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
2620esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
27- requests .set_socket (socket , esp )
2821
2922print ("Connecting to AP..." )
3023while not esp .is_connected :
3528 continue
3629print ("Connected to" , str (esp .ssid , 'utf-8' ), "\t RSSI:" , esp .rssi )
3730
38- #esp._debug = True
39- print ("Fetching text from" , TEXT_URL )
40- r = requests .get (TEXT_URL )
31+ requests .set_socket (socket , esp )
32+
33+ # Initialize a requests object with a socket and esp32spi interface
34+ TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
35+
36+ print ("Fetching text from %s" % TEXT_URL )
37+ response = requests .get (TEXT_URL )
38+ print ('-' * 40 )
39+
40+ print ("Text Response: " , response .text )
4141print ('-' * 40 )
42- print (r .text )
42+
43+ print ("Raw Response, as bytes: {0}" .format (response .content ))
4344print ('-' * 40 )
44- r .close ()
4545
46- print ()
47- print ("Fetching json from" , JSON_URL )
48- r = requests .get (JSON_URL )
46+ print ("Response's HTTP Status Code: %d" % response .status_code )
4947print ('-' * 40 )
50- print (r .json ())
48+
49+ print ("Response's HTTP Headers: %s" % response .headers )
5150print ('-' * 40 )
52- r .close ()
5351
54- print ("Done!" )
52+ # Close, delete and collect the response data
53+ response .close ()
0 commit comments