#Hey I m running to some issues with

1 messages · Page 1 of 1 (latest)

jolly lake
#
import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import os
import gc


def runtest():
  TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
  JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php"

  print("Hello World!")

  for network in wifi.radio.start_scanning_networks():
    print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
      network.rssi, network.channel))
  wifi.radio.stop_scanning_networks()

  print("Connecting to %s"%os.getenv("CIRCUITPY_WIFI_SSID"))
  wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
  print("Connected to %s!"%os.getenv("CIRCUITPY_WIFI_SSID"))
  print("My IP address is", wifi.radio.ipv4_address)

  ipv4 = ipaddress.ip_address("8.8.4.4")
  print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))

  pool = socketpool.SocketPool(wifi.radio)
  requests = adafruit_requests.Session(pool, ssl.create_default_context())

  print("Fetching text from", TEXT_URL)
  response = requests.get(TEXT_URL)
  print("-" * 40)
  print(response.text)
  print("-" * 40)

  print("Fetching json from", JSON_QUOTES_URL)
  response = requests.get(JSON_QUOTES_URL)
  print("-" * 40)
  print(response.json())
  print("-" * 40)

  gc.collect()
  print("done")
#

I only need to run it twice before I start running into memory errors

#

this also happens with an unexpectedmaker featherS2, and subsequent runs always run out of memory in the same spot

#
Fetching json from https://www.adafruit.com/api/quotes.php
Traceback (most recent call last):
  File "adafruit_requests.py", line 534, in _get_socket
espidf.MemoryError:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 39, in runtest
  File "adafruit_requests.py", line 728, in get
  File "adafruit_requests.py", line 668, in request
  File "adafruit_requests.py", line 515, in _get_socket
RuntimeError: Sending request failed
``` This spot (fetching json)
torn rock
#

Have you tried running this saved as code.py instead?

#

You can add a while True: loop before the fetching text statement to loop.

#

This is unusual.

jolly lake
#

I was just retrying to see how many times before I got the memory error

torn rock
#

OK. Can you try that? I am wondering if calling wifi connect and socketpool multiple times could be an issue.

jolly lake
#

I can put that outside of the function def so it runs once on import, but i believe that it would throw memory errors earlier if that was the case no?

#

II guess that was it... it swore I tired that before...

#

working code:

import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import os
import gc

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php"

print("Hello World!")

for network in wifi.radio.start_scanning_networks():
  print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
    network.rssi, network.channel))
wifi.radio.stop_scanning_networks()

print("Connecting to %s"%os.getenv("CIRCUITPY_WIFI_SSID"))
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Connected to %s!"%os.getenv("CIRCUITPY_WIFI_SSID"))
print("My IP address is", wifi.radio.ipv4_address)

ipv4 = ipaddress.ip_address("8.8.4.4")
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

def runtest():
  print("Fetching text from", TEXT_URL)
  response = requests.get(TEXT_URL)
  print("-" * 40)
  print(response.text)
  print("-" * 40)

  print("Fetching json from", JSON_QUOTES_URL)
  response = requests.get(JSON_QUOTES_URL)
  print("-" * 40)
  print(response.json())
  print("-" * 40)

  gc.collect()
  print("done")
torn rock
#

How many times have you run that?

jolly lake
#

like 30

#

ill throw in loop

torn rock
#

Well, that seems good

jolly lake
#

yeah no issues in infinite loop