mercredi 21 juillet 2021

How do I connect a variable from an HTML form to the parameter for that sets the resistors' value?

Here's what I tried that didn't work:

  1. Naming the variables and putting the variable inside the [] when setting the parameter

ie [r1]

  1. Having a variable that acts like a middleman

get = r1

bus.write_i2c_block_data(0x2c, 0x01, [get])

Code ATM:

#!/usr/bin/python3
import cgitb
import cgi 
import smbus
import threading
cgitb.enable()
print("Content-type:text/html\r\n\r\n");
print("")
# Create instance of FieldStorage 
form = cgi.FieldStorage() 
# Get data from fields
r1 = form.getvalue('r1')
r2 = form.getvalue('r2')
htmlFormat ="""
<html>
<form name="search" action= "file.py" method = "POST">
<label> resistance0x01: </label>
<input type = "number" name = "r1"><br /><br />
<label> resistance0x03: </label>
<input type = "number" name = "r2"><br /><br />
<input type = "submit" value = "Submit" />
</form>
<p>Resistance0x01: {r1} Ohms</p>
<p>Resistance0x03: {r2} Ohms</p>
</html>"""
print(htmlFormat.format(**locals()))
bus = smbus.SMBus(1)
data1 = bus.read_byte_data(0x2C, 0x01)
data2 = bus.read_byte_data(0x2C, 0x03)
bus.write_i2c_block_data(0x2c, 0x01, [r1])
bus.write_i2c_block_data(0x2c, 0x03, [r2])
#0x01
res_1 = (data1 / 256.0 ) * 1.0
ohm_1 = res_1 * 1000 # ohm format
#0x03
res_2 = (data2 / 256.0 ) * 1.0
ohm_2 = res_2 * 1000 # ohm format 
htmlFormat = """ <html>
        <head>
                <title>setResistanceValue</title>
        </head>
        <body>
                <p>Resistance of [0x01]: {ohm_1} Ohms</p>
                <p>Resistance of [0x03]: {ohm_2} Ohms</p>
        </body>
</html>
"""
print(htmlFormat.format(**locals()))

The error message it gives




Aucun commentaire:

Enregistrer un commentaire