Android to Arduino Uno+wifi 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 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 I have connected the Arduino to the internet using the Arduino WifiShield 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:

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
  // start the server:
  // you're connected now, so print out the status:

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 =;
         // 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");
         if (c == '\n') {
           // you're starting a new line
           newLine = true;
           line = "";
         else if (c != '\r') {
           // you've gotten a character on the current line
           newLine = false;
           line += c; 
      // give the web browser time to receive the data
     // close the connection:

I am basing this code off of something I found online: 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 in the link above

tl;dr, I need help accepting strings using the Arduino Wifi Shield.

Reading strings from serial/ethernet is pretty standard. Read this for suggestions:

[quote author=Nick Gammon link=topic=157611.msg1180567#msg1180567 date=1364778030] Reading strings from serial/ethernet is pretty standard. Read this for suggestions:


That link seems to be more geared towards Serial communication. I am relatively new to Arduino and I am having trouble relating serial and ethernet/wifi. Can you be more specific about the handling of the incoming strings?

Conceptually it’s virtually the same. In the link I gave these lines:

  if (Serial.available () > 0) 
    char inByte = ();

… could be replaced by:

  if (client.available () > 0) 
     char inByte = ();

The rest would be much the same. You collect the bytes into a buffer until a newline, and then handle the entire line.

The HTTP data comes in two parts:

Header stuff
<blank line>

So a blank line therefore tells you that the next line will be what was sent.

Some simple server code that captures the incomming characters into the String “readString”, then extractes the desired info from readString.

//zoomkat 4-05-12
//web LED code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ') 
//address will look like when submited
//for use with W5100 based ethernet shields
//turns pin 5 on/off

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino server ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router gateway
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //arduino server port

String readString; 


void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);

  //enable serial data print 
  Serial.println("servertest1"); // so I can keep track of what is loaded

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c =;

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          Serial.print(c); //print what server receives to serial monitor

        //if HTTP request has ended
        if (c == '\n') {

          Serial.println(readString); //see the captured string

          //now output HTML data header

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

          client.println("<TITLE>Arduino GET test page</TITLE>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION=\"\" method=get >");

          client.println("Pin 5 \"on\" or \"off\": <INPUT TYPE=TEXT NAME=\"LED\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">

          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Change Pin 5!\">");




          //stopping client

          if(readString.indexOf("on") >0)//checks for on
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          if(readString.indexOf("off") >0)//checks for off
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          //clearing string for next read


is there any chance someone can explain the need for:

client.println("HTTP/1.1 200 OK");

Take a look at the HTTP protocol:

First thing the server replies with is the line in question, which acknowledges the connection and states the version of HTTP in use.

Then there are “header” lines in the format:

Header-name: Value

For example, date, server type, content type.

Then there is a blank line.

Then the HTML code is sent.

Example from Wikipedia:

HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/ (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Content-Type: text/html; charset=UTF-8
Content-Length: 131
Connection: close

  <title>An Example Page</title>
  Hello World, this is a very simple HTML document.

I just noticed something peculiar. When I ping the Arduino's IP address on my home network, I get that the IP I pinged does not exist. even though the link light is on and the server is set up with the above code. Does any one know why that happens? Is it my wireless or something I am not doing?

Also do you think it is neccassary to send back the http reply since this is not a web server? From what I gathered from the http wikipedia page, http is a protocol that deals with web calls, not LAN calls.

Ping uses the ICMP protocol which the board (chip) may not implement.

But yes, you could use a simple protocol like telnet (effectively just send and receive data).