I have worked on this one for about ten hours and finally got it to work Page 145 of MicroPython Programming with ESP32 and ESP8266 (version 1.0) goes on to build a simple web server. I could get everything to work except for the browser to display Hello, World! To see where things were going wrong I started putting print() commands into the code to see where the code was at the time. Right after response = web_page() I put print(‘response is: ‘) on one line and the next has print(response). In the serial window I could see the response being printed. The next line conn.send(response) didn’t seem to be sending anything to the browser. I put a sleep(5) just after conn.send and before conn.close(). That didn’t help.
I ended up reading a lot more tonight about the .send() for sending html stuff. One person said to make sure to include HTTP status codes, but didn’t give any information about what to include. More reading and figuring out what to put into the code that is presented in this module.
I ended up adding some lines into the conn.send() part of the code. Here is what I included:
Start your code here
def web_page():
html = “””<!DOCTYPE html><html><head><meta name=”viewport” content=”width=device-width, initial-scale=1″></head>
<body><h1>Hello, World!</h1></body></html>”””
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((”, 80))
s.listen(5)
while True:
conn, addr = s.accept()
print(‘Got a connection from %s’ % str(addr))
request = conn.recv(1024)
print(‘Content = %s’ % str(request))
response = web_page()
conn.send(‘HTTP/1.1 200 OK’)
conn.send(‘Content-Type: text/html; encoding=utf8\nContent-Length: ‘)
conn.send(str(len(response)))
conn.send(‘\nConnection: close\n’)
conn.send(‘\n’)
conn.send(response)
conn.close()
I added five conn.send() lines (maybe the fifth isn’t needed, but it was recommended.) and <!DOCTYPE html> inside the html definition toward the top.
Did anyone else have trouble with this module of this new MicroPython book? Perhaps my troubles are with the MicroPython .bin file firmware I downloaded and installed. I am using esp32-20190113-v1.9.4-779-g5064df207.bin . It is fairly recent and does allow for waking the ESP32 from Deep_Sleep with a touch pin. I was excited to get that working.
Thanks.
Hi.
Thank you for sharing.
We were using an old .bin file. We’ll be testing the code with the latest version and see what we get.
I remember that when doing our first web server with MicroPython, it didn’t work with the <!DOCTYPE html> expression. That’s why we didn’t include it in our examples.
I’ve just tested our example with the latest firmware release from the MicroPython Downloads page, and it is working just fine.
What web browser are you using?
Hi Sara,
I am using a MacBook Pro to program the ESP32. The browser I am using to view the web pages from the ESP32 are on an iPhone, and an iPad using Safari. My MacBook also cannot receive the web page in Safari. It is like the data doesn’t ever show up.
I really wonder if anyone has had the trouble that I had.
I have gone all the way through the book and I find that it is really well written.
Greg