Marwan Alayli
Marwan Alayli

Reputation: 65

Android to Arduino Uno + Wi-Fi shield string communication

I am trying to make a wireless light control device (on/off/dimming) using an Arduino, an Android app, and a router.

I am setting the Arduino to a static IP 192.168.1.2 using the router. I am sending strings ("1"-off, "2"-decrease brightness, "3"-increase brightness, "4"-on) from the Android app to the IP address 192.168.1.2. I have connected the Arduino to the Internet using the Arduino Wi-Fi shield and set up the WifiServer using the following code:

char ssid[] = "NAME"; // Your network SSID (name)
char pass[] = "PASS"; // Your network password (use for WPA, or use as key for WEP)

int keyIndex = 0;     // Your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(23);

boolean alreadyConnected = false; // Whether or not the client was connected previously.

void setup() {
    // Start serial port:
    Serial.begin(9600);

    // Attempt to connect to Wi-Fi network:
    while ( status != WL_CONNECTED) {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);
        // Wait 10 seconds for connection:
        delay(10000);
    }
    // Start the server:
    server.begin();
    // You're connected now, so print out the status:
    printWifiStatus();
 }

The main problem I am having is how to accept and print out the strings from the Android device. The current code I have to do this is:

    // Listen for incoming clients
    WiFiClient client = server.available();
    if (client) {
        // An HTTP request ends with a blank line
        boolean newLine = true;
        String line = "";

        while (client.connected() && client.available()) {
            char c = client.read();
            Serial.print(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' && newLine) {
                // Send a standard HTTP response header
                //client.println("HTTP/1.1 200 OK");
                //client.println("Content-Type: text/html");
                //client.println();
            }
            if (c == '\n') {
                // You're starting a new line
                newLine = true;
                Serial.println(line);
                line = "";
            }
            else if (c != '\r') {
              // You've gotten a character on the current line
              newLine = false;
              line += c;
            }
        }
        Serial.println(line);

        // Give the web browser time to receive the data
        delay(1);

        // Close the connection:
        //client.stop();
    }
}

I am basing this code off of the blog post Android Arduino Switch with a TinyWebDB hack, but this code is for an Ethernet shield. The Android app was made using the MIT App Inventor, which is similar to the one found the blog post.

TLDR, how can I get the strings using the Arduino Wi-Fi shield?

Upvotes: 3

Views: 15349

Answers (1)

IanK.CO
IanK.CO

Reputation: 603

You can read the characters into a full string instead of reading each individual character into the serial monitor as in your example above.

In your example, this bit of code would read each character from the client TCP session and print it to the serial monitor, thus displaying the HTTP requests in the serial console.

    char c = client.read();
    Serial.print(c);

Try something like this instead. Declare a string named "readString" before your setup() function in the Arduino sketch like this:

    String readString;

    void setup() {
        //setup code
    }

    void loop() {

        // Try this when you're reading inside the while client.connected loop instead of the above:

        if (readString.length() < 100) {
            readString += c;
            Serial.print(c);
        }

Here is a working example of the loop():

void loop() {

    // Listen for incoming clients
    WiFiClient 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 (readString.length() < 100) {
                    readString += c;
                    Serial.print(c);
                }
                if (c == '\n' && currentLineIsBlank) {
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    client.println();
                    client.println("<!DOCTYPE HTML>");
                    //send the HTML stuff
                    client.println("<html><head><title>Admin Web</title><style type=\"text/css\">");
                    client.println("body { font-family: sans-serif }");
                    client.println("h1 { font-size: 14pt; }");
                    client.println("p  { font-size: 10pt; }");
                    client.println("a  { color: #2020FF; }");
                    client.println("</style>");
                    client.println("</head><body text=\"#A0A0A0\" bgcolor=\"#080808\">");
                    client.println("<h1>Arduino Control Panel</h1><br/>");
                    client.println("<form method=\"link\" action=\"/unlockdoor\"><input type=\"submit\" value=\"Unlock Door!\"></form>");
                    client.println("<br/>");
                    client.println("</body></html>");
                    break;
                }
                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 disonnected");
          if (readString.indexOf("/unlockdoor") > 0)
          {
             unlockdoor();
             Serial.println("Unlocked the door!");
          }
          readString = "";
  }

Upvotes: 1

Related Questions