MemoryError When Connecting Pico W with Azure IoT Central

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

First off, thank you to the Adafruit team for providing so much free information, software, and great products. Following the Pico W with Azure IoT guide on Adafruit (https://learn.adafruit.com/pico-w-wifi- ... ot-central), I am getting a MemoryError when connecting my Pico W to Azure IoT Central and hope you can help me!

Status printout in Shell from Pico W:

Code: Select all

]0;🐍REPL | 8.0.0-beta.3 \
Adafruit CircuitPython 8.0.0-beta.3 on 2022-10-20; Raspberry Pi Pico W with rp2040
Steps Taken
  • Fresh install of CircuitPython
  • Created Azure Application, created & connected device, obtained credentials
  • Created .env file w/ Azure credentials (id_scope, device_id and device_primary_key)
  • Downloaded & installed the Project Bundle from Pico W with Azure IoT
  • Ran the "Pico W Basic WiFi Test" ==> PASSED
  • Ran the "Pico W Requests Test (Adafruit Quotes)" ==> PASSED
  • Ran the "Azure IoT Central Test" ==> FAILED
Error Message

Code: Select all

>>> %Run -c $EDITOR_CONTENT
Connecting to WiFi...
Connected to WiFi!
Year seems good, skipping set time.
Connecting to Azure IoT Central...
Traceback (most recent call last):
  File "<stdin>", line 47, in <module>
  File "adafruit_azureiot/iotcentral_device.py", line 159, in connect
  File "adafruit_azureiot/device_registration.py", line 204, in register_device
  File "adafruit_azureiot/device_registration.py", line 141, in _start_registration
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 874, in loop
MemoryError: memory allocation failed, allocating 65536 bytes
>>> 
Since I have two Pico W's, I have tried this on both of them, and they both ran into the same error. I've searched around online and cannot find a clear solution, and there is no mention of this on the Adafruit site.

System Info
IDE:
  • Thonny 4.0.1
  • Darwin 21.6.0
  • Python 3.10.6
  • Tk 8.6.12
Computer:
  • MacBook Pro (14-inch, 2021) M1 Pro 16GB
  • macOS Monterey 12.6
Microcontroller:
  • Raspberry Pi Pico W RP2040 x 2
I hope you can help me, apologies if I am making a rookie mistake here... Thank you in advance!
Attachments
code.py script from Adafruit (2)
code.py script from Adafruit (2)
Screen Shot 2022-11-07 at 7.30.07 PM.png (313.81 KiB) Viewed 273 times
code.py script from Adafruit (1)
code.py script from Adafruit (1)
Screen Shot 2022-11-07 at 7.29.57 PM.png (320.22 KiB) Viewed 273 times
error message in Shell
error message in Shell
pico-w_circutpython_memory_error.png (99.85 KiB) Viewed 273 times

User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

Update: I noticed CircuitPython 8.0.0-beta.4 was released, and I figured I'd give that a shot. Unfortunately, with beta.4 I am unable to simply connect to my wifi network due to a missing module named 'ipaddress'.

The instructions on the guide Pico W Basic WiFi Test https://learn.adafruit.com/pico-w-wifi- ... -wifi-test clearly state
No libraries from the library bundle are used in this example, so the lib folder is empty.
. When I download the project bundle, there are no libraries included with the code.py wifi-test script. I've searched the CircuitPython library bundle for a module named 'ipaddress' and cannot find it :(

Has anyone else had success connecting your Pico W to WiFi? If so, how about using the requests library? I have no issues using MicroPython to connect to Wifi, create sockets, etc.

I have 3 sensors by Adafruit and cannot find drivers without using CircutPython...

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

Hi,

Use import ipaddress to fix your problem with 8.0.0 Beta 4. It is an internal library now.

Bruce

User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

Hi Bruce,

Thanks for the reply! The ipaddress module is being imported at the top of the script. I downloaded the Project Bundle directly from the Pico W Basic WiFi Test (https://learn.adafruit.com/pico-w-wifi- ... -wifi-test). After copying over the single file in the bundle (code.py) to the Pico W, I then ran the script.

Code: Select all

# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import os
import ipaddress
import wifi
import socketpool

print()
print("Connecting to WiFi")

#  connect to your SSID
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))

print("Connected to WiFi")

pool = socketpool.SocketPool(wifi.radio)

#  prints MAC address to REPL
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])

#  prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)

#  pings Google
ipv4 = ipaddress.ip_address("8.8.4.4")
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
Attachments
wifi_test from Pico W Basic WiFi Test
wifi_test from Pico W Basic WiFi Test
Screen Shot 2022-11-08 at 5.26.11 PM.png (399.5 KiB) Viewed 258 times

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

Hi,

I am using CP 8.0.0 Beta 4 on a RPi Pico W RP2040 with the following code segment:

Code: Select all

import wifi       # 8.0.0 Beta 4
import ipaddress  # 8.0.0 Beta 4
import socketpool # 8.0.0 Beta 4
I do not get the error that you do.

Do you have any files named ipaddress.py, ipaddress.txt, ipaddress.foo, etc. in the lib, root, etc. directory?. That can confuse things. The system looks for any import in the os.path then internal last. That way if you have a better library in the lib directory, it will be used instead of the internal library.

What is in the boot_out.txt file?

Bruce

User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

Hello,

Interesting, good to know it is working for you. I can't find any files named ipaddress.xx on the Pico, attached a screenshot of the entire file tree. The boot_out.txt file was auto generated when CP was installed, it reads:

Code: Select all

Adafruit CircuitPython 8.0.0-beta.4 on 2022-10-30; Raspberry Pi Pico with rp2040
Board ID:raspberry_pi_pico
UID:E66164084357B328
I'm going to try a flash_nuke and fresh install of CP. Maybe something went wrong when upgrading?
Attachments
pico-file-tree.jpg
pico-file-tree.jpg (157.81 KiB) Viewed 248 times
boot-out.jpg
boot-out.jpg (40.21 KiB) Viewed 248 times

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

You are using the RPi Pico CP file on a RPi Pico W.
You need the RPi Pico W CP file.

Let’s get the right CP file and see if that helps.

Bruce

User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

Thanks Bruce,

You're absolutely right, thank you for pointing that out! With CP for Pico W installed, the WiFi and Adafruit requests tests are working correctly.

However, I am still unable to connect the Pico to Azure IoT, the same MemoryError persists. I have triple checked my env variables, they are correct.

Do you have any insight on this?

Code: Select all

>>> %Run -c $EDITOR_CONTENT
Connecting to WiFi...
Connected to WiFi!
Year seems good, skipping set time.
Connecting to Azure IoT Central...
Traceback (most recent call last):
  File "<stdin>", line 47, in <module>
  File "adafruit_azureiot/iotcentral_device.py", line 159, in connect
  File "adafruit_azureiot/device_registration.py", line 204, in register_device
  File "adafruit_azureiot/device_registration.py", line 141, in _start_registration
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 874, in loop
MemoryError: memory allocation failed, allocating 65536 bytes
>>> 
Thanks again for your help so far.
Attachments
Screen Shot 2022-11-11 at 4.27.38 PM.png
Screen Shot 2022-11-11 at 4.27.38 PM.png (442.92 KiB) Viewed 241 times
Screen Shot 2022-11-11 at 4.26.47 PM.png
Screen Shot 2022-11-11 at 4.26.47 PM.png (87.4 KiB) Viewed 241 times
Screen Shot 2022-11-11 at 4.26.12 PM.png
Screen Shot 2022-11-11 at 4.26.12 PM.png (94.66 KiB) Viewed 241 times

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

Hi,

I am not sure what the error is. You may have the error masked by the try: except: trap.
Try replacing the try: with an if True: and the except: with an else:
You should get a more specific error that may help.

Bruce

User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

Updated to use the if/else instead of try/except, and the same error was logged to the console. I don't think the while True loop (line 52) is ever reached, the error is coming from device.connect() on line 45. The error seems to be tracing back to

Code: Select all

File "adafruit_minimqtt/adafruit_minimqtt.py", line 874, in loop
.
Attachments
Screen Shot 2022-11-11 at 4.54.28 PM.png
Screen Shot 2022-11-11 at 4.54.28 PM.png (188.22 KiB) Viewed 238 times

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

Hi,

Generally if a line shows a error, the error may be in one of the lines above.
I would have a close look at the .env file. You might try putting a chunk of code at a good place near line 35 that prints out each item in your .env file.
print(os.getenv(‘password’) etc. Use a separate line for each print.

There is no reason your code should be trying to allocate 65536 bytes for anything.

Bruce

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

I find it interesting that some of the comment lines in the while loop have a tan bar in front of them. I have seen that when tabs are used for spacing instead of space characters. For uniformity you may want to replace the tabs with spaces.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

As an asside, I would use this chunk of code ### to set the rtc time. NTP does not always connect, so a while/try: loop works better.

Code: Select all

#  ntp clock
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool)
got_time = False ###
while not got_time: ###
  try: ###
    rtc.RTC().datetime = ntp.datetime ###
    got_time = True ###
  except: ###
    time.sleep(1) ###

if time.localtime().tm_year < 2022:
    print("Setting System Time in UTC")
    rtc.RTC().datetime = ntp.datetime
else:
    print("Year seems good, skipping set time.")

User avatar
jbenowitz
 
Posts: 15
Joined: Tue Nov 01, 2022 6:45 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by jbenowitz »

I would have a close look at the .env file. You might try putting a chunk of code at a good place near line 35 that prints out each item in your .env file.
Added this to top of the script, .env variables are all printing properly. This is the output in Shell:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
--- .env variables ---
test_string: Hello World
SSID: RedSpoon
id_scope: 0ne00824092
device_id: 1ymiyal5o9n
device_primary_key: AeGcEjdBN4U6VYLa/bM4LQbDhwl+LO3orVKHsk0XNZo=
-----------------
Connecting to WiFi...
Connected to WiFi!
Year seems good, skipping set time.
Connecting to Azure IoT Central...
Traceback (most recent call last):
  File "<stdin>", line 56, in <module>
  File "adafruit_azureiot/iotcentral_device.py", line 159, in connect
  File "adafruit_azureiot/device_registration.py", line 204, in register_device
  File "adafruit_azureiot/device_registration.py", line 141, in _start_registration
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 874, in loop
MemoryError: memory allocation failed, allocating 65536 bytes
>>> 
I find it interesting that some of the comment lines in the while loop have a tan bar in front of them. I have seen that when tabs are used for spacing instead of space characters. For uniformity you may want to replace the tabs with spaces.
You're right - those were tabs and not spaces. I have removed those lines completely, they were just comments.
As an asside, I would use this chunk of code ### to set the rtc time. NTP does not always connect, so a while/try: loop works better.
Replaced rtc chunk with the while/try code snippet you provided, thank you for that tip.

After all of this, still getting the same MemoryError... allocating 65536 bytes? I also reset the flash and freshly downloaded the adafruit libraries back on the pico. Doubled checked I am using a solid micro usb data cable.
Attachments
Screen Shot 2022-11-15 at 9.11.36 PM.png
Screen Shot 2022-11-15 at 9.11.36 PM.png (448.49 KiB) Viewed 215 times
Screen Shot 2022-11-15 at 9.11.24 PM.png
Screen Shot 2022-11-15 at 9.11.24 PM.png (176.35 KiB) Viewed 215 times
Screen Shot 2022-11-15 at 9.04.57 PM.png
Screen Shot 2022-11-15 at 9.04.57 PM.png (32.76 KiB) Viewed 215 times

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: MemoryError When Connecting Pico W with Azure IoT Central

Post by blakebr »

Let’s see how much memory you are working with.

Put import gc with your other imports. gc stands for garbage collect.

Before the line your program crashes put print(gc.mem_free()) to see what we are working with.

I am thinking the Pico may not have enough memory.

You can try putting gc.collect() before the mem_free() on a second run to see if it helps.

Bruce

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”