从URL读取GET请求值到Arduino WebServer

问题描述 投票:2回答:1

我希望我能以一种好的方式解释我的要求。

我想要一个Android应用程序通过以太网盾向Arduino发送GET值。从Android应用程序调用的URL就像http://192.168.1.199?comm=100,其中100可以是任何整数值。

现在使用Arduino IDE中的WebServer示例,它确实显示为一行说:

GET /?comm=100 HTTP/1.1

但是我找不到一个合适的方法来处理脚本中“comm”的值。有人可以帮我这个吗?我包括了草图,但它几乎就是这个例子,因为我甚至不知道从哪里开始。

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 199);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("OK");

        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}
arduino get webserver
1个回答
1
投票

首先,创建全局变量:

/* maximum URL length (GET) is 2048 characters */
char getAnswer[2048]; /* array to store the get-answer, can be less if you know the max. size */
int getAnswerCount = 0; /* counter for getAnswer array */
char answerValue[10]; /* array to store the actual value, can be less if you know the max. length */
int answerValueCount = 0; /* counter for getAnswer array */

其次,将GET答案放在数组中,跳过客户端的标题。最后,从数组中获取值。你的void loop()将成为:

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

  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean firstLine = true; /* only the first line in the array */
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (firstLine) {
          getAnswer[getAnswerCount] = c;
          ++getAnswerCount;
        }

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("OK");

        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          firstLine = false;

        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");

    for (int i = 5; i < getAnswerCount; ++i) {
      char dataCharacter = getAnswer[i];
      if (dataCharacter == '=') {
        ++i;
        dataCharacter = getAnswer[i];
        while ( dataCharacter != ' ') { /* value ends with a space */          
          answerValue[answerValueCount] = dataCharacter;
          ++answerValueCount;
          ++i;
          dataCharacter = getAnswer[i];
        }
        answerValue[answerValueCount] = '\0';
        /* make an actual number of answerValue */
        char * pEnd;
        long int nameYourNumber = strtol(answerValue, &pEnd, 10);
        Serial.println(nameYourNumber + 1); /* + 1 to prove it is a number */
        break;
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.