Hi there,
Maybe this section is more appropriate for my problem that is acqually posted at TCP/IP sending unsigned long array - Networking, Protocols, and Devices - Arduino Forum
Substantially I have problem in formatting an array that I have to send over TCP from a client ESP32 Dev Board to a server on ARDUINO MEGA 2560.
that's the sketch client side:
#include <WiFi.h>
///////////////////////////////////////////////////////////////////////////////////
// Wifi configuration
// Replace with your network credentials
const char* ssid = "netwroksssssfds";
const char* password = "passsssssss";
byte server[] = {xxx, xxx,xxx,xxx}; // web server IP (CORE)
WiFiClient client;
/********** Setup **********/
void setup(){
// Serial port for debugging purposes
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");
delay(1000);
}
// connecting to server
if (client.connect(server,1235)){
Serial.print("Connected to Server: ");
}
}
void loop(){
// Sensors Output
unsigned long Out0= 0;
unsigned long Out1= 1;
unsigned long Out2= 2;
unsigned long Out3= 3;
unsigned long Out4= 4;
unsigned long Out5= 5;
unsigned long Out6= 6;
unsigned long Out7= 7;
unsigned long Out8= 8;
unsigned long Out9= 9;
unsigned long Out10= 10;
unsigned long Out11= 11;
unsigned long Out12= 12;
unsigned long sensorboxdata[] ={Out0, Out1, Out2, Out3, Out4, Out5,Out6,Out7,Out8,Out9,Out10,Out11,Out12};
for (int i=0;i<=10;i++){
Serial.print("Sensore numero ");
Serial.println(i);
Serial.println(sensorboxdata[i]); // they are correct there
}
// Send data to Server
if (client.connected()){
Serial.println("sensorboxdatabyte to Server");
client.write((uint8_t*) &sensorboxdata,sizeof(sensorboxdata));
delay(10);
} else {
Serial.println("Connection to Server lost, trying to reconnect...");
if (client.connect(server,1234)){
Serial.println("Connected to Server");
}
else {
Serial.println("Unable to connect to Server!");
}
}
delay(3000);
}
and the serial debug gives the right values:
19:34:33.563 -> Sensore numero 0
19:34:33.563 -> 0
19:34:33.563 -> Sensore numero 1
19:34:33.609 -> 1
19:34:33.609 -> Sensore numero 2
19:34:33.609 -> 2
19:34:33.609 -> Sensore numero 3
19:34:33.643 -> 3
19:34:33.643 -> Sensore numero 4
19:34:33.690 -> 4
19:34:33.690 -> Sensore numero 5
19:34:33.690 -> 5
19:34:33.690 -> Sensore numero 6
19:34:33.728 -> 6
19:34:33.728 -> Sensore numero 7
19:34:33.728 -> 7
19:34:33.728 -> Sensore numero 8
19:34:33.764 -> 8
19:34:33.764 -> Sensore numero 9
19:34:33.764 -> 9
19:34:33.764 -> Sensore numero 10
19:34:33.811 -> 10
19:34:33.811 -> sensorboxdatabyte to Server
But when I read the array from the server side:
#include <SPI.h> // SPI: for screen & ethernet
#include <Ethernet.h>
byte mac[] = {xxxxxxxx};
byte ip[] = { xxxxxxxx };
EthernetServer server(1234);
//////////////////////// SETUP /////////////////////////
void setup() {
Serial.begin(9600);
Ethernet.begin(mac); // Start Ethernet Connection _ Ethernet.begin(mac, ip);
server.begin(); // start server
}
//////////////////////// LOOP /////////////////////////
void loop() {
unsigned long sensorboxdata[13];
// request data from sensor box
Serial.print("Request data from sensor box");
EthernetClient client = server.available(); // wait and assign client
int size;
unsigned long * sensordata = (unsigned long*)malloc(size);
if (client) {
Serial.println("client ready");
if (size=client.available()){
size = client.read((uint8_t *) sensordata, size);
for (int i=0;i<=12;i++){
sensorboxdata[i]=0;
String str = String("") + sensordata[i];
sensorboxdata[i]=str.toInt();
Serial.print("Output #");
Serial.println(i);
Serial.println(str);
Serial.println(String(sensorboxdata[i]));
}
}
}
else {
Serial.println("No client available");
}
delay(5000);
}
////////////////////////////////////// loop end
... I got complete no sense output, with Output0 and Output7 / 12 correct, and others are wrong:
19:35:58.390 -> Request data from sensor boxclient ready
19:35:58.424 -> Output #0
19:35:58.458 -> 0
19:35:58.458 -> 0
19:35:58.458 -> Output #1
19:35:58.458 -> 131072
19:35:58.458 -> 131072
19:35:58.458 -> Output #2
19:35:58.492 -> 808517632
19:35:58.492 -> 808517632
19:35:58.543 -> Output #3
19:35:58.543 -> 859191089
19:35:58.543 -> 859191089
19:35:58.543 -> Output #4
19:35:58.543 -> 655417
19:35:58.590 -> 655417
19:35:58.590 -> Output #5
19:35:58.590 -> 3617076
19:35:58.590 -> 3617076
19:35:58.590 -> Output #6
19:35:58.643 -> 942669878
19:35:58.643 -> 942669878
19:35:58.643 -> Output #7
19:35:58.643 -> 56
19:35:58.643 -> 56
19:35:58.643 -> Output #8
19:35:58.690 -> 8
19:35:58.690 -> 8
19:35:58.690 -> Output #9
19:35:58.690 -> 9
19:35:58.690 -> 9
19:35:58.690 -> Output #10
19:35:58.690 -> 10
19:35:58.744 -> 10
19:35:58.744 -> Output #11
19:35:58.744 -> 11
19:35:58.744 -> 11
19:35:58.744 -> Output #12
19:35:58.744 -> 12
19:35:58.744 -> 12
What is going on?
Thanks in advance!