I’m finding that when I connect to my Spark using putty telnet, I have to hit ENTER on my client before data sent to the server is shown as available. The code below uses ‘1’ to turn on the LED, ‘0’ to turn it off, and ‘q’ to close the session. I verified with Wireshark that the ‘1’ is sent immediately on keypress, and the ENTER (technically CR/LF) sent in the next telnet packet, but the ‘1’ doesn’t make it to my app until the ENTER. I’d like to get the ‘1’ immediately, without requiring the ENTER.
// init vars
TCPServer server = TCPServer(23);
TCPClient client;
void setup()
{
// set led as output
pinMode(D7, OUTPUT);
// start server
server.begin();
}
void loop()
{
char c;
if (client.connected())
{
// loop until data stops
while (client.available())
{
// handle keypress events
switch (c = (char)client.read())
{
case '1':
// 1 turns on
digitalWrite(D7, HIGH);
break;
case '0':
// 0 turns off
digitalWrite(D7, LOW);
break;
case 'q':
// q quits
client.stop();
// Reconnect to the cloud when telnet is disconnected
Spark.connect();
break;
}
server.println(c,HEX);
}
}
else
{
// wait for new connections
client = server.available();
// When my telnet client connects, disconnect from the cloud
if ( client.connected() ) {
delay(500);
Spark.disconnect();
}
}
}