0

在 Webserver 上,基本上,我想从用户那里获取一个值并在程序中处理它。我在 Thonny IDE 上使用带有 Raspberry Pi Pico 的 Micropython。TCP/IP 通信协议与 W5500 芯片一起使用。我使用 /get 格式来获取输入。但是,我无法达到搜索栏上 '' get?Value= '' 之后的相应值。你能帮助我吗 ?

import board
import busio
import digitalio
import time

import adafruit_requests as requests
from adafruit_wiznet5k.adafruit_wiznet5k import *
from adafruit_wsgi.wsgi_app import WSGIApp
import adafruit_wiznet5k.adafruit_wiznet5k_wsgiserver as server
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket

##SPI Communication Connections
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17
##reset
W5x00_RSTn = board.GP20

print("Wiznet5k HTTP Test (no DHCP)")

MY_MAC = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05) #My Own IP Credentials
IP_ADDRESS = (10, 0, 0, 197)
SUBNET_MASK = (255, 255, 254, 0)
GATEWAY_ADDRESS = (10, 0, 0, 254)
DNS_SERVER = (10, 0, 0, 1)

led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT

ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
ethernetRst.direction = digitalio.Direction.OUTPUT

# For Adafruit Ethernet FeatherWing
cs = digitalio.DigitalInOut(SPI0_CSn)

spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)

# Reset W5500 first
ethernetRst.value = False
time.sleep(1)
ethernetRst.value = True

# Initialize ethernet interface without DHCP
eth = WIZNET5K(spi_bus, cs, is_dhcp=False, mac=MY_MAC, debug=False)

# Set network configuration
eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)

print("My IP address is:", eth.pretty_ip(eth.ip_address))

# Initialize a requests object with a socket and ethernet interface
requests.set_socket(socket, eth)

# Here we create our application, registering the
# following functions to be called on specific HTTP GET requests routes
web_app = WSGIApp()

html_string = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Raspberry Pi Pico</title>
</head>
<body>
<div align="center">
<H1>Raspberry Pi Pico Web Server</H1>
  <form action="/get">
    IP ADRESS: <input type="text" name="Value">
    <input type="submit" value="Submit">
  </form><br>
</div>
</body>
</html>
'''
html_string = html_string.replace("$CHIPNAME",eth.chip)
html_string = html_string.replace("$IPADDRESS",eth.pretty_ip(eth.ip_address))

@web_app.route("/") # Problem is here, How to get entered input ?
def root(request):
    print("Value is Taken")
    return ("200 OK", [], [html_string])

server.set_interface(eth)
wsgiServer = server.WSGIServer(80, application=web_app)

print("Open this IP in your browser: ", eth.pretty_ip(eth.ip_address))
# Start the server
wsgiServer.start()

while True:
    # Our main loop where we have the server poll for incoming requests
    wsgiServer.update_poll()
    # Maintain DHCP lease
    eth.maintain_dhcp_lease()
4

1 回答 1

0

有关 pi pico、wiznet 芯片、micropython 和 http 客户端,请参考以下链接。

https://www.youtube.com/watch?v=6RJRbAHxu5Y&t=145s

https://github.com/SteveSEK/Raspberry-Pi-Pico-MicroPython-Ethernet/blob/master/ports/rp2/documents/example_urequest.md

于 2022-03-02T23:36:32.840 回答