The main part is pasted below. I'm interested if it could be done better: memory errors are showing up mostly when I'm replacing parts of html_content. I need that part, as server is updating html with current variable values while running.
"l" (in "l.set_power()", ...) is class instance of my led driver.
Also I'm trying to save memory by importing only needed things from libraries, for example "from machine import Pin, PWM, Timer" instead of just "import machine" - does this give much savings?
Code: Select all
#--------------------------------------
gc.collect()
f = open('index.html')
html_content = f.read()
f.close()
content = ''
def main():
global html_content, content
s = socket.socket()
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print('Listening, connect your browser to http://'+str(wifi.wifi.ifconfig()[0])+':80/')
while True:
conn, address = s.accept()
# read request
req = conn.readline().decode('utf-8')
# read rest of the request ...
while True:
line = conn.readline()
if not line or line == b'\r\n':
break
# and discard it
del line
print(str(req))
# send header
conn.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
# check the request
match = ure.search('smooth', req)
if match:
match = ure.search('power=[0-9]+', req)
if match:
l.set_power( int( match.group(0).split('=')[1] ) )
match = ure.search('balance=[0-9]+', req)
if match:
l.set_balance( int( match.group(0).split('=')[1] ) )
# if no parameters found, serve main webpage
else:
content = html_content.replace('{power}', str( l.get_power() ) )
content = content.replace('{balance}', str( l.get_balance() ) )
content = content.replace('{time}', str( l.get_time() ) )
content = content.replace('{ip_address}', str( wifi.wifi.ifconfig()[0] ) )
#content = html_content.format( power=l.get_power(), balance=l.get_balance(), time=l.get_time, ip_address=wifi.wifi.ifconfig()[0] )
# send web page in smaller parts
i = 0
while i<len(content)-1:
if len(content)-1<256:
i += conn.send(content[i:])
print("Data sent: "+str(i))
else:
i += conn.send(content[i:i+256])
print("Data sent: "+str(i))
# close connection
conn.close()
# update leds
l.smooth()
# a little cleanup
gc.collect()
main()