TCPClient seems to kill my connection

I’m working on a small project that uses an instance of TCPClient to hit a scenario I’ve set up on PushingBox, but it seems like every time it makes that call, it loses its connection to the internet a few seconds later; i.e., starts flashing cyan, then a seemingly random pattern of red flashes, then attempting to reconnect to my wifi network. I’ll attach a link to a video clip of the pattern in case anyone is curious.

Red error LED pattern

Fortunately, the call seems to always go through, and the core does reconnect itself within a few seconds, so it’s not a huge disruption in how my project should work, but I am curious to know why this is happening. I’ve specifically written my code to make the call to PushingBox a maximum of once every ten seconds, and I’ve been able to verify through some serial debugging messages that it is only attempting a TCP connection once per button press.

My cores are all up to date with the recent deep update. Has anyone else had this problem?

Thanks.

My code in case it helps:


char DEVID1[] = "<MY DEV ID>";
char serverName[] = "api.pushingbox.com";
int waittime = 0;
char rang = 0;
char DEBUG = 1;
TCPClient client;
char lastConnected = false;

void setup() {
    pinMode(D1,INPUT_PULLUP);
    Serial.begin(115200);
}

void loop() {
    if(!(digitalRead(D1) || rang)){
        sendToPushingBox(DEVID1);
        rang = 1;
        waittime = millis();
    }
    if(rang && (millis() - waittime >=10000)){
        rang = 0;
    }
    if (!client.connected() && lastConnected) {
        if(DEBUG){Serial.println();}
        if(DEBUG){Serial.println("disconnecting.");}
        client.stop();
    }
    lastConnected = client.connected();
    
}



void sendToPushingBox(char devid[]){
  client.stop();
  if(DEBUG){Serial.println("connecting...");}

  if (client.connect(serverName, 80)) {
    if(DEBUG){Serial.println("connected");}

   if(DEBUG){Serial.println("sendind request");}
   client.print("GET /pushingbox?devid=");
   client.print(devid);
   client.println(" HTTP/1.1");
   client.print("Host: ");
   client.println(serverName);
   client.println("User-Agent: Arduino");
   client.println();
  } 
  else {
    if(DEBUG){Serial.println("connection failed");}
  }
}

Hi @twalsh

The red flashes are SOS N-flashes SOS as described here. Your video never loaded for me so I can say what you have but 1 flash for hard-fault or 8 flashes for out of memory seem likely.

You are leaving the connection open to pushing box for the 10 seconds since you don’t do client.stop() until you come round again, but you never pickup the bytes pushingbox is sending back to you. I would try waiting for a return value to come back via client.available() and then use client.flush().

1 Like

That appears to have done the trick, thanks!

1 Like