Hi there!
My first post, so I take occasion to present myself: I am not a developer, neither an engineer, just an economist passionate about programming and, since a couple of weeks, microelectronics (thanks to arduino!), with a bunch of spare time to develop some project!
I am trying to send an unsigned long array from a "Client" microcontroller (in my case an ESP32 Dev Board, not important just for info, Wifi embedded) to a "Server" microcontroller (ARDUINO MEGA 2560 + Ethernet Shield 2) through TCP protocol.
I am able to send a single value, but I don't have idea how to send in a smart way the entire array.
Basically, on the client side, I came up with an unsigned long array made up from different sensors output.
Here is the portion of the code of interest from the client sketch (I omit library, setup, just to be coincise, all the code is working properly, I just need help with handling an entire long unsigned array with client.write()):
connecting stuff, sensor testing and so on, all working well
...
void loop() {
//.... sensor output all fine
//collecting outputs
unsigned long sensordataset[] ={Out1, Out2, Out3, Out4};
if (client.connected()){
client.write((uint8_t*) &sensordataset,sizeof(sensordataset));//print
delay(10);
}
delay(5000); // I have a timer with millis(), just putting delay to be more coincise in this example
}
on the server side instead:
void loop() {
//.... sensor output all fine
// array for collecting data from client:
unsigned long sensordataset[] ={Out1, Out2, Out3, Out4};
EthernetClient client = server.available(); // wait and assing client
int size;
unsigned long * clientdata = (unsigned long *)malloc(size);
if (client) {
Serial.println("new client");
if (size=client.available()){
size = client.read((uint8_t *)clientdata , size);
String str = String("") + (*clientdata );
Serial.println(str);
Out1=str.toInt(); // <----- here i find the first value but I would like the entire array
}
}
// do calculation & other stuff with sensordataset array elements
delay(10000);
So I give an unsigned long array to client.write, and I receive just the first value in client.read on the server side.
How can I solve the issue?
Thanks in advance!
PS: given my little experience, I decided to use TCP because I read it is reliable instead of UDP, that could not ensure all data are sent in the desidered order. Anyway what I need is just to receive on the ARDUINO MEGA with Ethernet shield the data collected from the ESP32WiFi (12 unsigned long every minute is the maximum rate, so not so much and complex data), so if you have other suggestion I will be more pleased to receive them! (of course not I2C wired connection
neither BLE)