Difference: WEBServerPicoweb (2 vs. 3)

Revision 32020-06-04 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="InternetAccess"

A WEB Server on the ESP32

Deleted:
<
<

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.

Line: 27 to 11
 
  • 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
Changed:
<
<
Here is the Python code of the WEB server:
>
>
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:

 
Changed:
<
<
https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/helloWorldWebServerV1.py.txt
>
>
request=conn.recv(1024)
print("Content %s" % str(request))
request=str(request) response=html conn.send(response) conn.close()
 
Changed:
<
<
and here the log output from the first version of the WEB server
>
>
html is the http string to be sent.
 
Changed:
<
<
helloWorldWEBServerV1Log.png
>
>
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

Added:
>
>

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
Line: 51 to 70
 
META FILEATTACHMENT attachment="helloWorldWEBServerV1.png" attr="" comment="" date="1588078557" name="helloWorldWEBServerV1.png" path="helloWorldWEBServerV1.png" size="67353" user="UliRaich" version="1"
META FILEATTACHMENT attachment="helloWorldWebServerV1.py.txt" attr="" comment="" date="1588079679" name="helloWorldWebServerV1.py.txt" path="helloWorldWebServerV1.py.txt" size="1652" user="UliRaich" version="1"
META FILEATTACHMENT attachment="helloWorldWebServerV2.py.txt" attr="" comment="" date="1588078598" name="helloWorldWebServerV2.py.txt" path="helloWorldWebServerV2.py.txt" size="736" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="dummyLED.png" attr="" comment="" date="1591261411" name="dummyLED.png" path="dummyLED.png" size="45510" user="UliRaich" version="1"
META FILEATTACHMENT attachment="led-blue-off.png" attr="" comment="" date="1591261513" name="led-blue-off.png" path="led-blue-off.png" size="49728" user="UliRaich" version="1"
META FILEATTACHMENT attachment="led-blue-on.png" attr="" comment="" date="1591261513" name="led-blue-on.png" path="led-blue-on.png" size="59783" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pic_bulboff.gif" attr="" comment="" date="1591261513" name="pic_bulboff.gif" path="pic_bulboff.gif" size="2493" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pic_bulbon.gif" attr="" comment="" date="1591261513" name="pic_bulbon.gif" path="pic_bulbon.gif" size="2554" user="UliRaich" version="1"
 
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