html to variable

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

html to variable

Post by bellad » Thu Sep 12, 2019 7:39 am

hello,
i would collect 2variable in html to micropython , but how to do ?

Code: Select all

<p align="center" style="margin-bottom: 0cm; line-height: 100%"><B>Demande exceptionnelle</B></p>
  <div>
    <p align="center" style="margin-bottom: 0cm; line-height: 100%">
    <input type="text" id="name1" name="name1" required
       minlength="2" maxlength="2" size="3">
	   <label for="Choi1"> Degrés  |  </label>
	<input type="text" id="name2" name="name2" required
       minlength="1" maxlength="2" size="2"> 
       <label for="Choi2">Heure(s) |</label>
      <p align="center" style="margin-bottom: 0cm; line-height: 100%"><input type="submit" value="Enregistrer">	   
   </div>
thank you
sorry for my english , i use google translate
Attachments
dem.jpg
dem.jpg (8.63 KiB) Viewed 11021 times

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: html to variable

Post by ThomasChr » Fri Sep 13, 2019 9:30 am

One way would be to have the html elements in a <form> element with a submit-Tag which has the address of the pyboard in it. Whenever you submit the form the values of the variables will be submitted with it, either in the url (method=get) or in the body (method=post) of the request.

A little bit more complicated would be a Javascript xmlhttprequest. This can be totally asynchronous and the data format can be more changed by you, for example JSON.

Thomas

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: html to variable

Post by bellad » Fri Sep 13, 2019 12:16 pm

thank you Thomas

do you have an example?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: html to variable

Post by jimmo » Fri Sep 13, 2019 12:19 pm

Take a look at "microdot" which is a web server for MicroPython. It includes an example that has a web page that lets you control a GPIO pin.

https://github.com/miguelgrinberg/microdot

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: html to variable

Post by bellad » Fri Sep 13, 2019 12:36 pm

thank's , ok I study the example

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: html to variable

Post by bellad » Mon Nov 18, 2019 3:47 pm

hello,
sorry but is a mystery to me.

Code: Select all

<form action=”” method=”POST”>
      <p align="center" style="margin-bottom: 0cm; line-height: 100%">
    <input type="text" id="name1" name="name1" required
       minlength="2" maxlength="2" size="3" >
	   <label for="contactChoice1"> Degres_    </label>
	<input type="text" id="name2" name="name2" required
       minlength="1" maxlength="2" size="2" > 
       <label for="contactChoice1">Heure(s) </label>
      <p align="center" style="margin-bottom: 0cm; line-height: 100%"><button type="submit">Enregistrer</button><p/>	
   </form>	  

Code: Select all

 
 if request.method == 'POST':
    deg=request.form['name1']
    laps=request.form['name2']
AttributeError: 'str' object has no attribute 'method'
but i two request one for

Code: Select all

conn, addr = s.accept()
  #print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  #print('Content = %s' % request)
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  led2_on = request.find('/?led2=on')
  if led_on == 6:
    mode="Travail"
   # print('LED ON')
and one for

Code: Select all

try:
    import urequests as requests
except ImportError:
    import requests
import socket
import machine
I would like to recover the values name1 and name2 in variables deg and laps ,
can you help
thank you

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: html to variable

Post by jimmo » Mon Nov 18, 2019 9:03 pm

urequests is for sending HTTP requests, so that won't help here.

Did you take a look at microdot? It takes care of this for you. Let us know if we can help you get that set up.

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: html to variable

Post by bellad » Tue Nov 19, 2019 8:52 am

i see microdot , but i think that it's going to overload the ram on my esp12 , because my prog is already substantial

for 2 variable there is no other less cumbersome solution ?
thank

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: html to variable

Post by bellad » Wed Nov 20, 2019 9:44 am

I found a solution
i search in 'request 'variable and retrieve data

lalongo
Posts: 1
Joined: Wed Apr 29, 2020 12:31 pm

Re: html to variable

Post by lalongo » Wed Apr 29, 2020 12:34 pm

Hello Bellad,

I have the same issue than you but I cannot understand what do you mean by :
"i search in 'request 'variable and retrieve data"
Can you be more specific ? It would be really helpfull :D

Thanks

Post Reply