Am I running out of RAM in this example?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Am I running out of RAM in this example?

Post by eradicatore » Sun Apr 23, 2017 1:26 am

Here is my code that I'm loading into a NODEMCU which I believe has 32MB of Flash, but not sure how much RAM (is it always 96Kb?). Thing were going great, I was loading this code as "main.py" with Adafruit's "ampy" and testing often. The web page was serving up great, and then I added all the various volume levels to the "master volume" select html element and when I load the page now it stops loading about half way down the list of volumes at "0db"? Am I running out of RAM? Is the socket timing out? Is there a limit to how many characters the web page can serve?

## Wifi Connection setup
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
sta_if.connect('your ssid here', 'your password here')
while not sta_if.isconnected():
pass

do_connect()

## Simple Web Server
#import machine
#p_led = machine.Pin(2, machine.Pin.OUT)

html = """<!DOCTYPE html>
<html>
<head> <title>Denon Controller</title> </head>
<body> <h1>DenonControl</h1>
<form action="http://192.168.87.20" method="get">
Power:
<select name="powerP1">
<option value=""></option>
<option value="ON">On</option>
<option value="STANDBY">Standby</option>
</select>
<button type="submit">></button>
<br>
</form>
<form action="http://192.168.87.20" method="get">
Mute:
<select name="muteP1">
<option value=""></option>
<option value="ON">On</option>
<option value="OFF">Off</option>
</select>
<button type="submit">></button>
<br>
</form>
<form action="http://192.168.87.20" method="get">
Master Volume:
<select name="mvP1">
<option value=""></option>
<option value="98">18db(max)</option>
<option value="95">15db</option>
<option value="90">10db</option>
<option value="85">5db</option>
<option value="80">0db</option>
<option value="75">-5db</option>
<option value="70">-10db</option>
<option value="65">-15db</option>
<option value="60">-20db</option>
<option value="50">-30db</option>
<option value="40">-40db</option>
<option value="20">-60db</option>
<option value="00">-80db</option>
<option value="99">(min)</option>
</select>
<button type="submit">></button>
<br>
</form>
</body>
</html>
"""


import ure
import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

while True:
cl, addr = s.accept()
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
match = ure.search(r'GET.*powerP1=(.*) ', line)
if match:
print("PW"+str(match.group(1)), '\r')
match = ure.search(r'GET.*muteP1=(.*) ', line)
if match:
p1=match.group(1)
final = "MU"+p1
print (final)
if line == b'\r\n':
break
response = html
cl.send(response)
cl.close()

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Am I running out of RAM in this example?

Post by Roberthh » Sun Apr 23, 2017 6:07 am

Hello @eradicatore, I think it is more a problem of ampy and the way it uploads a script from it's internal worksheet. Most likely, the string assigned to html gets too log to be handled by raw REPL. EIther break up the string in pieces, or save your script into a file and upload that file to your Nodemcu. I tried the latter and my device did not complain.
About memory: Only 4 MB flash is available for MicroPython. About 600k is used by MP itself. The remaining space is used as a file system. Most of RAM is used by MP. You have about 30k remaining fro scripts, which is about 200 lines of python code. But scripts can also be embedded in Flash, which pushes the bar up, because the Python bytecode the does not need RAM, only the data it deals with.

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: Am I running out of RAM in this example?

Post by eradicatore » Sat Apr 29, 2017 2:29 pm

Thanks @Roberthh!

So I tried splitting it up into two strings, no luck. I haven't played around with it more yet, but will let you know if I figure it out. Ampy seems very reliable for the most part, so maybe I will try to see if the file is really being partially written by checking it manually?

I think I may try to learn more about this example here and see if I can do this without having to encode the whole html in the nodeMCU anyway.

https://learn.adafruit.com/micropython- ... l#software

thetreerat
Posts: 15
Joined: Thu Apr 27, 2017 6:40 pm

Re: Am I running out of RAM in this example?

Post by thetreerat » Sat Apr 29, 2017 2:59 pm

Have you tried to use the web repl? tony tutorial is here: https://learn.adafruit.com/micropython- ... l/overview

apmy nice because I can script the install of multiple files, but the repl is nice as you can upload a file and interact with the repl with out killing your buffer.

Hal

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: Am I running out of RAM in this example?

Post by eradicatore » Sun Jun 18, 2017 2:09 pm

Well I've been doing a different projects, and have been using ampy as it was the best thing I could find to quickly and easily upload files, but now I just found upyloader! viewtopic.php?f=16&t=2245 I can't tell you how much happier I am with that! It allows you to compile to bytecode, it allows you to change speed with a simple pull down, it is just amazing. Good bye Ampy, hello uPyLoader!

I hope to get back to this project here with the Denon serial controller someday, but can't say when.

My only wish is that the nodeMCU supported two UARTS. That would make my project soooooo much easier to develop.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Am I running out of RAM in this example?

Post by pythoncoder » Mon Jun 19, 2017 6:30 am

I use and thoroughly recommend rshell for all target boards https://github.com/dhylands/rshell.
Peter Hinch
Index to my micropython libraries.

Post Reply