ESP8266 Running Problems - Memory [Python]

Thread Starter

levi_ackerman

Joined Mar 25, 2023
23
I am using my ESP8266 device and have installed Micropython as firmware on it.

I wrote some python code to fetch environmental data through a DHT11.
The scripts I'm running with the ampy module, specifically, the command "ampy --port COM5 put file.py" and the command "ampy --port COM5 run file.py".

For some scripts the execution is successful, but with a particular script, in which I import a library called "micropython modbus", the program returns this error:
Traceback (most recent call last):
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 192, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\Scripts\ampy.exe\__main__.py", line 7, in <module>
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\click\core.py", line 1157, in __call__
return self.main(*args, **kwargs)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\click\core.py", line 1078, in main
rv = self.invoke(ctx)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\click\core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\click\core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\click\core.py", line 783, in invoke
return __callback(*args, **kwargs)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\ampy\cli.py", line 338, in run
output = board_files.run(local_file, not no_output, not no_output)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\ampy\files.py", line 309, in run
self._pyboard.execfile(filename, stream_output=True)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\ampy\pyboard.py", line 285, in execfile
return self.exec_(pyfile, stream_output=stream_output)
File "C:\Users\carmi\AppData\Local\Programs\Python\Python38\lib\site-packages\ampy\pyboard.py", line 279, in exec_
raise PyboardError('exception', ret, ret_err)
ampy.pyboard.PyboardError: ('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 5, in <module>\r\n File "/lib/umodbus/tcp.py", line 20, in <module>\r\nMemoryError: memory allocation failed, allocating 72 bytes\r\n')"
This error apparently shows a memory leak in my ESP8266.

Now I ask you if by chance you can give me some advice to solve this problem.
Do you know an alternative to run python scripts on the ESP8266? Am I doing something wrong??
 

Ya’akov

Joined Jan 27, 2019
8,505
Welcome to AAC.

In general, the ESP8266 boards with have a small memory size.

Are you just plain running out of memory?
Have you tried an alternative like a more capable ESP32?

You might also find the RPi Pico W to be a good choice for MicroPython because it is basically designed for it and getting the program onto the dev board is far simpler.
 

Thread Starter

levi_ackerman

Joined Mar 25, 2023
23
Welcome to AAC.

In general, the ESP8266 boards with have a small memory size.

Are you just plain running out of memory?
Have you tried an alternative like a more capable ESP32?

You might also find the RPi Pico W to be a good choice for MicroPython because it is basically designed for it and getting the program onto the dev board is far simpler.
I'll explain my situation.

For a university project I'm trying to create a modbus TCP architecture to make my master (which in my case is a BeagleBone Black) communicate with my slaves (ESP8266).

I was testing to run python files on my esp and i got this error when i imported this python library for modbus with ampy.
Isn't there another solution without necessarily changing the development board or in general for IoT devices do you have to import the scripts into the card? isn't there a way to run them and activate tabs "remotely" without using space? Excuse the ignorance

In case it is not possible, I have to buy another one apparently because the "micropython-modbus" library is heavy.

If so, how do these look to you? Could they be fine considering the storage space? DUBEUYEW Raspberry Pi Pico.

thanks,
 

Thread Starter

levi_ackerman

Joined Mar 25, 2023
23
Welcome to AAC.

In general, the ESP8266 boards with have a small memory size.

Are you just plain running out of memory?
Have you tried an alternative like a more capable ESP32?

You might also find the RPi Pico W to be a good choice for MicroPython because it is basically designed for it and getting the program onto the dev board is far simpler.
Stupid question: If ESP8266s don't have that much memory, how are they used? For just simple commands ?
 

Ya’akov

Joined Jan 27, 2019
8,505
I'll explain my situation.

For a university project I'm trying to create a modbus TCP architecture to make my master (which in my case is a BeagleBone Black) communicate with my slaves (ESP8266).

I was testing to run python files on my esp and i got this error when i imported this python library for modbus with ampy.
Isn't there another solution without necessarily changing the development board or in general for IoT devices do you have to import the scripts into the card? isn't there a way to run them and activate tabs "remotely" without using space? Excuse the ignorance

In case it is not possible, I have to buy another one apparently because the "micropython-modbus" library is heavy.

If so, how do these look to you? Could they be fine considering the storage space? DUBEUYEW Raspberry Pi Pico.

thanks,
If you don't need the wireless, and you do need memory, the ESP8266 seems a very poor choice. The RPi Pico, which I did mention (though you can drop the W if WiFi isn't needed) is an excellent choice. But to know if it is going to work you really need to profile the code to see how big it is when it runs to be sure you have sufficient memory.

The RPi Pico has other advantages as well, such as PIO (Programmable IO) which might come in handy, as well as being designed with MicroPython as a principal programming language. So, I think it is an excellent option.

As always, the right thing to do it try it. Buy one and test your programs on it. Profile resources and performance, and get an idea of how well it works for you. They are about twice the price of the cheapest ESP8266 boards, but if you were using the Node LUA type boards they will be comparable.
 

Ya’akov

Joined Jan 27, 2019
8,505
Stupid question: If ESP8266s don't have that much memory, how are they used? For just simple commands ?
No, they are programmed with a compiled language—generally C/C++—and with libraries that are targeting the smaller memory size. Dynamic languages are generally not used for programming embedded controllers because of th e inefficiency. Sometimes, even lower level, assembler programs are used to maximize performance and resources.
 

Thread Starter

levi_ackerman

Joined Mar 25, 2023
23
If you don't need the wireless, and you do need memory, the ESP8266 seems a very poor choice. The RPi Pico, which I did mention (though you can drop the W if WiFi isn't needed) is an excellent choice. But to know if it is going to work you really need to profile the code to see how big it is when it runs to be sure you have sufficient memory.

The RPi Pico has other advantages as well, such as PIO (Programmable IO) which might come in handy, as well as being designed with MicroPython as a principal programming language. So, I think it is an excellent option.

As always, the right thing to do it try it. Buy one and test your programs on it. Profile resources and performance, and get an idea of how well it works for you. They are about twice the price of the cheapest ESP8266 boards, but if you were using the Node LUA type boards they will be comparable.
I need the wifi module to use APIs in Flask and allow devices to communicate through the Modbus TCP infrastructure. In these APIs the functions of the modbus library will be used.

How much memory does an RPi Pico have on average? Have you ever experimented with some code? Just to get an idea.
 

Thread Starter

levi_ackerman

Joined Mar 25, 2023
23
No, they are programmed with a compiled language—generally C/C++—and with libraries that are targeting the smaller memory size. Dynamic languages are generally not used for programming embedded controllers because of th e inefficiency. Sometimes, even lower level, assembler programs are used to maximize performance and resources.
I ran scripts to view memory specifically for my ESP8266.

I managed to get the results, I show you below:

For RAM:
Total memory: 37952 bytes
Free memory: 33744 bytes

For Flash memory:

Total flash memory: 16777216 bytes
Flash memory used: 13819904 bytes
Free flash memory: 2957312 bytes

How do these values look to you? comparing them to those of the Raspberry you recommended, they look similar.

Doing research it seems it's memory's fault
RAM too small that I can't run the code? Could you help me? I do not know what to do.
 

Ya’akov

Joined Jan 27, 2019
8,505
I ran scripts to view memory specifically for my ESP8266.

I managed to get the results, I show you below:

For RAM:
Total memory: 37952 bytes
Free memory: 33744 bytes

For Flash memory:

Total flash memory: 16777216 bytes
Flash memory used: 13819904 bytes
Free flash memory: 2957312 bytes

How do these values look to you? comparing them to those of the Raspberry you recommended, they look similar.

Doing research it seems it's memory's fault
RAM too small that I can't run the code? Could you help me? I do not know what to do.
The memory size is only half of the information. The total resident size to the firmware and code in RAM is the important thing. The MicroPython implementation for the RPi is the native option unlike the ESP8266.

As I said, you can profile the the memory allocation after the program is loaded up, or, and almost certainly better, you can get an RPi Pico W and try it. The empirical route is going to be the most certain and you will get an immediate answer.

Which ESP8266 development board are you using?
 

Thread Starter

levi_ackerman

Joined Mar 25, 2023
23
The memory size is only half of the information. The total resident size to the firmware and code in RAM is the important thing. The MicroPython implementation for the RPi is the native option unlike the ESP8266.

As I said, you can profile the the memory allocation after the program is loaded up, or, and almost certainly better, you can get an RPi Pico W and try it. The empirical route is going to be the most certain and you will get an immediate answer.

Which ESP8266 development board are you using?
Specifically I'm developing with these boards that I got on Amazon: ESP8266.
 
Top