[SOLVED] Sending multiple data packets over TCP connection

Hey everyone.

I’ll try to keep this concise.
My goal: Photon 1 (TCP Client) needs to send 4 different integer values to Photon 2 (TCP Server).
The integer values all have an uint_8t range (0-255).

Basically I need help programming 4 integers into something that can be send over TCP connection and then the server should be able to process this data as those 4 integers. So far my code is able to send 1 integer quite reliably at a speed of <70ms (I disabled cloud connection).

So, any advice on how to package these integers into data (I’m assuming an array of "char"s) and then extract them back into 4 integers after the server has read this? The biggest question mark for me is that since the range is 0-255 the integers can either have 1, 2 or 3 digits and this can/will vary over time, so how can the server interpret this data correctly?

If anyone can point me into the right direction I’d really appreciate that!!

ps: I’m not the most experienced programmer :smiley:

TCPClient does deal with binary data (sending and receiving) - no need to turn the values into decimal string representation of the number.

Try something like this

// buffer
uint8_t values[4];
//sending
client.write(values, 4);
// receiving
for(int i=0; client.available() && i<4; i++)
  values[i] = client.read();
1 Like

Can’t believe it’s that simple… Thank you so much, you’re a life/project-saver!

For people that want the code:

TCPServer:

TCPServer server = TCPServer(80);
TCPClient client;
uint8_t values[4];
uint32_t lastTime;
const char replymsg[60] = "TheInMsg and then a whole lot more characters than before";

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

  // Make sure your Serial Terminal app is closed before powering your device
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  while(!Serial.available()) Particle.process();

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

void out(const char *s) {server.write( (const uint8_t*)s, strlen(s) );  client.stop();}

void loop()
{
  if (client.connected()) {
    // echo all available bytes back to the client
    for(int i=0; client.available() && i<4; i++){
      values[i] = client.read();
      Serial.println(values[i]);
    }
    client.read();
    client.flush();
    lastTime = millis();
    while( millis()-lastTime < 5){}
    out(replymsg);
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
    Serial.println("Checking for connecctions");
  }
}

TCPClient:

TCPClient client;
byte server[] = {192,168,0,160}; 
uint8_t values[4];
char inmsg[512];
const char replymsg[60] = "TheInMsg and then a whole lot more characters than before";
bool complete;
uint32_t lastTime;
String myInStr;

void setup()
{
  // Make sure your Serial Terminal app is closed before powering your device
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  values[0] = 255;
  values[1] = 1;
  values[2] = 0;
  values[3] = 0;
}

void in(char *ptr, uint8_t timeout) {
  int pos = 0;
  unsigned long lastdata = millis();
  while ( client.available() || (millis()-lastdata < timeout)) {
    if (client.available()) {
      char c = client.read();
      lastdata = millis();
      ptr[pos] = c;
      pos++;
      //Serial.println("I received and read the response");
    }
  }
  client.read();
}

void loop(){
    
  complete = false;
  lastTime = millis();
  while ((!complete) &&  (millis() - lastTime < 100)) {
      if (client.connect( server, 80)) {
        if (client.connected()) {
          client.write(values, 4);
          Serial.println("Writing values");
          lastTime = millis();
          while ((!client.available()) && (millis() - lastTime < 500)) {
            Particle.process();
            //Serial.println("Waiting for a response");
          }//wait for response
          in(inmsg,10);//5-10 pure trial and error
          myInStr =inmsg;
          if (myInStr.indexOf(replymsg)  >= 0) {
            complete = true;
          }
        }
        client.stop();
        Serial.println("Stopping client");
      }
      client.stop();
      Serial.println("Stopping client 2");
  }
  delay(1);
}

This works incredibly well and even with the cloud enabled I get a ridiculous speed that’s definitely below 100 milliseconds. Really awesome, I’ll have this going overnight and see if it can make it through till morning to check reliability.

2 Likes