Im a newbie on this arduino thing and I would like to build a curtain remotecontrolled from a local webpage with 2 buttons... a up an a down...
I got the connection via my phone the the local page with the buttons. :D
My problem on this one is that the buttons' if statement (command == on); doesnt work or in other words the client.println(command); get the "on" command and write it on the page... but it dont do the function in the if statement (the button)...
Can anybody help me...? it should be simple..:D
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
int LEDPIN = 3; // your LED PIN
YunServer server;
String readString;
void setup() {
// Start our connection
Serial.begin(9600);
pinMode(LEDPIN,OUTPUT);
digitalWrite(LEDPIN,HIGH); // turn on Led while connecting
Bridge.begin();
// Show a fancy flash pattern once connected
digitalWrite(LEDPIN,LOW);
delay(150);
digitalWrite(LEDPIN,HIGH);
delay(150);
digitalWrite(LEDPIN,LOW);
delay(150);
digitalWrite(LEDPIN,HIGH);
delay(150);
digitalWrite(LEDPIN,LOW);
delay(150);
// Disable for some connections:
// Start listening for connections
// server.listenOnLocalhost();
server.begin();
}
/* ------------------------------------------------*/
/* --------------- End WIFI connection ------------*/
/* ------------------------------------------------*/
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
delay(50);
}
void process(YunClient client) {
String command = client.readStringUntil('\\');
client.println("Status: 200");
client.println("Content-type: text/html");
client.println();
client.println("<B><Center><br><br><br>");
client.println("<a href='/arduino/on\\'>ON</a><br><br><br>");
client.println("<a href='/arduino/off\\'>OFF</a><br><br><br>");
client.print("Command: ");
client.println(command);
client.println("</B></Center>");
/* ------------------------------------------------*/
/* ------------------- Buttons --------------------*/
/* ------------------------------------------------*/
if (command == "on") {
digitalWrite(LEDPIN,HIGH);
digitalWrite(13,HIGH);
}
if (command == "off") {
digitalWrite(LEDPIN,LOW);
digitalWrite(13,LOW);
}
}
-
3Perhaps your command isn't exactly "on" or "off" - could it have trailing characters? Try displaying its length in your debugging message, or only comparing only the beginning with startsWith. Also, comparing your code to a reference example, you seem to have replaced a single forward slash with an escaped backslash in the readStringUntil() so perhaps you are mistakenly reading the whole thing.Chris Stratton– Chris Stratton2014年07月16日 19:51:41 +00:00Commented Jul 16, 2014 at 19:51
-
Is it possible you have a case sensitivity issue ("ON" != "on")?TDHofstetter– TDHofstetter2014年08月22日 02:06:55 +00:00Commented Aug 22, 2014 at 2:06
1 Answer 1
Pretty sure your problem is coming from the fact that you chose to use the backslash as your terminator character. The backslash requires special handling when encoded as a URL. You can observe this by Googling for "\" and and the results page you'll see your query in the address bar assigned to the "q" variable: q=%5C my entire URL looked like this, you may have something a bit different: https://www.google.com/?gws_rd=ssl#q=%5C
Ok - so if you want a quick fix, stop using the \ to terminate your commands, since backslashes are often used to help sort out special characters (like "TAB" as \t) it's a a really bad choice, but an honest mistake.
If you change your code to generate links terminated with dashes you can get around the issue
client.println("<a href='/arduino/on-'>ON</a><br><br><br>");
client.println("<a href='/arduino/off-'>OFF</a><br><br><br>");
and of course change your readStringUntil invocation too
client.readStringUntil('-');
Edit: You might want to use the "" if the hostname part of your URL contains a space: http://my-web-server.com/on
And one more alternative, seek the "%" instead since that's going to be used to encode the backslash. In that case you just need to change the readStringUntil()