Api.instapush.im host not connecting with TCPClient [408 timeout]

As the topic title says, I’m trying to do a basic request to the host api.instapush.im on port 80 (no ssl). I’ve succesfully connected to google.com (and other hosts) but this one is not working. What’s the best way to intercept/troubleshoot the packets the client is sending/receiving when trying to set up a TCP connection? Also, have others also had any difficulties with “some” hosts not connecting?

Update, I’m getting hard faults (red light flashing 1 time)

code:

TCPClient client;
byte server[] = { 162, 242, 228, 178 }; // api.instapush.im

int unsuccessfullConnects = 0;
int successfullConnects = 0;
int connected = 0;

char statusOfCore[100];
void clientConnect(void);

int disconnectTCPClient(String command)
{
    client.stop();
    sprintf(statusOfCore, "Disconnected client");
}
void setup()
{
  sprintf(statusOfCore, "Setting up");
    
  Spark.function("disconnect", disconnectTCPClient);
  Spark.variable("status",statusOfCore,STRING);
  Spark.variable("connects",&successfullConnects,INT);
  Spark.variable("disconnects",&unsuccessfullConnects,INT);

}

void clientConnect(void)
{
      if (client.connect(server, 80))
  {
    sprintf(statusOfCore, "Connected");
    successfullConnects++;
    Spark.variable("status",statusOfCore,STRING);
   
    
  }
  else
  {
      sprintf(statusOfCore, "Connection failed");
      unsuccessfullConnects++;
      client.stop();
    
  }
  
}

void loop()
{
 if(!connected)
 {
 clientConnect();
 delay(10000);
 }
}

Update #2
The same code connects sometimes now, and when it does, it gives me an 408 http response from the webserver. It seems like the HTTP header is not sent fast enough? I’ve changed the code so that the first thing it does after connect is send the lines for the http header, but that doesn’t work…

Update #3:
I’ve managed to focus the problem on the client.println() functions. Connection is stable, only when adding println functions, I get hard faults and an occasional 408 timeout response.

    client.println("POST /v1/post HTTP/1.1");
    client.println("Host: api.instapush.im");
    client.println("x-instapush-appid:xxxx");
    client.println("x-instapush-appsecret:xxx");
    client.println("Content-Type: application/json");
    client.println("Content-Length: 56");
    client.println();

this is working, but when I add this line:

the sparkCore crashes and gives an hard fault!

Apologies, this is above my paygrade, but have you tried searching “JSON” in the forum (magnifying glass symbol), it should pop up a few good suggestions.

Are you flashing your code using the web IDE?

I will look for JSON in the forum, but I think that’s not the issue.
I’m flashing with the web IDE, and the thing is, sometimes it crashes, sometimes not. It’s very random!

I am pretty sure there is code that runs in the background when using the webIDE. You might also want to look into flashing over USB with dfu-util and/or with the Spark command line interface(CLI) (windows reference).

You do need to make sure you flush any bytes coming back from the webserver–otherwise they will build up and when buffer overflow, you can get a hard fault. People have also found that adding a small delay like delay(20); after sending the final newline can help with stability for reasons that are not fully understood.

2 Likes

I’ve added the delay (and the flushing was indeed necessary) and it suddenly worked fine!
I think the delay part needs to be added to the startup guide, because this was very unnecessary troubleshooting for me (and maybe for a lot of people too!). I’m glad it is working though! Thanks bko!

2 Likes

Thanks for troubleshooting this guys, I added a bug for this to the firmware repo here: https://github.com/spark/core-firmware/issues/244

Thanks!
David