Receiving TCP packets with WiFly

Hey, i cant seem to receive TCP commands when using a server.

I can receive TCP commands and print them to the serial window using the below code;

*

  SpiUartTerminal - tool to help troubleshoot problems with WiFly shield

  This code will initialise and test the SC16IS750 UART-SPI bridge then enable you
  to send commands to the WiFly module.

  Copyright (c) 2010 SparkFun Electronics. http://sparkfun.com LGPL 3.0

 */

#include <SPI.h>
#include <WiFly.h>

void setup() {

  Serial.begin(9600);
  
  
  Serial.println("Attempting to connect to SPI UART...");
  SpiSerial.begin();
  Serial.println("Connected to SPI UART.");
  Serial.println();
 
  delay(1000);
  Serial.write("$$");    
  
SpiSerial.begin();
 
  //exit CMD mode if not already done
  SpiSerial.println("");
  SpiSerial.println("exit");
  Serial.println("Exited CMD mode");
  delay(100);
 
  //set into CMD mode
  SpiSerial.print("$$");
  Serial.println("Into CMD mode");
  delay(100);

  //switch DHCP on
  SpiSerial.println("set ip dhcp 1");
  Serial.println("Set DHCP"); 
  delay(100);

  //set authorisation level
 // SpiSerial.println("set w a 4");
  //Serial.println("Set authorisation level");
  //delay(1000);

  //set passphrase
  SpiSerial.println("set wlan key 9d7cdbc6ce");
  Serial.println("Set WLAN key");
  delay(100);
 
  //set localport
  //SpiSerial.println("set i l 80");
  //Serial.println("Set port");
  delay(100);
 
  //disable *HELLO* default message on connect
  SpiSerial.println("set comm remote 0");
  Serial.println("Set comm remote 0");
  delay(100);

  //join wifi network
  SpiSerial.println("join BTHub3-HKP4");
  Serial.println("Join network");
  delay(200);

SpiSerial.println("set ip local 2102");
 Serial.println("Listening on port 2102");
 delay(200);
  
  SpiSerial.println("set ip protocol 2");
  Serial.println("SETTING TCP");
  delay(200);
  
  //exit CMD mode
  SpiSerial.println("exit");
  Serial.println("Exiting SpiSerial");
  delay(300);
  
  
  
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  
server.begin();


}

void loop() {
  // Terminal routine

  // Always display a response uninterrupted by typing
  // but note that this makes the terminal unresponsive
  // while a response is being received.
  while(SpiSerial.available() > 0) {
    Serial.write(SpiSerial.read());
    
}
  
  if(Serial.available()) { // Outgoing data
    SpiSerial.write(Serial.read());
  }

  }
}

Which is no use to me, because i cant use the send TCP packet for any if statements.
I have tried the following code, which worked once, then never worked again!

#include <SPI.h>
#include <WiFly.h>

// Enter a MAC address, IP address and Portnumber for your Server below.
// The IP address will be dependent on your local network:


// Initialize the Ethernet server library
// with the IP address and port you want to use 
WiFlyServer server(2102);

void setup()
{
 
   WiFly.begin();
  if (!WiFly.join("BTHub3-HKP4", "9d7cdbc6ce")) {
    while (1) {
      // Hang on failure.
    }
  }
  server.begin();
  Serial.println("Server started");//log
}

void loop()
{
  // listen for incoming clients
  WiFlyClient client = server.available();
  if (client) {
    Serial.print("1");
    String clientMsg ="";
    while (client.connected()) {
      
      if (client.available()) {
        Serial.print("3");
        char c = client.read();
        Serial.print(c);
        clientMsg+=c;//store the recieved chracters in a string
        //if the character is an "end of line" the whole message is recieved
        if (c == '\n') { 
         Serial.println("Message from Client:"+clientMsg);//print it to the serial
         // if (clientMsg == "hi"){
           //Serial.print("command received");
         //}
          client.println("You said:"+clientMsg);//modify the string and send it back
          clientMsg="";
        } 
      }
    }
    // give the Client time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

the code gets to while (client.connected()) and no further. I have connected because i get a solid green light upon TCP connection via comms operator.

any help?

Ive ensured all firewalls are open

thanks

I know the WiFly receives the command because the red light lights up for a split second.

I know this is probably a really hard question to answer, with a million and one possibilities as to what it could be.

Just to help me with debugging, when i connect to a home network over spiserial commands, are these overwritten by the uploaded code commands to connect? For example, the listening port for TCP and protocol used.
I can ensured these are saved on a config before i connect, and ive used the following youtube video to get a response.

Now, yesterday i was able to connect to the device with comms operator. Today ive done exactly the same as i did yesterday, followed the youtube video from scratch and i couldnt connect to the device using comms operator.
I tried on several different computers, put the Wifly in the routers DMZ and opening port etc.
The Wifly connects to the wireless network, but for some reason now it wont connect to a tcp client. I havent changed the code on my arduino AT ALL since, so this is why im wandering if commands sent via serial have an impact.

Can anyone give me any tips for TCP connectivity? thanks

I think ive broken my WiFly :frowning: its connects, but i cant access it over the net. The only thing i can think of that ive done is connected a speaker to my device. Maybe the 100ohm resistor wasnt enough.
It completely unresponsive over all protocols :frowning:

Maybe you haven't bricked it. I would try something simple first. Replace your loop() with something like this.

void loop()
{
  // listen for incoming clients
  WiFlyClient client = server.available();
  if (client) {
    Serial.println("client");
    while (client.connected()) {
      while (client.available()) {
        // get the character
        char c = client.read();
        // echo to the serial monitor
        Serial.print(c);
        //if the character is an "end of line" the whole message is received
        if (c == '\n') { 
          Serial.println("Got it");
          client.println("Got it");
          // close the connection
          client.stop();
        } 
      }
    }
  }
}

I don't have the WiFly library, so I couldn't check it. All it does is echo the request to the serial monitor, and when it receives a \n it sends "Got it" to the client and closes the connection.

Does the serial monitor show what you think you sent? I wouldn't try anything more until you get it to work.

Thanks, i have tried all the examples. The one you've written there is very similar to the example given for the WiFly server.
I have also set it to UDP mode and listened for the responses on my computer and have heard nothing.
TCP also doesnt connect anymore.

The serial doesnt display what ive sent, and the red light doesnt blink like it used to when i try to connect to the server by browser. (It receives nothing)

I will try this code in the morning just to see.

If anyone has some suggestion please let me know.
Otherwise i have a £70 piece of junk :confused:

Thank you for your reply.
I have purchased a new WiFly module, and used your code above.
I can connect to the device by TCP connection and when i send a command, a red light iluminates for a few milliseconds to indicate that it has recieved a response.

However, i don't receive any serial prints or client prints.

Any ideas?

I have tried a simple

while(SpiSerial.available() > 0) {
    Serial.write(SpiSerial.read());
  }
  
  if(Serial.available()) { // Outgoing data
    SpiSerial.write(Serial.read());
  }

loop, and the serial prints what i send over TCP, but only using this code.

Thanks