WiFly Shield + Android Phone : I want them to understand eachother.

Hi everybody,

I finally succeded in making my Android Phone and my Arduino Uno + Wifly Shield communicate. So for now, I can connect my Samsung Galaxy S to my Arduino and then send the values of the accelerometer (that's an example, it can send values, that's the important thing).

Well now, what i'm trying to do is to read these values and make the Arduino react according to them. I mean, if the SmartPhone sends a value like "<1", then it means a servo has to be started, or whatever. In fact, I guess it's only parsing but I can't get it to work :-/ I tried to use the TextFinder Library but I got a problem : Here's the code which is provided as an example (adapted for WiFly, because it was for Ethernet Shield):

/*
 * WebServerParsing
 *
 * Respond to requests in the URL to change digital and analog output ports
 * show the number of ports changed and the value of the analog input pins.
 *
 * the following url turns digital pin 2 on and 3 off, analog output on pin 9 is set to 128, pin 11 to 255 
 * only onen parm http://192.168.1.177/2
 *
 * http://192.168.1.177/?pinD2=1&pinD3=0&pinA9=128&pinA11=255
 */
 
#if ARDUINO > 18
#include <SPI.h>         // needed for Arduino versions later than 0018 
#endif

#include <WiFly.h>
#include <TextFinder.h>

char ssid[] = "WIFI-ECOLE-WPA";

char passphrase[] = "WifiCampus";

Server server(80);

void setup()
{
  Serial.begin(9600);
  WiFly.begin();
  
  if (!WiFly.join(ssid, passphrase))
  {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }
  
  
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());  
    
  server.begin();
  Serial.println("ready");
}

void loop()
{
  Client client = server.available();
  if (client) {
    TextFinder  finder(client );  
    // an http request ends with a blank line
    //    boolean current_line_is_blank = true;
    while (client.connected()) {      
      if (client.available()) {          
        int digitalRequests = 0;  // counters to show the number of pin change requests
        int analogRequests = 0;
        if( finder.find("GET /") ) {            
          // find tokens starting with "pin" and stop on the first blank line  
          while(finder.findUntil("pin", "\n\r")){  
            char type = client.read(); // D or A
            int pin = finder.getValue();
            int val = finder.getValue();
            if( type == 'D') {                 
              Serial.print("Digital pin ");
              pinMode(pin, OUTPUT);
              digitalWrite(pin, val);
              digitalRequests++;
            }
            else if( type == 'A'){                  
              Serial.print("Analog pin ");  
              analogWrite(pin, val); 
              analogRequests++;
            }
            else {  
              Serial.print("Unexpected type "); 
              Serial.print(type);                
            }               
            Serial.print(pin);
            Serial.print("=");
            Serial.println(val);
          }                         
        }        
        Serial.println();
        
        // when we get here the findUntil has detected the blank line (a lf followed by cr)
        // so the http request has ended and we can send a reply
        // send a standard http response header
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();

        // output the number of pins handled by the request
        client.print(digitalRequests);
        client.print(" digital pin(s) written");
        client.println("
");
        client.print(analogRequests);
        client.print(" analog pin(s) written");
        client.println("
");
        client.println("
");

    
        // output the value of each analog input pin
        for (int i = 0; i < 6; i++) {
          client.print("analog input ");
          client.print(i);
          client.print(" is ");
          client.print(analogRead(i));
          client.println("
");
        }
        break;        
      } 
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

But here's what I get :

error: no matching function for call to 'TextFinder::TextFinder(Client&)'

They use it like that here though : http://www.arduino.cc/playground/Code/TextFinder

As a consequence, i'm wondering what's the problem. It does work with Ethernet and not with WiFly ? I'm a little bit lost...

Thank you very much and have a nice and sunny day

you have to compare what is returned by server.available() in both libraries (ethernet vs wifly) , there might be just a small diff (don't have wifly lib installed so I can't test)

Hi, thank your for your answer.

Here's the function of the WiFly Library (Server.cpp):

Client& Server::available() {
  /*
   */

  // TODO: Ensure no active non-server client connection.

  if (!WiFly.serverConnectionActive) {
    activeClient._port = 0;
  }

  // TODO: Ensure begin() has been called.

  // Return active server connection if present
  if (!activeClient) {
    // TODO: Handle this better
    if (WiFly.uart.available() >= strlen(TOKEN_MATCH_OPEN)) {
      if (WiFly.responseMatched(TOKEN_MATCH_OPEN)) {
	// The following values indicate that the connection was
	// created when acting as a server.

	// TODO: Work out why this alternate instantiation code doesn't work:
	//activeClient = Client((uint8_t*) NULL, _port);

	activeClient._port = _port;
	activeClient._domain = NULL;
	activeClient._ip = NULL;

	activeClient.connect();
	WiFly.serverConnectionActive = true;
      } else {
	// Ignore other feedback from the WiFly module.
	// TODO: Should we check we're not ditching a connect accidentally?
	//WiFly.skipRemainderOfResponse();
	WiFly.uart.flush();
      }
    }
  }

  return activeClient;
}

And here's the Ethernet one:

Client Server::available()
{
  accept();

  for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
    Client client(sock);
    if (EthernetClass::_server_port[sock] == _port &&
        (client.status() == SnSR::ESTABLISHED ||
         client.status() == SnSR::CLOSE_WAIT)) {
      if (client.available()) {
        // XXX: don't always pick the lowest numbered socket.
        return client;
      }
    }
  }

  return Client(MAX_SOCK_NUM);
}

I think my level in C++ is too low to really understand the difference between both of them..

Do you note the difference in the first line of the two functions ? &

That causes your error.

Oh OK... The WiFly Library was made to replace the Ethernet Library in case of use of Wifi but it seems the programmer changed some stuff... Well, I hope I can get it to work easily, i may ask him directly...

you might try
TextFinder finder(&client);
in your program.

Here is what I get when I do that :

WiFly_WebServer:29: error: no matching function for call to 'TextFinder::TextFinder(Client*)'
C:\Program Files (x86)\Arduino\libraries\TextFinder/TextFinder.h:56: note: candidates are: TextFinder::TextFinder(Stream&, int)
C:\Program Files (x86)\Arduino\libraries\TextFinder/TextFinder.h:36: note: TextFinder::TextFinder(const TextFinder&)

Sorry for the joke, but I can't resist... have you tried Rosetta Stone? LOL

Ahah ^^

In fact my question can be asked this way : how can I read wifi-transmitted data. It's very simple to read serial-transmitted data thanks to the Serial.read() method, but it seems more difficult for a wireless transmission doesn't it ?

It's very simple to read serial-transmitted data thanks to the Serial.read() method, but it seems more difficult for a wireless transmission doesn't it ?

Why? You use the Serial.read() function in one case and the Client.read() function in the other. Both return a single character...

I'm having the same problem you are trying to create an interactive web page. Did you ever get the problem resolved? You mentioned contacting the programmer. Were you successful and if so what was his response?