A WEB Server on the ESP32

WiFi

The WeMos D1 mini ESP32 CPU board offers WiFi access implementing a TCP/IP and full 802.11 b/g/n Wi-Fi MAC protocol (see the ESP32 data sheet) and the ESP32 port of MicroPython offers a network module with the necessary access functions.

Before looking into the WEB server we must first make sure we have Internet access through WiFi.The following screen dump shows an interactive MicroPython session where WiFi access is granted:

wifi_connect.png

Of course you must add your correct WiFi ssid and password. In the above case we also scan the WiFi network to see all access points available. If you use your WeMos D1 CPU at different locations with different WiFi networks you can use this function to figure out to which network you have access and connect to the right network automatically.

In the above screen dump wlan.ifconfig() shows you that you can connect to your ESP32 on the IP address 192.168.0.46.

Here is a small piece of Python code accomplishing network access when the ssid is known:

https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/wifi_connect_anonymous.py.txt

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

Here is the Python code of the WEB server:

https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/helloWorldWebServerV1.py.txt

and here the log output from the first version of the WEB server

helloWorldWEBServerV1Log.png

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

helloWorldWEBServerV1.png

-- Uli Raich - 2020-04-28

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
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 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 | r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r2 - 2020-04-28 - 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