1
+ # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ from os import getenv
5
+
6
+ import adafruit_connection_manager
7
+ import adafruit_requests
8
+ import board
9
+ import busio
10
+ from digitalio import DigitalInOut
11
+
12
+ from adafruit_esp32spi import adafruit_esp32spi
13
+
14
+ # Get wifi details and more from a settings.toml file
15
+ # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
16
+ ssid = getenv ("CIRCUITPY_WIFI_SSID" )
17
+ password = getenv ("CIRCUITPY_WIFI_PASSWORD" )
18
+
19
+ print ("ESP32 SPI webclient test" )
20
+
21
+ TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
22
+ JSON_URL = "http://wifitest.adafruit.com/testwifi/sample.json"
23
+
24
+
25
+ # If you are using a board with pre-defined ESP32 Pins:
26
+ esp32_cs = DigitalInOut (board .ESP_CS )
27
+ esp32_ready = DigitalInOut (board .ESP_BUSY )
28
+ esp32_reset = DigitalInOut (board .ESP_RESET )
29
+
30
+ spi = board .SPI ()
31
+ esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
32
+
33
+ pool = adafruit_connection_manager .get_radio_socketpool (esp )
34
+ ssl_context = adafruit_connection_manager .get_radio_ssl_context (esp )
35
+ requests = adafruit_requests .Session (pool , ssl_context )
36
+
37
+ if esp .status == adafruit_esp32spi .WL_IDLE_STATUS :
38
+ print ("ESP32 found and in idle mode" )
39
+ print ("Firmware vers." , esp .firmware_version )
40
+ print ("MAC addr:" , ":" .join ("%02X" % byte for byte in esp .MAC_address ))
41
+
42
+ for ap in esp .scan_networks ():
43
+ print ("\t %-23s RSSI: %d" % (ap .ssid , ap .rssi ))
44
+
45
+ print ("Connecting to AP..." )
46
+ while not esp .is_connected :
47
+ try :
48
+ esp .connect_AP (ssid , password )
49
+ except OSError as e :
50
+ print ("could not connect to AP, retrying: " , e )
51
+ continue
52
+ print ("Connected to" , esp .ap_info .ssid , "\t RSSI:" , esp .ap_info .rssi )
53
+ print ("My IP address is" , esp .ipv4_address )
54
+ print ("IP lookup adafruit.com: %s" % esp .pretty_ip (esp .get_host_by_name ("adafruit.com" )))
55
+ print ("Ping google.com: %d ms" % esp .ping ("google.com" ))
56
+
57
+ # esp._debug = True
58
+ print ("Fetching text from" , TEXT_URL )
59
+ r = requests .get (TEXT_URL )
60
+ print ("-" * 40 )
61
+ print (r .text )
62
+ print ("-" * 40 )
63
+ r .close ()
64
+
65
+ print ()
66
+ print ("Fetching json from" , JSON_URL )
67
+ r = requests .get (JSON_URL )
68
+ print ("-" * 40 )
69
+ print (r .json ())
70
+ print ("-" * 40 )
71
+ r .close ()
72
+
73
+ print ("Done!" )
0 commit comments