Client-Server Socket communication

Hi everyone,

I am using Arduino RP2040 Connect for collecting IMU data and sending ti through WiFi Communication protocol.
I am actually using a client-server socket where this Arduino serves as first one (client) and Python serves as latter one (sever).

I am able to correctly connect Arduino to my Access Point, and to the server too. The communication works fine as long as Arduino stops generating data.
I see this bug by printing the data buffer which I want to send. In Serial Monitor I see data as long as a certain point they disappear, and obviously Python is not able to collect data anymore.

Here the most significant part of the code:


/* --------------------------------- */
/* --- Include Arduino libraries --- */
/* --------------------------------- */

/*************************************************/
/*** --------------- Variables --------------- ***/ 
/*************************************************/

unsigned long currentTime = 0, previousTime = 0, globalTime = 0;
int16_t deltaTime = 0;
int16_t nPacket = 0;
int16_t dataBuffer[BUFFER_LENGHT];
char c;
bool flag;

IMUsense      LSM6DSOX_sensor;        // IMU reading class

// Wi-Fi definitions 

WiFiClient client;                    // Initialize the WiFi client library
String readString;                    // String for reiceving data
String end_payload = "ENDPAYLOAD";    // Payload MSG

/* ---------- SSID and PWD ----------- */
char ssid[] = "xxxxxxxxxxx";     // the name of your network
char pass[] = "xxxxxxxxxxx";

/* -------------- WiFi Parameters--------------- */
byte mac[] = {0x08, 0x3A, 0xF2, 0xB1, 0x9D, 0xE0};    // MAC Address Arduino
IPAddress ip(192, 168, 43, 98);                       // IP Address local network (Client)
IPAddress server(192,168,0,100);                      //access point

void Sensors_Reading(void) {
 

}

void setup() {

// ---------- LED ----------
  digitalWrite(LED_WIFI, LOW);    // Blue LED - wireless connection
  digitalWrite(LED_ON, HIGH);     // Green LED - power on

// ---------- Serial COM startup ----------
  Serial.begin(921600);           // Initialize data rate at 921.600 bps
#if defined(COM_SERIAL_ENABLE)
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  Serial.println("Start!");
  delay(5);
#endif // COM_SERIAL_ENABLE

// ----------  IMU initialization ----------
  Serial.println("IMU initialization...");
  if (!IMU.begin()) {
    Serial.println("Failed!");
    while (1);
  } else {
    Serial.print("Accelerometer sample rate = ");
    Serial.print(IMU.accelerationSampleRate());
    Serial.println(" Hz");

    Serial.print("Gyroscope sample rate = ");
    Serial.print(IMU.gyroscopeSampleRate());
    Serial.println(" Hz");

    Serial.println("IMU ready");
  }
// ----------  WiFi initialization ----------  
  #if defined(COM_WIFI_ENABLE)
    // Start WiFi
    WiFi.begin(ssid,pass);
    Serial.println("Connected to WiFi...");
    
    // give WiFi 3 seconds to initialize:
    Serial.println("connecting...");
    delay(3000);
    
    if (client.connect(server,port)) {
    // Print result
      Serial.println("Connection Done");
    }
    else {
      Serial.println("Connection Failed");
    }
  #endif
  Serial.println("Starting acquisition...");        // Exit from setup, start "loop()"

  delay(100);                                       // Wait a bit, then start!

  previousTime = millis();                          // Take current time
}


void loop() {
  // if there are incoming bytes available 
  // from the server, read them and store them:
  if (client.available()) {
      c = client.read();
    }


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting. . .");
    client.stop();
    // do nothing:
    while(true);
  }
  
// ----------  Sensors Acquisition ----------
      if ( (millis()-previousTime) > SAMPLING_TIME-1 ) {
		
        Sensors_Reading();
			
		if (flag) {
		  client.write((byte*) dataBuffer, sizeof(dataBuffer));     		// Data Buffer sending
	  Serial.println(dataBuffer[0]);
	  Serial.println(dataBuffer[1]);
	  Serial.println(dataBuffer[2]);
	  Serial.println(dataBuffer[3]);
	  Serial.println(dataBuffer[4]);
	  Serial.println(dataBuffer[5]);
	  Serial.println(dataBuffer[6]);
	  Serial.println(dataBuffer[7]);
	  Serial.println(dataBuffer[8]);
	  Serial.println(dataBuffer[9]);
	  Serial.println(dataBuffer[10]);
	  Serial.println(dataBuffer[11]);
	  Serial.println(dataBuffer[12]);
		}
      }
}

Has anyone any idea how to solve it?

Thanks.

reconnect the client, if it disconnects. now you stop

@Juraj thanks for replying!
I do not think client is disconnetting, even because in this piece of code I check every times the connection.

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting. . .");
    client.stop();
    // do nothing:
    while(true);
  }

What do you think?

reset previousTime

In sensors_Reading() (which I did not report just to let code more readable) the first operations I do is:

// ---------- Set reading time ---------- 
  currentTime = millis();
  deltaTime = (float)(currentTime - previousTime);
  previousTime = currentTime;

So, I actually reset previousTime. Indeed, the communication starts fine and works for a while.

Apparently, the WiFi gets slowly the whole code running. If don't enable WiFi the code is perfectly able to generating data and print it in Serial Monitor.On the other hand, if I enable WiFi the Madgwick filter stops working and produces just NaN.
Consequently, enabling sending data and streaming for a while the code stops generating data.
So 2 bottlenecks:

  • Madgwick does not works anymore by enabling WiFi
  • Sending data over WiFi

Does make any sense this? Any ideas?
Thanks.