TCPClient HTTP POST on Electron is Intermitant

I’ve got gode running on my Photons that sets up a TCPClient, connects to a php script on my [hosted] server and manually submits an HTTP POST request with a JSON payload in the request body.

When I tried to run this same code on the Electron version of our product, the code connects [client.connect(targetServer, port) returns true] and occasionally the sever saves the JSON to it’s file and the Electron reports the response from the server. But most of the time the server logs a request to the php script, but no data is saved and the Electron reports 0 bytes returned from the server.

I added a bunch of delay()s in the the theory that TCP over the cellular connection might have more latency, to no avail.

So far haven’t been able to find a pattern to when it does and when it won’t upload the payload.

Any suggestions?

Could you post your code?

Full code is in the Test branches of the GitHub repositories linked above.

The electron version of the pushReport(Report newReport) function in publishSVPA.cpp is below.

int pushReport(Report newReport){
  String putJSON = getJSON(newReport);

  publishDebug(String("targetServer=" + targetServer));
  delay(1000);

  /*
  // Local Network debugging
  byte targetIP[] = {192, 168, 11, 36};
  targetServer = "ProlCat";
  port = 2129;
  publishDebug(String("targetIP=" +
    String(targetIP[0]) + "."+ String(targetIP[1])
    + "."+ String(targetIP[2])+ "."+ String(targetIP[3])
    ));
  */

  publishDebug(String("port=" + String(port)));

  delay(1000);

  if (client.connect(targetServer, port))
  //if (client.connect(targetIP, port))
  {
     // "PUT /api/newdeveloper/lights/3/state HTTP/1.1"
     //String putRequest = "PUT ";
     String pushRequest = "POST ";
      pushRequest.concat(targetResource);
      pushRequest.concat("  HTTP/1.1");
      pushRequest.concat("\r\n");
      pushRequest.concat("Host: ");
      pushRequest.concat(targetServer);
      pushRequest.concat("\r\n");
      pushRequest.concat("Connection: keep-alive");
      pushRequest.concat("\r\n");
      //client.println("Content-Type:   application/json;");
      pushRequest.concat("Content-Type:   text/plain; charset=UTF-8");
      pushRequest.concat("\r\n");
      pushRequest.concat("Content-Length:  ");
      pushRequest.concat(String(putJSON.length()));

    client.println(pushRequest);
    client.println();
    client.println(putJSON);
    client.println();
    client.println();

    publishDebug(String("pushRequest=" + pushRequest));
    delay(10000);

    publishDebug(String("client.available=" + String(client.available())));

    String pushResponse = "";
    char nextResponseChar;

    while (client.available())
      {
        nextResponseChar = client.read();
        pushResponse.concat(nextResponseChar);
        delay(15);
      }

    publishDebug(String("pushResponse=" + pushResponse));

    return 0;
  }
  else
  {
    publishDebug(String("putReportResult= Did not connect."));
    return -1;
  }
}

Added some debugging code to my php script and it looks like the JSON payload is getting cutoff after 1023 bytes with “A” appended as the 1024th byte.

"schemaVersion":"1","firmwareVersion":"8","deviceId":"2f004a000e5135343332363","nextUpdateTime":"1487476823","readings":[{"timeStamp":"-1708712930","range":"0","internalTemp":"0.000000","internalPressure":"0.000000","internalHumidity":"0.000000","soc":"0.000000","voltage":"0.000000","rssi":"0"},{"timeStamp":"0","range":"0","internalTemp":"0.000000","internalPressure":"0.000000","internalHumidity":"0.000000","soc":"0.000000","voltage":"0.000000","rssi":"0"},{"timeStamp":"0","range":"0","internalTemp":"0.000000","internalPressure":"0.000000","internalHumidity":"0.000000","soc":"0.000000","voltage":"0.000000","rssi":"0"},{"timeStamp":"0","range":"0","internalTemp":"0.000000","internalPressure":"0.000000","internalHumidity":"0.000000","soc":"0.000000","voltage":"0.000000","rssi":"0"},{"timeStamp":"0","range":"0","internalTemp":"0.000000","internalPressure":"0.000000","internalHumidity":"0.000000","soc":"0.000000","voltage":"0.000000","rssi":"0"},{"timeStamp":"0","range":"0","internalTemp":"0.000000","internalPreA

It sounds like your String objects are fragmenting. Strings require more RAM and are more complicated than normal char arrays, and are more likely to fragment. Using allocated C strings (char arrays) is safer.

Side note:
Is it possible to achieve this POST request using a Webhook? This would reduce data consumption on the Electron.

This is a bug, and it should be fixed in system firmware 0.7.0. You can’t send 1024 bytes on an Electron. 1023 or fewer is fine. I usually send 512.

3 Likes