Difference: WEBServerPicoweb (4 vs. 5)

Revision 52020-06-04 - UliRaich

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

A WEB Server on the ESP32

A "Hello World!" WEB server

Changed:
<
<
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.
>
>
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.

Implementing the Hello World! WEB server

  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
Line: 11 to 12
 
  • 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:
<
<
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.

>
>
Let's start simple and produce a WEB server that just sends "Hello World!" to the browser when called. You may add some HTML text if desired. In the first version we will write a TCP server creating a stream socket and binding to port 80.

 
Changed:
<
<
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()
>
>
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.

Changed:
<
<
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))
>
>
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:
<
<
request=conn.recv(1024)
print("Content %s" % str(request))
request=str(request) response=html conn.send(response) conn.close()
>
>
request=conn.recv(1024)
print("Content %s" % str(request))
request=str(request)  response=html
conn.send(response)  conn.close()
 
Changed:
<
<
html is the http string to be sent.
>
>
html is the http string to be sent, e.g.

html = """ <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<title>Hello World </title>
<h1>The Hello World! HTML page</h1>
<p>Hello World!</p>

</body>
</html>
"""

  Please check https://docs.micropython.org/en/latest/library/usocket.html for more details.
Added:
>
>
The complete code is here:

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

 
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.

Added:
>
>
https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/helloWorldWebServerV2.py.txt
 
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.

Changed:
<
<

>
>
https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/helloWorldWebServerV3.py.txt
  and finally a screen dump of the WEB pages as seen by the browser

helloWorldWEBServerV1.png

Added:
>
>

Adding Temperature and Humidity Measurements

From the Hello World Web server to a server that actually shows measurements is only a small step. In the following example we create a table on HTML into which we insert the measurements taken with the SHT30 (see SHT30NopI2CTemperatureAndHumiditySensor) This is done with templates using the utemplate module in MicroPython. Have a look at the picoweb examples to see how this can be done.

https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/sht30.tar.gz

The tar archive contains:

  • sht30Server.py: The WEB server, based on picoweb, that must be run on the ESP32.
  • templates/sensor.tpl: The template file that must be stored in the templates directory of the ESP32
  • setup.sh: a shell script that creates the directories needed on the ESP32 (if they do not exists already) and uploads the template file into its correct place
  • timeStamp.py: a test program creating a time stamp from the current time (not needed here)

sht30WebSite.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?

Changed:
<
<
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:
>
>
Such types of actions can be accomplished with javascript, 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

Changed:
<
<
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.
>
>
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. The icons for the LED are attached to this page:
 
Changed:
<
<
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.
>
>
led-blue-off-128.png and led-blue-on-128.png

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

  dummyLED.png
Added:
>
>
Whenever you click the "Turn on the light" button the icon switches to led-blue-on-128.png showing a lighted blue LED while the other button shows the LED turned off. Just select the dummyLED.html in the tar archive with the browser and try.

https://iotworkshop.africa/pub/IoT_Course_English/WEBServerPicoweb/ledControl.tar.gz

 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.

Line: 64 to 93
 
<--/commentPlugin-->
Deleted:
<
<
META FILEATTACHMENT attachment="wifi_connect_anonymous.py.txt" attr="" comment="" date="1588060975" name="wifi_connect_anonymous.py.txt" path="wifi_connect_anonymous.py.txt" size="390" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="wifi_connect.png" attr="" comment="" date="1588060975" name="wifi_connect.png" path="wifi_connect.png" size="199166" user="UliRaich" version="1"
META FILEATTACHMENT attachment="helloWorldWEBServerV1Log.png" attr="" comment="" date="1588079485" name="helloWorldWEBServerV1Log.png" path="helloWorldWEBServerV1Log.png" size="76274" user="UliRaich" version="2"
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"
META FILEATTACHMENT attachment="dummyLED.png" attr="" comment="" date="1591261411" name="dummyLED.png" path="dummyLED.png" size="45510" user="UliRaich" version="1"
Deleted:
<
<
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"
Added:
>
>
META FILEATTACHMENT attachment="led-blue-off-128.png" attr="" comment="" date="1591284651" name="led-blue-off-128.png" path="led-blue-off-128.png" size="18552" user="UliRaich" version="1"
META FILEATTACHMENT attachment="led-blue-on-128.png" attr="" comment="" date="1591284651" name="led-blue-on-128.png" path="led-blue-on-128.png" size="24353" user="UliRaich" version="1"
META FILEATTACHMENT attachment="helloWorldWebServerV3.py.txt" attr="" comment="" date="1591284768" name="helloWorldWebServerV3.py.txt" path="helloWorldWebServerV3.py.txt" size="789" user="UliRaich" version="1"
META FILEATTACHMENT attachment="sht30.tar.gz" attr="" comment="" date="1591285702" name="sht30.tar.gz" path="sht30.tar.gz" size="1872" user="UliRaich" version="1"
META FILEATTACHMENT attachment="sht30WebSite.png" attr="" comment="" date="1591287148" name="sht30WebSite.png" path="sht30WebSite.png" size="15849" user="UliRaich" version="1"
META FILEATTACHMENT attachment="ledControl.tar.gz" attr="" comment="" date="1591289001" name="ledControl.tar.gz" path="ledControl.tar.gz" size="160245" user="UliRaich" version="2"
 
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