首页 > 解决方案 > 如何从 HTM 获取输入整数?- Arduino / NodeMCU ESP32

问题描述

嗨,我目前正在做伺服 NodeMCU 控制。我的计划是创建一个输入框,我可以在其中输入,然后我输入的任何数字都将用作使用 Webserver 或 194.168.4.1 等的伺服角度。(例如:我输入 90,伺服角度将是 90) 我不知道如何得到它的问题。这是代码:

#include<Servo.h>
Servo ServoPin;
int angle = 0;
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define ServoPin 34;   // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
const char *ssid = "XXXXXX";
const char *password = "XXXXXX";

WiFiServer server(80);


void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");
  pinMode(ServoPin,OUTPUT);
  // You can remove the password parameter if you want the AP to be open.
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<!DOCTYPE html>");
            client.print("<html>");
            client.print("<body>");
            client.print("<p>Change the text of the text field, and then click the button below.</p>"); //
            client.print("INPUT NUMBER: <input type='number' id='servo'>"); //in this area, I WILL TYPE number 0-255.
            client.print("<button type='button' '>Go</button>");
            ServoPin.attach(number);  //the area where i will assign the servo to the angle i type.
          
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

所以你可以在 client.print"INPUT NUMBER.... 中看到,这就是我的代码将设计一个输入框的地方。问题是它在一个 " " 或双引号内。我想知道什么我应该做些什么来获取“数字”输入,然后在 ServoPin.attach(number) 上使用它;

我是 HTML 的初学者(实际上为零),因为我在这里还没有知识。此代码主要取自互联网 w3school 网站,然后我将其修改为伺服控制。所以我真的希望有人能告诉我怎么做......

板子:Node32 实际板子:NodeMCU ESP 32

标签: htmlwebarduinoesp32nodemcu

解决方案


推荐阅读