Hey Everyone. Got a TCPClient issue. Here’s my code:
#include "application.h"
TCPClient client;
byte server[] = { 192, 168, 0, 8 };
char buff[50];
int row = 0;
void setup()
{
// Make sure your Serial Terminal app is closed before powering your Core
Serial.begin(9600);
// Now open your Serial Terminal, and hit any key to continue!
while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println("Connecting to local server...");
if (client.connect(server, 6000))
{
Serial.println("Connected. Sending welcome packet");
sprintf(buff,"R:%d", row++);
client.println(buff);
}
else
{
Serial.println("Connection failed");
}
}
void loop()
{
if(client)
{
Serial.println("New Client");
while(client.connected())
{
int clientAvail = client.available();
if(clientAvail > 0)
{
Serial.print(clientAvail, DEC);
char c = client.read();
Serial.println(c);
}
if(row < 10)
{
sprintf(buff,"R:%d", row++);
client.println(buff);
}
else
{
client.print("close");
}
}
Serial.println("Disconnecting from server.");
client.stop();
for(;;) SPARK_WLAN_Loop();
}
}
The server listens for a connection, accepts it, and then waits for data. Once it gets data is responds with a “client wants row XXX” where X is the number the SparkCore sends. Somestimes this works great. Othertimes the core gets “hung up” on a client.read(). It’s always when Client.available() returns 1 and usually happens the first time the client attempts to read data from the server. Any ideas? Given the current inconsistency TCPClient is effectively useless to me but I know others are using without problems so it must be my code. Help?
Thanks!