Wifly with Phone App

Hi,

I will be very grateful if anyone could assist with the problems I’m having with my project setup.

With this project, I am connecting a phone (using a hotspot) to a Wifly and Arduino with the Wifly library (GitHub - bobbrez/WiFly: WiFly Shield). I can successfully connect the Wifly with the phone. Running on the phone is an app created using NS Basic App Studio. This is a permanent app on my phone.

Within my app program, I have below. This first line sends the text to the Wifly along with the rest of the connection data. The second line and below is to confirm to the app that the text was successfully received. The app has a URL.

BASIC code (App)

req = Ajax("http://192.168.43.56/text_to_send")

If req.status = 200 Then 'wifi successful
       MsgBox("", 0, "Wifi ok")
Else 'wifi failed
       MsgBox("", 0, req.status & " Wifi fail")
End If

Arduino code (Wifly)

////////////////////////////Wifly/////////////////////
#include "WiFly.h"
#include "Credentials.h"
#include "SPI.h"

#define maxLength 255
String receivedText = String(maxLength);
int what =0;

WiFlyServer server(80);

#include <Wire.h>

///////////////////////////////////////////////////////////////////
void setup() 
{
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial1.begin(9600);
  
  //////////////////////////  Wifly   ///////////////////////////////

  server.begin();
  Serial2.begin(9600);
  
  Serial.println("Searching for device...");
  
  WiFly.setUart(&Serial2);
  //WiFly.begin();
  WiFly.begin();

  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Failed to find device");
    while (1)
    {
      // Hang on failure.
    }
  }

  Serial.println("Successfully connected to device");
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
}

void loop() 
{

 /////////////////////////////    Wifly   //////////////////////////////// 

 WiFlyClient client = server.available();
  if (client) 
  {
      // an http request ends with a blank line
      boolean current_line_is_blank = true;
      while (client.connected()) 
      {
          if (client.available()) 
          {
                char c = client.read();
                
                Serial.print(c);
            
                //////////////////////////
                if (receivedText.length() < maxLength)
                {
                  receivedText += c;
                }
                //////////////////////////
                // If first request upon connexion, the 3 first characters will be "GET"
                // If "GET" is caught, skip the request info
                       
                // Otherwise, if the request contains data,
                // the first characters will be "POST"
                // We then skip the request header and this "if" becomes our main function
                if( receivedText.equals("POST"))
                {
                  Serial.println("");
         
                  // Read the data package length
                  receivedText="";
                }
                /////////////////////////
                
                                                    
                // if we've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so we can send a reply
                if (c == '\n' && current_line_is_blank) 
                {                                                
                        //delay(2000);
                        // send a standard http response header
                        //client.println("HTTP/1.1 200 OK");


                        DO SOMETHING HERE??

                          
                        break;
                }
                
                if (c == '\n') 
                {
                      // we're starting a new line
                     current_line_is_blank = true;
                } 
                else if (c != '\r') {
                     // we've gotten a character on the current line
                      current_line_is_blank = false;
                }
          }
      }
      // give the web browser time to receive the data    
      delay(500);
   
      client.stop();
    }
 } //end loop(

The problem I am having relates to the status of the communication. The data is successfully sent to the Wifly but I’m unable to send the correct response from the Arduino code to the app to confirm a successful data transfer. What code from the Arduino should be sent so the status of 200 is received in my app if transfer is successful?

I would greatly appreciate any help here.

String receivedText = String(maxLength);

I'm really curious as to why the initial value for receivedTest is "255". How is this a meaningful value?

What code from the Arduino should be sent so the status of 200 is received in my app if transfer is successful?

Uncomment this:

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

Uncomment this:
//client.println(“HTTP/1.1 200 OK”);

I originally had this uncommented but had no joy.

I tried others with no success also such as

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

I’m not sure what else can be used. Can a reference to the app URL be used?

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

I still get a reply req = 0 within my app.

I'm still not having much joy with this.

Regardless of what I try to send to the App (as seen from previous posts), I still get a req == 0 for all commands sent.

Does anybody have any ideas/ suggestions of the correct way to do this?

Thanks