1

I am making an Arduino ethernet and serial communication program. The point of my program is to "SendKeys" the received message from Arduino that received the message from HTTP request query, like if I type "10.0.0.45/?hello world" (10.0.0.45 is my Arduino's IP) it should retrieve the "hello world" message and send it via serial.

Everything works fine, but when I am trying to send escaped characters, such as "\n", it will not work, it will type the same thing. But, I can use the replace function in my Arduino code, like this: query.replace("{ENTER}","\n"); Then it will work, but I want to make it work even without the replace function.

I am not good at explaining, so you can ask for details.

Here's my code:

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

File file;
String clientGetUrl;
String query;
String readString;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetServer server(80);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  SD.begin(4);
  Ethernet.begin(mac);
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  server.begin();
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
  pinMode(2, OUTPUT);
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }
        if (c == '\n' && currentLineIsBlank) {
          clientGetUrl = readString.substring(readString.indexOf("/"), readString.lastIndexOf(" HTTP"));
          if (clientGetUrl.indexOf("?") > 0) {
            query = clientGetUrl.substring(clientGetUrl.indexOf("?") + 1, clientGetUrl.length());
          } else {
            query = "";
          }
          query.replace("%20", " ");
          query.replace("{ENTER}", "\n");
          query.replace("{BP}", "\b");
          query.replace("{ESC}", "\x1B");
          query.replace("{SHIFT}", "+");
          query.replace("{CTRL}", "^");
          query.replace("{ALT}", "%");
          Serial.print(query);

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();

          file = SD.open("baduino/sendkeys.htm");
          if (file) {
            while (file.available()) {
              client.write(file.read());
            }
          }
          file.close();
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    readString = "";
    client.stop();
  }
}

And again, if i go to my Arduino's page, "http://10.0.0.45/?hello \n", it will retrieve the hello message and send it to my PC via serial, and then my PC receives the message and makes a "SendKeys" with my message. But the problem is instead of making the new line at the end of the message, it types "hello \n".

11
  • In HTTP there are no \ escapes. That is a C compiler thing. For HTTP you have to use %nn where nn is the hex code for the character you want. It's down to you to program your code to interpret those properly. Also space is not space, it's + or %20. ?hello world\n should actually be ?hello+world%0A – Majenko Jan 24 at 13:33
  • Also query parameters should have a name. x.x.x.x/?text=hello+world%0A – Majenko Jan 24 at 13:35
  • The serial monitor shows me the same thing as the HTTP request... So i should make my code to change the %nn to a backslash?? – KimPlayz4LK Jan 24 at 13:35
  • You need to look for a % then parse what comes after it. HTTP escape codes w3schools.com/tags/ref_urlencode.ASP – Majenko Jan 24 at 13:37
  • 2