Page 1 of 1

How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Sun May 21, 2017 11:44 am
by riktronics
I am running MicroPython 1.8.7 in ESP12-F. Trying to add images in web pages that are hosted by the "ESP Micropython Server".
I got a little bit success, but still, it's buggy. Whenever the socket connection is closed, image loads no more and inspect elements shows "net::ERR_CONNECTION_RESET" error.
I have stored an image named alogo.gif in the file system of micropython and trying to send it when a client (browser) requests it. If I don't close socket in code, the image loads but loading bar in browser keeps on rotating forever. If stopped by clicking on "cross" (X), image vanishes. Below is my code.:

[CODE]
import machine

html = """<!DOCTYPE html>
<html>
<body>

<h2>Arduino Logo</h2>
<img src="alogo.gif" alt="Arduino">

</body>
</html>
"""
imOpen = open('alogo.gif')
image=imOpen.read()
imOpen.close()

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

s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)

while True:
cl, addr = s.accept()
#print('client connected from', addr)
cl_file = cl
while True:
line = cl_file.readline()
line2=str(line)
print(line2)
if not line or line == b'\r\n':
cl.write(html)
#cl.close() commented, so that image can load
break
if line == b'GET /alogo.gif HTTP/1.1\r\n':
cl.write(image)
# cl.close() commented, so that image can load
break
[/CODE]

I've been trying for more than 2 months. This time got a little bit success but need your help to find a proper solution. Thank you all.

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Sun Sep 03, 2017 5:11 pm
by rdagger
Did you ever resolve the problem you were having serving images?
I'd like to create a simple web server on an ESP32 and I'm trying to gather advice.

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Mon Sep 04, 2017 6:17 am
by riktronics
Yes, I solved it and made a great web server code that can even handle gzip compressed files. If you need it, ping me.

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Mon Sep 04, 2017 9:22 am
by benalb
would you mind sharing the code? github repo, perhaps :)

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Mon Sep 04, 2017 11:35 pm
by rdagger
I'd also like to see the code. Tried to send you a message on the forum but it looks like it's stuck in my outbox.
Do you have a repo?

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Tue Sep 05, 2017 7:42 am
by riktronics
I have not uploaded the code to GitHub yet. I'm bit busy now for a rocket launch program. My email ID abhra0897@gmail.com. Please email me after 3-4 days and I'll send you the code/GitHub link.

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Wed Sep 06, 2017 7:40 pm
by fdushin
There are likely many solutions out there already. Feel free to try, for example,

https://github.com/fadushin/esp8266/tre ... hon/uhttpd

It is designed more for REST-ful web applications, but it does have a file server module.

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Wed Sep 06, 2017 9:24 pm
by riktronics
That's great! Thanks for sharing.

Re: How to send image (.jpg, .gif etc) from esp8266 webserver

Posted: Thu Sep 07, 2017 6:05 am
by pythoncoder
@fdushin The HTTP daemon looks really useful. Excellent docs too :D