• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Micropython – So many questions

Q&A Forum › Category: ESP32 › Micropython – So many questions
0 Vote Up Vote Down
Mario Zandel asked 5 years ago

Hello I have been trying to work this out for months. I have seen so many tutorials my eyes are square.

A brief explanation of what i am trying to do:

I want to be able to connect a phone to an ESP32 acting as a stand alone access point ( which is further down the track, at this point I am happy to connect through a router). from there I want to be able to access the ESP32 through a web page. The web page will display 6 buttons which will return a value to the ESP32 of 1 to 6. The rest I can probably work out.

I can get the web page to load, but I cant seem to figure out how to get the CSS to load and run. Also i can`t figure out how to return the value I need to the ESP32

Here is the main.py file:

from netcreds import *
from nettools import *
import usocket as socket

wlan_connect(essid,essid_password)

def web_page():

  html = """
 <!DOCTYPE html>
<html>
<head>
    <title>AUTOAGILITY &copy</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="container">
        <div id="header">
            <h2>AUTOAGILITY</h2>   
        </div>
        <div id= "instruct">
            <p>PRESS A BUTTON TO SET THE HEIGHT</p>
        </div>
        <div class="radio-toolbar">
            <input type="radio" id="five" name="height" value="5">
            <label for="five">500</label>  </br>
            <input type="radio" id="four" name="height" value="4">
            <label for="four">400</label> </br>
            <input type="radio" id="three" name="height" value="3">
            <label for="three">300</label> </br>
            <input type="radio"id="two" name="height" value="2">
            <label for="two">200</label> </br>
            <input type="radio" id="one" name="height" value="1" checked>
            <label for="one">100</label>
            <p></p>
        </div>
        <div id="footer">
                Copyright &copy; 2019 Autoagility
        </div>
    </div>


</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)
  request = str(request)
  print('Content = %s' % request)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  conn.close()

And the CSS:

body {{
background-color: #aaa;
font-family: arial;
}}
container {{
background-color: white;
width: 950px;
margin-left: auto;
margin-right: auto;
}}
header {{
background-color: deepskyblue;
color: white;
font-size: 90px;
text-align: center;
}}
instruct {{
color: deepskyblue;
text-align: center;
font-size: 25px;
}}
.radio-toolbar input[type="radio"] {{
opacity: 0;
position: fixed;
width: 0;
}}
.radio-toolbar label {{
display: block;
background-color: cornflowerblue;
padding: 5px 5px;
text-align: center;
font-size: 127px;
border: 5px solid #444;
border-radius: 30px;
margin-bottom: 50px;
}}
.radio-toolbar input[type="radio"]:focus + label {{
border: 5px dashed darkgreen;
}}
.radio-toolbar input[type="radio"]:checked + label {{
background-color: #bfb;
border-color: #4c4;
}}
explain {{
color: blue;
font-size: 20px;
text-align: center;
margin: 0;
}}
footer {{
padding: 15px;
background-color: deepskyblue;
font-size: 30px;
color: white;
text-align: center;
}}

Can anybody tell me where i`m going wrong?

5 Answers
0 Vote Up Vote Down
Steph answered 5 years ago

Hello, Mario,

I’m not very familiar with Python, but I’ll still try to shed some light on what’s wrong with your code 🙂

In your main loop, whatever HTTP request reaches your web server, it always returns the HTML code of your main page (with your radio buttons).

Now, inside this HTML code, you have placed the following directive :

<link rel="stylesheet" type="text/css" href="style.css">

This will necessarily generate a new request from your browser, to obtain an additional resource, corresponding to your CSS stylesheet, whose URL is /style.css. Thus, the HTTP request headers will be in the form :

Request URL: http://[IP of your ESP]/style.css
Request Method: GET

However, your server always returns the same HTML code (the one on your main page), regardless of the HTTP request. It will therefore never return the code corresponding to your CSS stylesheet.
To solve this, you have two possibilities:
– either you keep this way of proceeding (which is not really ideal) and in this case, in your web_page() function, you have to replace :

<link rel="stylesheet" type="text/css" href="style.css">

by:

<style>
/* put your CSS codes here */
</style>

– either you modify the HTTP request processing code of your server so that it can process the 2 following HTTP requests:
The one concerning the main HTML page:

Request URL: http://[IP of your ESP]/
Request Method: GET

And the one concerning the CSS stylesheet :

Request URL: http://[IP of your ESP]/style.css
Request Method: GET

As I told you, I’m not very familiar with Python, so my code may have some errors, but basically here’s what you can do:

while True:
    conn, addr = s.accept()
    request = conn.recv(1024)
    request = str(request)

    isCSS = request.find('/style.css')

    if isCSS:
        response = css_stylesheet()
        mimetype = 'text/css'
    else:
        response = web_page()
        mimetype = 'text/html'
    
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: %s\n' % mimetype)
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    conn.close()

Assuming that you also define a css_stylesheet() function capable of providing the CSS code to be sent to the browser.

Hoping this answers your question…

0 Vote Up Vote Down
Mario Zandel answered 5 years ago

@Stephane Thank you SO MUCH 😀  I had tried to link the CSS in the HTML using <style> but it didnt work, so I gave up. When you said that was one way to do it, I went back to the CSS and found a couple of errors I hadn`t noticed. Fixed then and the page loads  as expected.  So now it is just figuring out how to return the value of the button press.

So this is the code which now loads the web page:

  1. from netcreds import *
  2. from nettools import *
  3. import usocket as socket
  4. wlan_connect(essid,essid_password)
  5. def web_page():
  6.    html = “”” <!DOCTYPE html>
  7.   <html>
  8.   <head>
  9.    <title>AUTOAGILITY &copy</title>
  10.    <style>
  11.    body {
  12.     background-color: #aaa;
  13.     font-family: arial;
  14. }
  15. #container {
  16.     background-color: white;
  17.     width: 950px;
  18.     margin-left: auto;
  19.     margin-right: auto;
  20. }
  21. #header {
  22.     background-color: deepskyblue;
  23.     color: white;
  24.     font-size: 90px;
  25.     text-align: center;
  26. }
  27. #instruct {
  28.     color: deepskyblue;
  29.     text-align: center;
  30.     font-size: 25px;
  31. }
  32. .radio-toolbar input[type=”radio”] {
  33.     opacity: 0;
  34.    position: fixed;
  35.    width: 0;
  36. }
  37. .radio-toolbar label {
  38.    display: block;
  39.    background-color: cornflowerblue;
  40.    padding: 5px 5px;
  41.    text-align: center;
  42.    font-size: 127px;
  43.    border: 5px solid #444;
  44.    border-radius: 30px;
  45.    margin-bottom: 50px;
  46. }
  47. .radio-toolbar input[type=”radio”]:focus + label {
  48.    border: 5px dashed darkgreen;
  49. }
  50. .radio-toolbar input[type=”radio”]:checked + label {
  51.    background-color: #bfb;
  52.    border-color: #4c4;
  53. }
  54. #explain {
  55.     color: blue;
  56.     font-size: 20px;
  57.     text-align: center;
  58.     margin: 0;
  59. }
  60. #footer {
  61.     padding: 15px;
  62.     background-color: deepskyblue;
  63.     font-size: 30px;
  64.     color: white;
  65.     text-align: center;
  66. }
  67.    </style>
  68.   </head>
  69.   <body>
  70.    <div id=”container”>
  71.        <div id=”header”>
  72.            <h2>AUTOAGILITY</h2>    
  73.        </div>
  74.        <div id= “instruct”>
  75.            <p>PRESS A BUTTON TO SET THE HEIGHT</p>
  76.        </div>
  77.        <div class=”radio-toolbar”>
  78.            <input type=”radio” id=”five” name=”height” value=”5″>
  79.            <label for=”five”>500</label>  </br>
  80.            <input type=”radio” id=”four” name=”height” value=”4″>
  81.            <label for=”four”>400</label> </br>
  82.            <input type=”radio” id=”three” name=”height” value=”3″>
  83.            <label for=”three”>300</label> </br>
  84.            <input type=”radio”id=”two” name=”height” value=”2″>
  85.            <label for=”two”>200</label> </br>
  86.            <input type=”radio” id=”one” name=”height” value=”1″ checked>
  87.            <label for=”one”>100</label>
  88.            <p></p>
  89.        </div>
  90.        <div id=”footer”>
  91.                Copyright &copy; 2019 Autoagility
  92.        </div>
  93.    </div>
  94.   </body>
  95.   </html>”””
  96.    return html
  97. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  98. s.bind((”, 80))
  99. s.listen(5)
  100. while True:
  101.   conn, addr = s.accept()
  102.   print(‘Got a connection from %s’ % str(addr))
  103.   request = conn.recv(1024)
  104.   print(‘Content = %s’ % str(request))
  105.   response = web_page()
  106.   conn.send(response)
  107.   conn.close()

 

0 Vote Up Vote Down
Steph answered 5 years ago

You’re welcome! My pleasure.
Don’t hesitate to come back and ask questions if you get stuck 😉

But open a specific topic for each question, I think Rui & Sara prefer it that way.

And when you have a lot of code to post, it would be better if you host it on GitHub Gist to make it easier for us to read it 😉

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Mario.

You need to create a form for your radio buttons and you need a “Submit” button so that the page actually makes a request to the server (send some information to the ESP32).

When you submit the form, depending on the selected options, you’ll make a request on a different URL. Then, accordingly to that, the ESP32 can perform different tasks.

Unfortunately, we don’t have any tutorial showing how radio buttons work. But a good resource is always the w3schools website: https://www.w3schools.com/tags/att_input_type_radio.asp

Click the try it yourself button and see their example: https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_radio

Depending on your application, you may also want to consider different input types: https://www.w3schools.com/html/html_form_input_types.asp

To know how to handle requests on different URLs, you can take a look at the microPython eBook project that starts at page 163.

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
Mario Zandel answered 5 years ago

Thanks Sarah. Time for some reading.
 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.