Reputation: 25
I hope I can explain what I'm asking for in a good way.
I want to have an Android app to send a GET value to the Arduino via the Ethernet Shield. The URL that that would be called from the Android app would like http://192.168.1.199?comm=100
where the 100
could be any integer value.
Now with the WebServer example in Arduino IDE it does show up as a line which says:
GET /?comm=100 HTTP/1.1
but I can't find a decent way to just handle the value of "comm" inside the script. Could anyone please help me with this? I included the sketch but it's pretty much just the example because I don't even know where to start.
#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");
}
}
Upvotes: 2
Views: 7297
Reputation: 159
First, create global variables:
/* 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 */
Second, put the GET answer in the array, skip the header of the client. Last, get the value out of the array. Your void loop() will become:
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;
}
}
}
}
Upvotes: 2