How can I send DeviceID to a tcp connection as one packet?

I would like to send the device Id to a tcp server and cannot seem to stop it splitting it into many packets fore each element of the string. What is the correct way to send this string variable as a single value to the client port please? I am after a way to include the dev_id in my data packet along with the other fields with sprintf . It sends correctly to the serial port.
The following code hopefully explains what I have been struggling with.
Thanks for any help.

indent preformatted text by 4 spaces
TCPClient client;
byte server[] = { 192, 168, 0, 195 }; 

char id[12];
char id2[100];
char* dev_id;

String String_ID = NULL;
String myIDStr = NULL;

void setup()

  while(!Serial.available()) SPARK_WLAN_Loop();

    Serial.println("connecting to host...");

    if (client.connect(server, 3053))
       String myIDStr = Spark.deviceID();
       Serial.println(myIDStr) ;  // prints correctly to the serial port
       client.print("Dev ,Packet nos ,Data, unit_Id, RSSI, Batt,  datetime");  //send one packet to tcp server

       client.print(myIDStr) ;   //sends 24+ single byte network packets 

       for (int i = 0 ; i < 24 ; i ++ ) 
              String_ID.concat( String(myIDStr[i]) );

        client.print(String_ID);   //sends 24+ single byte network packets 

void loop()

indent preformatted text by 4 spaces

Hi @floridact

This is unfortunately a known problem on both Spark and Arduino but the fix is pretty easy and I think the Spark firmware will be fixed soon. Here is what to do:

//was client.print(myIDStr) ;
client.write(myIDStr.c_str(), myIDStr.length());  //sends one packet
//was client.print(String_ID);
client.write(String_ID.c_str(), String_ID.length());  //sends another packet

By the way, the markup for code is:

  //code here

Preparing and passing a buffer might the solution you are aiming for. You can send a whole packet with TCPClient::write(buffer, size) instead of TCPClient::print(String) (defined by Print::print, doing single calls to TCPClient::write(uint8_t) for each character, creating single packets by calling TCPClient::write(uint8_t *, 1)).

In your specific case:

#define ID_LENGTH 24
TCPClient client;
byte server[] = {192, 168, 0, 195}; 
const char header[] = "Dev ,Packet nos ,Data, unit_Id, RSSI, Batt,  datetime";
void setup() {
  char buffer[ID_LENGTH];
  if (client.connect(server, 3053)) {
    client.write((uint8_t *)header, sizeof(header));
    Spark.deviceID().toCharArray(buffer, ID_LENGTH);
    client.write((uint8_t *)buffer, ID_LENGTH);

To make sure that its sent as two packets:

$ nc -l 3053
Dev ,Packet nos ,Data, unit_Id, RSSI, Batt,  datetimec0ffeec0ffeec0ffeec0ffee
$ sudo tcpdump -X dst port 3053

06:09:23.846000 IP > station.dsom-server: Flags [P.], seq 0:54, ack 1, win 1460, length 54
0x0000: 0023 1222 c57b 0800 2868 0929 0800 4500 .#.".{…(h.)…E.
0x0010: 005e 0022 0000 4006 9493 c0a8 b262 c0a8 .^."…@…b…
0x0020: b231 07be 0bed 3599 5026 fa7d 4904 5018 .1…5.P&.}I.P.
0x0030: 05b4 0bad 0000 4465 7620 2c50 6163 6b65 …Dev.,Packe
0x0040: 7420 6e6f 7320 2c44 6174 612c 2075 6e69 t.nos.,Data,.uni
0x0050: 745f 4964 2c20 5253 5349 2c20 4261 7474 t_Id,.RSSI,.Batt
0x0060: 2c20 2064 6174 6574 696d 6500 ,…datetime.

06:09:23.846731 IP > station.dsom-server: Flags [P.], seq 54:78, ack 1, win 1460, length 24
0x0000:  0023 1222 c57b 0800 2868 0929 0800 4500  .#.".{..(h.)..E.
0x0010:  0040 0023 0000 4006 94b0 c0a8 b262 c0a8  .@.#..@......b..
0x0020:  b231 07be 0bed 3599 505c fa7d 4904 5018  .1....5.P\.}I.P.
0x0030:  05b4 437c 0000 XXXX XXXX XXXX XXXX XXXX  ..C|..XXXmyXXXXX

juhuu :smile: hope that helped :wink:

-edit- ei, had an old version of the page open without @bko’s compact solution, so here we go with some additional info at least + less nice approach :angel: -/edit-

1 Like

Thanks a lot for those answers (so quick too!) , works fine now and I’m moving forward again :slight_smile: