A WEB Server on the ESP32

A "Hello World!" WEB server

Writing a WEB server from scratch is not a trivial task but then the need of a WEB server is so common that you would expect that some kind soul has done the job for you. On MicroPython you find several WEB servers ready for deployment. I have selected picoweb because it is small enough to be easily studied and it has all the functionality needed for the projects in this course. On the other hand picoweb depends on several modules that are not installed in MicroPython by default. I therefore collected everything that is needed and compiled picoweb into the MicroPython binary making it available for immediate use.

As examples we provide 3 different versions of the Hello World WEB server:

  • Version1 does not use picoweb at all but opens a socket and listens to requests. Once it sees a request it sends some HTML text, stored in the MicroPython source code
  • Version 2 exports the HTML code to a separate HTML file. It uses picoweb and answers if either serverHost/ or serverHost/index.html is requested. The HTML file is read and its text returned to the caller
  • Version 3 works in a similar fashion to Version 2 but the HTML file is gzipped. Since the ESP32 has little free "disk space", well. space in its flash, it is interesting to compress the HTML file to save space.
Version 1

Let's start simple and produce a WEB server that just sends "Hello World!" to the browser when called. In the first version we will create a TCP server creating a stream socket and binding to port 80.

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try:
s.bind(('',80))
except OSError as exception:
if exception.args[0] == uerrno.EADDRINUSE:
print("Socket is already bound, please reset the machine")
sys.exit()

The server then starts listening for an incoming connection request and establishes a connection by accepting it.

s.listen(5)
print("Starting the Hello World WEB server on IP address ",ipaddr,"port 80")
while True:
conn,addr=s.accept() print("GOT a connection from %s" % str(addr))

Finally it reads an http request from the connection and answers with its Hello World page:

request=conn.recv(1024)
print("Content %s" % str(request))
request=str(request) response=html conn.send(response) conn.close()

html is the http string to be sent.

Please check https://docs.micropython.org/en/latest/library/usocket.html for more details.

Version 2:

In the second version we will separate the HTML text from the server and store it in the html directory (to be created with ampy: ampy mkdir /html) on the ESP32. Now we can use a ready made Web server for MicroPython, picoweb to serve the page. picoweb and utemplates (see exercise 2) are already included in the MicroPython binary.

Studying example_webapp.py in the base directory of picoweb will show you how to do this.

Version 3:

If we do not use an external SD card, then space for the HTML pages is rather limited. It may therefore be interesting to use gzip compresses pages. Modify your picoweb server to use gzipped HTML.

and finally a screen dump of the WEB pages as seen by the browser

helloWorldWEBServerV1.png

Making the WEB page interactive

The WEB server above has one major flaw: for each measurement the complete page must be reloaded. Wouldn't it be nice if we could keep the table and only replace the measurement results and time stamp?

Such types of actions can be accomplished with javascript. javascript is a full programming language for WEB programming running on the client side, in the browser. Teaching also javascript would largely exceed the scope of this course. However, if you don't know javascript well enough (as was the case for me!) here is a nice tutorial which will put you on track quickly:

https://www.w3schools.com/js/DEFAULT.asp

Controlling the ESP32 User Led

Before looking at how to display the SHT30 measurements on our WEB server let's first try to give WEB control to the user LED on the ESP32 CPU board. We want to create a WEB page allowing to switch this LED on and off.

We will start off with the light bulb example from the introduction in this tutorial, replacing the light bulb with a blue LED (simulating the LED on the ESP32 CPU board.

dummyLED.png

Communication between the WEB client, on which javascript is running, and the WEB server on the ESP 32 can be accomplished with Asynchronous JavaScript And XML (AJAX). To understand this you will have to go through yet another tutorial: the AJAX tutorial.

Essentially you must create aXMLHttpRequest, which you send from the client to the server and to which the server will respond. You use the answer from the server to update the HTML page using javascript.

-- Uli Raich - 2020-04-28

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng dummyLED.png r1 manage 44.4 K 2020-06-04 - 09:03 UliRaich  
PNGpng helloWorldWEBServerV1.png r1 manage 65.8 K 2020-04-28 - 12:55 UliRaich  
PNGpng helloWorldWEBServerV1Log.png r2 r1 manage 74.5 K 2020-04-28 - 13:11 UliRaich  
Texttxt helloWorldWebServerV1.py.txt r1 manage 1.6 K 2020-04-28 - 13:14 UliRaich  
Texttxt helloWorldWebServerV2.py.txt r1 manage 0.7 K 2020-04-28 - 12:56 UliRaich  
PNGpng led-blue-off.png r1 manage 48.6 K 2020-06-04 - 09:05 UliRaich  
PNGpng led-blue-on.png r1 manage 58.4 K 2020-06-04 - 09:05 UliRaich  
GIFgif pic_bulboff.gif r1 manage 2.4 K 2020-06-04 - 09:05 UliRaich  
GIFgif pic_bulbon.gif r1 manage 2.5 K 2020-06-04 - 09:05 UliRaich  
PNGpng wifi_connect.png r1 manage 194.5 K 2020-04-28 - 08:02 UliRaich  
Texttxt wifi_connect_anonymous.py.txt r1 manage 0.4 K 2020-04-28 - 08:02 UliRaich  
Edit | Attach | Watch | Print version | History: r8 | r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r4 - 2020-06-04 - UliRaich
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback