TCP communication optimisation

Hi, I am using Arduino Mega with wiznet W5100 module for network based communication.
in following code I am sending tcp based data to server, since packet consist some null char in-between the stream, so I am unable to use string and string concatenation.
my rest of of program is really very big, which is used to read multiple sensors so code optimisation is very critical for me.
kindly give your suggestions to optimize below codes.

EthernetClient client;
IPAddress newserverIP = Network.myremoteIP();

byte my_array1[] = {0x0D, 0x0F};
uint8_t my_array2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

String zbxh = “ZBXD\1";
String zbxd = "{"
              "\"request\":\"sender data\","
              "\"data\":["
              "{"
              "\"host\":\"TEST\","
              "\"key\":\"agent.ping\","
              "\"value\":\"1\""
              "}"
              "]"
              "}";

uint32_t  zbxlen1 = zbxh.length() + 1;
uint32_t  zbxlen2 = zbxd.length() + 1;
//uint32_t  zbxlen = 10;
char char_array1[zbxlen1];
char char_array2[zbxlen2];
zbxh.toCharArray(char_array1, zbxlen1);     
zbxd.toCharArray(char_array2, zbxlen2);     
      uint8_t tcp_len =  sizeof(char_array1) - 1 + sizeof(my_array1) + sizeof(my_array2) + sizeof(char_array2)-1;
      uint8_t tcp_a[tcp_len];
      memcpy(tcp_a, char_array1, sizeof(char_array1)-1 );
      memcpy(tcp_a + sizeof(char_array1) - 1 , my_array1, sizeof(my_array1) );
      memcpy(tcp_a + sizeof(char_array1) - 1 + sizeof(my_array1), my_array2, sizeof(my_array2));
      memcpy(tcp_a + sizeof(char_array1) - 1 + sizeof(my_array1) + sizeof(my_array2) ,char_array2,sizeof(char_array2)-1);

if (client.connected()) {      
      client.write(tcp_a, sizeof(tcp_a));
      client.flush();
      client.stop();
}

following is the output on receive end.

00000000: 5a42 5844 010d 0f00 0000 0000 007b 2272 ZBXD.........{"r

00000010: 6571 7565 7374 223a 2273 656e 6465 7220 equest":"sender

00000020: 6461 7461 222c 2264 6174 6122 3a5b 7b22 data","data":[{"

00000030: 686f 7374 223a 2254 4553 5422 2c22 6b65 host":"TEST","ke

00000040: 7922 3a22 6167 656e 742e 7069 6e67 222c y":"agent.ping",

00000050: 2276 616c 7565 223a 2231 227d 5d7d "value":"1"}]}

You need to say more about what you want to optimise for: Speed, Memory usage, ease of maintenance...

You are watsing both memory and performance by using Strings that are merely copied into Char arrays that are copied into a buffer. Drop the Strings they are generaly bad news especially when trying to optimise code.

Why use uint32_t for the lengths of strings you can easily see are relatively short.

Have you thought about using Progmem for storing your static strings?
Lots of options but we need to clearly understand what you are trying to achieve.

memory optimisation is first priority and speed is second.
I can definitely move my first string directly to memcpy, what about json sting, need help to optimise/store json string to some alternate method to finally transfer to memcpy buffer.

Get rid of the Strings zbxh and zbxd, just use const char arrays and put them in Progmem. You can then just copy them direct into your tcp buffer. It will save you twice the amount of memory you currenty use for them. Use the smallest datatype that will hold the size of the value you require, often for strings that will be a byte rather than a long.

You will get more help if you follow the guidelines and post the whole sktetch rather than just portions of it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.