My main goal is to read several parameters from an HTTP request to my arduino.
I am able to read just one piece of data (eg: 192.168.1.177?data12=1). However, when I try to read more than a variable (eg: 192.168.1.177?data12=1&key=123456) I have problems.
The HTTP request to capture is of the following type:
192.168.1.177?data12=1&key=123456
Where data12=1 is a command and key=123456 is a key that my program should check.
My guess is that the problem is with the strcmp somehow, because I have tried debugging the problem usin serial.write and serial.print and the key that I get from the request appears to be the same one as the char* from the program.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
const int msPersiana = 13700;
const int tubo1 = 5;
const int tubo2 = 6;
const int subirPersiana = 7;
const int bajarPersiana = 8;
const char* pwd_key = "&key=123456";
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(tubo1, OUTPUT);
pinMode(tubo2, OUTPUT);
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
Serial.println("new client");
bool currentLineIsBlank = true;
String cadena = "";
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c);
if (cadena.length()<50)
{
cadena.concat(c);
// Buscar campo data
int dataPosition = cadena.indexOf("data");
int keyPosition = cadena.indexOf("&key");
String command = cadena.substring(dataPosition, keyPosition);
String key = cadena.substring(keyPosition);
//TESTS
//Serial.println(command);
//Serial.println(key);
//Serial.println();
//Serial.write(key.c_str());
//Serial.println();
//Serial.write(pwd_key);
//Serial.println();
//if(strcmp("Hola", "Hola") != 0){
// Serial.println("Funciona");
//}
if (strcmp(key.c_str(), pwd_key) != 0){
break;
}
if (command == "data12=0" || command == "data1=0"){
digitalWrite(tubo1, LOW);
}
if (command == "data12=0" || command == "data2=0"){
digitalWrite(tubo2, LOW);
}
if (command == "data12=1" || command == "data1=1"){
digitalWrite(tubo1, HIGH);
}
if (command == "data12=1" || command == "data2=1"){
digitalWrite(tubo2, HIGH);
}
}
// Al recibir linea en blanco, servir página a cliente
if (c == '\n' && currentLineIsBlank)
{
client.println(F("<html>\n<head>\n<title>Smart Home</title>\n</head>\n"
"<body>\n<H1>Alex Smart Home</H1>\n</body>\n</html>"));
break; //Sale del Bucle while
}
if (c == '\n')
{
currentLineIsBlank = true;
}
else if (c != '\r')
{
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
So as it can be seen the "key" parameter is a kind of password to allow the client connecting to the server or being kicked out.
I have not been able to run the program correctly using the query:
192.168.1.177?data12=1&key=123456
Which should match passwords and execute the command data12=1.
Thank you.