我希望我能很好地解释我的要求。
我想有一个Android应用程序,通过以太网屏蔽将GET值发送到Arduino。从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");
}
}
答案 0 :(得分: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;
}
}
}
}