Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Problem with adafruit_requests library Metro M4 Airlift Lite
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Problem with adafruit_requests library Metro M4 Airlift Lite

by jstanle1 on Tue Aug 04, 2020 7:03 pm

I'm getting started with Circuit Python on the Metro M4 Airlift Lite board. I've got the latest boot loader, Python stable libraries, and ESP32 firmware. I am trying to get the adafruit_requests.py sample code to run. It does connect properly to WiFi, and pings Google without any trouble, but when it tries to get text from the Adafruit test URL it gives the following Traceback:

IP lookup adafruit.com: 104.20.38.240
Ping google.com: 30 ms
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
Traceback (most recent call last):
File "code.py", line 71, in <module>
File "adafruit_requests.py", line 291, in get
File "adafruit_requests.py", line 251, in request
File "adafruit_requests.py", line 197, in request
AttributeError: 'socket' object has no attribute 'send'

Using the requests_simpletest.py sample code from the adafruit_requests library I get a slightly different error from the same place:
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
Traceback (most recent call last):
File "code.py", line 53, in <module>
File "adafruit_requests.py", line 291, in get
File "adafruit_requests.py", line 182, in request
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 54, in getaddrinfo
File "adafruit_esp32spi/adafruit_esp32spi.py", line 461, in get_host_by_name
RuntimeError: Failed to request hostname

It looks like there's something wrong with the socket being generated.

Thanks for any ideas how to resolve these issues.

jstanle1
 
Posts: 35
Joined: Sat Dec 12, 2015 5:53 pm

Re: Problem with adafruit_requests library Metro M4 Airlift

by tannewt on Wed Aug 05, 2020 12:01 pm

Hi! Please post the current version of the code you are trying. That way it's possible to know exactly what you are doing.

One debug step would be to print out the socket object before the error.

tannewt
 
Posts: 1789
Joined: Thu Oct 06, 2016 8:48 pm

Re: Problem with adafruit_requests library Metro M4 Airlift

by jstanle1 on Wed Aug 05, 2020 4:04 pm

Thanks for your reply. The code is taken from the esp32spi_simpletest.py example in the Adafruit_CircuitPython_ESP32SPI repository.

Code: Select all | TOGGLE FULL SIZE
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests

print("ESP32 SPI webclient test")

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"


# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an AirLift Shield:
# esp32_cs = DigitalInOut(board.D10)
# esp32_ready = DigitalInOut(board.D7)
# esp32_reset = DigitalInOut(board.D5)

# If you have an AirLift Featherwing or ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)

# If you have an externally connected ESP32:
# NOTE: You may need to change the pins to reflect your wiring
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

print("Connecting to AP...")
while not esp.is_connected:
    try:
        esp.connect_AP(b"MYNET", b"PASSWORD")
    except RuntimeError as e:
        print("could not connect to AP, retrying: ", e)
        continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "http://httpbin.org/get"
JSON_POST_URL = "http://httpbin.org/post"

print("print socket: ")
print(socket)
esp._debug = True
print("Fetching text from %s" % TEXT_URL)
response = requests.get(TEXT_URL)
print("-" * 40)


(I use my actual network name and password in the running code)

The output from running this is:

ESP32 SPI webclient test
Connecting to AP...
Connected to MYNET RSSI: -66
print socket:
<module 'adafruit_esp32spi.adafruit_esp32spi_socket' from '/lib/adafruit_esp32spi/adafruit_esp32spi_socket.mpy'>
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
*** Get host by name
*** Get socket
Allocated socket #0
*** Socket connect mode 0
*** Open socket
Traceback (most recent call last):
File "code.py", line 65, in <module>
File "adafruit_requests.py", line 291, in get
File "adafruit_requests.py", line 251, in request
File "adafruit_requests.py", line 197, in request
AttributeError: 'socket' object has no attribute 'send'

It looks like the 'allocated socket' isn't set up properly, but I don't know what to make of the debug output.

Thanks for any word on this.

jstanle1
 
Posts: 35
Joined: Sat Dec 12, 2015 5:53 pm

Re: Problem with adafruit_requests library Metro M4 Airlift

by brubell on Thu Aug 06, 2020 2:02 pm

Hi jstanle1,

You may have a mismatch between the libraries on your Metro Airlift, an older version of esp32spi_socket used write instead of send.

Could you navigate to: https://circuitpython.org/libraries
and download the latest library bundle.

Replace the ESP32SPI library and the Requests library on your device.

Then, try running code.py again.

brubell
 
Posts: 1043
Joined: Fri Jul 17, 2015 10:33 pm

Re: Problem with adafruit_requests library Metro M4 Airlift

by jstanle1 on Thu Aug 06, 2020 5:42 pm

Thanks for your prompt reply -- that fixed it, socket and connection to the test web page are working fine.

jstanle1
 
Posts: 35
Joined: Sat Dec 12, 2015 5:53 pm

Please be positive and constructive with your questions and comments.