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

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
riktronics
Posts: 4
Joined: Sun May 21, 2017 11:23 am

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

Post by riktronics » Sun May 21, 2017 11:44 am

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.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

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

Post by rdagger » Sun Sep 03, 2017 5:11 pm

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.

riktronics
Posts: 4
Joined: Sun May 21, 2017 11:23 am

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

Post by riktronics » Mon Sep 04, 2017 6:17 am

Yes, I solved it and made a great web server code that can even handle gzip compressed files. If you need it, ping me.

User avatar
benalb
Posts: 25
Joined: Fri May 19, 2017 1:23 pm

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

Post by benalb » Mon Sep 04, 2017 9:22 am

would you mind sharing the code? github repo, perhaps :)

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

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

Post by rdagger » Mon Sep 04, 2017 11:35 pm

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?

riktronics
Posts: 4
Joined: Sun May 21, 2017 11:23 am

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

Post by riktronics » Tue Sep 05, 2017 7:42 am

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.

User avatar
fdushin
Posts: 32
Joined: Thu Jul 21, 2016 5:38 pm

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

Post by fdushin » Wed Sep 06, 2017 7:40 pm

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.

riktronics
Posts: 4
Joined: Sun May 21, 2017 11:23 am

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

Post by riktronics » Wed Sep 06, 2017 9:24 pm

That's great! Thanks for sharing.

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

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

Post by pythoncoder » Thu Sep 07, 2017 6:05 am

@fdushin The HTTP daemon looks really useful. Excellent docs too :D
Peter Hinch
Index to my micropython libraries.

Post Reply