Instantly share code, notes, and snippets.

Embed
What would you like to do?
M5Stack sample code: Download a JPEG image from given URL, then display it on LCD.
from m5stack import lcd
import socket
import os
def split_list(l, s):
count = 0
ni = []
for i in range(0, len(l)-len(s)):
if l[i] == s[count]:
count=count+1
if count == len(s):
lcd.println("Matched")
ni.append(i+1)
count = 0
else:
count = 0
if len(ni) == 0:
return l
else:
r = []
pi = 0
for j in range(0, len(ni)):
r.append(l[pi:(ni[j]-len(s))])
pi = ni[j]
r.append(l[pi:len(l)])
return r
def http_getFile(url,file):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
lcd.println(str(path))
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(200)
lcd.println(str(len(data)))
if data:
l = split_list(data,[13,10,13,10])
if len(l) == 2:
lcd.println(str(len(l[0])))
lcd.println(str(len(l[1])))
file.write(l[1])
break
while True:
data = s.recv(100)
if data:
file.write(data)
else:
break
lcd.println("File got")
s.close()
file.close()
lcd.clear()
lcd.setCursor(0, 0)
lcd.setColor(lcd.WHITE)
os.remove("logo.jpg")
f = open('logo.jpg','w')
http_getFile("http://www.okadajp.org/RWiki/Rlogo-old.jpg", f)
#lcd.clear()
lcd.image(0, 0, file="logo.jpg", scale=0, type=lcd.JPG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment