Page 1 of 2

html to variable

Posted: Thu Sep 12, 2019 7:39 am
by bellad
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

Re: html to variable

Posted: Fri Sep 13, 2019 9:30 am
by ThomasChr
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

Re: html to variable

Posted: Fri Sep 13, 2019 12:16 pm
by bellad
thank you Thomas

do you have an example?

Re: html to variable

Posted: Fri Sep 13, 2019 12:19 pm
by jimmo
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

Re: html to variable

Posted: Fri Sep 13, 2019 12:36 pm
by bellad
thank's , ok I study the example

Re: html to variable

Posted: Mon Nov 18, 2019 3:47 pm
by bellad
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

Re: html to variable

Posted: Mon Nov 18, 2019 9:03 pm
by jimmo
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.

Re: html to variable

Posted: Tue Nov 19, 2019 8:52 am
by bellad
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

Re: html to variable

Posted: Wed Nov 20, 2019 9:44 am
by bellad
I found a solution
i search in 'request 'variable and retrieve data

Re: html to variable

Posted: Wed Apr 29, 2020 12:34 pm
by lalongo
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