Skip to content

Instantly share code, notes, and snippets.

@hiway
Created December 13, 2016 08:27
Show Gist options
  • Save hiway/b3686a7839acca7d62e3a7234fdbb438 to your computer and use it in GitHub Desktop.
Save hiway/b3686a7839acca7d62e3a7234fdbb438 to your computer and use it in GitHub Desktop.
Micropython (ESP8266): Download files from a known server to populate the filesystem with necessary files.
import ujson as json
import uos as os
import usocket as socket
# todo: https
def http_get_async(url):
_, _, host, path = url.split('/', 3)
if ':' in host:
host, port = host.split(':')
else:
port = 80
addr = socket.getaddrinfo(host, port)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host),
'utf8'))
started = False
header_str = b''
headers = {}
while True:
data = s.recv(1000)
if data:
body_bytes = data
if not started and b'HTTP' in body_bytes:
started = True
continue
buffer = header_str + body_bytes
if not headers and not b'\r\n\r\n' in buffer:
header_str += body_bytes
continue
if b'\r\n\r\n' in buffer:
headers, body_bytes = buffer.split(b'\r\n\r\n')
if body_bytes:
yield body_bytes
else:
break
def http_get(url):
response = b''
try:
get = http_get_async(url)
while True:
file_bytes = get.send(None)
response += file_bytes
except StopIteration:
pass
response_str = str(response, 'utf-8')
return response_str
def ensure_dirs(path):
split_path = path.split('/')
if len(split_path) > 1:
for i, fragment in enumerate(split_path):
parent = '/'.join(split_path[:-i])
try:
os.mkdir(parent)
except OSError:
pass
def http_get_to_file(url, path):
ensure_dirs(path)
with open(path, 'w') as outfile:
try:
get = http_get_async(url)
while True:
file_bytes = get.send(None)
outfile.write(file_bytes)
except StopIteration:
outfile.close()
def start(url='http://192.168.1.107:5000/esp8266/files.json'):
response = json.loads(http_get(url))
for file in response['files']:
path = file['path']
url = 'http://192.168.1.107:5000/esp8266/{}'.format(path)
http_get_to_file(url, path)
if __name__ == '__main__':
start()
@patrick-rt
Copy link

I always get this error on the ESP32:
MemoryError: memory allocation failed, allocating 13925 bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment