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"}]}