Does anyone have a working TCP Server after the deep update?

i have tried to use the example server code and i always get the core to hard fault once i have connected. when i connect i have the core send me 9 “hello” and it only gets to the 2nd and dies. i have 2 cores i have tried this on and it doesn’t work on either.

// EXAMPLE USAGE

// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
// start listening for clients
server.begin();

// 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(Network.localIP());
Serial.println(Network.subnetMask());
Serial.println(Network.gatewayIP());
Serial.println(Network.SSID());
}

void loop()
{
if (client.connected()) {

server.write("Hello1");
server.write("Hello2");
server.write("Hello3");
server.write("Hello4");
server.write("Hello5");
server.write("Hello6");
server.write("Hello7");
server.write("Hello8");
server.write("Hello9");

// echo all available bytes back to the client
while (client.available()) {
server.write(client.read());
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
}

So I have the webduino HTTP server code running after deep update–no real problems.

The most recent firmware has changed the Network class to be part of the WiFi class, so all the Network. methods are not going to compile.

Is your problem running or compiling? The code above will not compile in the most recent web IDE or repos.

1 Like

Sorry I caught that part. I replaced the network commands with WiFi commands and it compiles fine. It then crashes on me.

This is my current code and it still does the same. (the tcp server example but with led notice to show connected [D7] & some server.writes to show that it can send data. )

Note: i commented out while(!Serial.available()) SPARK_WLAN_Loop(); as this wouldn’t even connect while this loop existed. (does this detect that i’m plugged in using usb power?)

Still only gets to the 2nd “hello” then stops sending and after 20sec will show hardfault.

// EXAMPLE USAGE

// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
  pinMode(D7,OUTPUT);   
  // start listening for clients
  server.begin();
  

  // 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(WiFi.localIP());
  Serial.println(WiFi.subnetMask());
  Serial.println(WiFi.gatewayIP());
  Serial.println(WiFi.SSID());
}

void loop()
{
  if (client.connected()) {
      digitalWrite(D7, HIGH);
      server.write("Hello1");
      server.write("Hello2");
      server.write("Hello3");
      server.write("Hello4");
      server.write("Hello5");
      server.write("Hello6");
      server.write("Hello7");
      server.write("Hello8");
      server.write("Hello9");
    // echo all available bytes back to the client
    while (client.available()) {
      server.write(client.read());
    }
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
    digitalWrite(D7, LOW);
  }
}

Hi @Bootes

I tried your code and it does behave strangely for me but not in the same way that you described. When I connected to the core, it sent Hello strings very fast and eventually crashed with a red LED SOS and one flash meaning hard fault.

I then added a delay(5000); at the bottom of loop() and then it sends this:

$ telnet 10.0.0.2 23
Trying 10.0.0.2...
Connected to 10.0.0.2.
Escape character is '^]'.
Hello1Hello2Hello3Hello4Hello5Hello6Hello7Hello8Hello9Hello1Hello2Hello3Hello4Hello5Hello6Hello7Hello8Hello9Hello1Hello2Hello3Hello4Hello5Hello6Hello7Hello8Hello9Hello1Hello2Hello3Hello4Hello5Hello6Hello7Hello8Hello9

I then stopped the telnet client and the core hard faulted again and rebooted.

I guess at this point I would need to know more about what you are trying to do to give better advice. Are you trying to send very quickly? I am not sure what else the server.write() calls are meant to do every time through loop.

1 Like

Adding a delay(5000); at the end doesn’t seem to help me. it’s just longer for the client to connect to the server(makes sense) but it still hard faults after “hello2”.
In a different attempt, I tried putting a delay(500); between each hello but it gets to hello2 and hard faults again.
I will be using my tcp server to monitor a couple of temp sensors and broadcast the results. I will not broadcast that often however I am surprised that I can get more than 12 characters before it faults.

OK, something is different in your setup–maybe it is your host side code. I was using telnet under Mac OS and never sent from host to core. Using this setup I was able to send the Hello strings from core to host using your code. I added the delay at the end of loop() because something was getting overrun without them but it is not clear what that was.

What are you using on the host side to receive this data? A TCP server you wrote yourself or something telnet?

I am currently running Putty as the telnet client. i will be using a crestron processor eventually but for testing i’m just using putty.

i also get a flashing cyan (lost contact with spark cloud) is there some ports i have to open on my router?

Here's the doc:

The only change you may need to make to your router is to open up outgoing port 5683, the default CoAP port the Spark Core uses to connect to the Spark Cloud.

But if you have ever had breathing cyan on this router, then that port is likely open.

Since this code echos everything sent to it, it will also echo any telnet negotiations, and so the effect might change depending on the client and it’s specific configuration. This is awfully crude code (but I agree it probably should not die in an assortment of ugly ways, but rather should be put out of it’s misery by better code :smile:)

1 Like

Thanks bko,
i have now connected it to my Crestron Processor and have slowed the communications enough that it doesn’t hard fault. i have been running some code that displays the date,time and 2 analog temp sensors every 10s for the last 3 hours and it appears to be working.

I did still have the issue with my crestron processor until i slowed the communications down.

Thanks again

I think this code is not the best way to handle this--you should probably read until client.available() is false or a time-out or certain number of times through the loop and then send what you have to send in server.write() command.

I know this code is in the official example but it is just a simple example and needs to be expanded.

Same problem over here:

It outputs only “Hello1Hello2” and then stops and reboots after 20 seconds. I’ve excluded the client read loop and I’m only outputting the “Hello’s” once. Any news?

The host is a standard linux machine

telnet 10.2.0.107
Trying 10.2.0.107...
Connected to 10.2.0.107.
Escape character is '^]'.
Hello1Hello2
TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
  pinMode(D7,OUTPUT);   
  // start listening for clients
  server.begin();
  
  // 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();

  delay(5000);

  Serial.println(WiFi.localIP());
  Serial.println(WiFi.subnetMask());
  Serial.println(WiFi.gatewayIP());
  Serial.println(WiFi.SSID());
}

void loop()
{
    int cnt;
    bool wasConnected=false;
    
  if (client.connected()) {
      if (!wasConnected) {
          digitalWrite(D7, HIGH);
          server.write("Hello1");
          server.write("Hello2");
          server.write("Hello3");
          server.write("Hello4");
          server.write("Hello5");
          server.write("Hello6");
          server.write("Hello7");
          server.write("Hello8");
          server.write("Hello9");
          wasConnected=true;
      }
    // echo all available bytes back to the client
    cnt=0;
    //while (cnt<100 && client.available()) {
    //  server.write(client.read());
    //  cnt++;
    //}
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
    digitalWrite(D7, LOW);
    wasConnected=false;
  }
}

Sorry for the huge delay. having been playing with my spark in a while.

I ended up using sprintf to create my complex strings and then print it. I don’t send back to back strings without a delay between strings. then used server.print (tempStr);

 http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm 

Let me know if you found out something more from your experience