Eliminating sensor data loss while sending data via Wifi request? (MKR1010 Wifi)

Hi everybody,

I am new to programming and need some advice.
In my project I am using an Arduino MKR Wifi 1010 and a MPU 6050 sensor to send accelerometer data at about 80 Hz via Wifi to a server.
The data is temporarily stored in an array on the arduino memory and sent about every 3 seconds via HTTP request.

But here is the problem: during request time, the accelerometer data gets lost, which means i got gaps of about 150ms every 3 seconds. I want to detect steps and other biomedical parameters, so this makes my measurement inaccurate.

How can I adapt my code, so nothing gets lost?
Can I use the Fifo buffer on the MPU 6050 to store the missing data temporarily?
Are there other possible solutions?

Please helf me out, I am happy for any advice I can get.

#include <MPU6050_tockn.h>
#include <Wire.h>
#include <SPI.h>
#include <WiFiNINA.h>

//MPU
    MPU6050 mpu6050(Wire);
    long timer = 0;
    long interval = 11;
    int arr_length = 0;
    String data; 
    String dataold;

// server to connect to
    char server[] = "****************";
    char script[] = "/addarray.php";

//WIFI
    char ssid[] = "************";
    char pass[] = "************";
    int status = WL_IDLE_STATUS;
    WiFiClient client;

void printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    // print your board's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);
    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}

void setup() {

    Serial.begin(115200);
    //WIFI
    // check for the WiFi module:
        if (WiFi.status() == WL_NO_MODULE) {
            Serial.println("Communication with WiFi module failed!");
            // don't continue
            while (true);
        }

    // attempt to connect to Wifi network:
    while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
        status = WiFi.begin(ssid, pass);
        // wait 2 seconds for connection:
        delay(2000);
        }
        Serial.println("Connected to wifi");
        printWifiStatus();
        
    //MPU
    Wire.begin();
    mpu6050.begin();
    mpu6050.calcGyroOffsets(true);

}

void loop() {  
    mpu6050.update();
    if(millis() - timer > interval){
        int t = timer;
        int z = mpu6050.getAccX();
        timer = millis();
        if (arr_length < 1) {
            data.concat("data=[[");
            data.concat(t);
            data.concat(",");
            data.concat(z);
            data.concat("],[");
            arr_length++;
        } else if (arr_length <= 250) {
            data.concat(t);
            data.concat(",");
            data.concat(z);
            data.concat("],[");
            arr_length++;    
        } else {
            data.concat(t);
            data.concat(",");
            data.concat(z);
            data.concat("]]");
            dataold = data;
            data.remove(0);
            arr_length = 0;
        }        
    }
    if (dataold.length() > 10) {
        if (client.connect(server,80)) {
            client.print("POST ");
            client.print(script);
            client.println(" HTTP/1.1");
            client.print("Host:");
            client.println(server);
            client.println("Content-Type: application/x-www-form-urlencoded");
            client.print("Content-Length:");
            client.println(dataold.length());
            client.println();
            client.print(dataold);
            client.stop();
            dataold.remove(0);
        }
    }
}

You should check out the TimerOne library here and look at the interrupt example.

You can fetch and store your data in the interrupt routine. In loop() you will continue to update and check the size of arr_length. When it hits 250, copy the data (like in the interrupt example) reset your variables and then execute the POST. The interrupts should continue in the background filling up the array again while the data is being transmitted. (Unless the WiFi library does some blocking code... I don't know about that)

blh64:
You should check out the TimerOne library here and look at the interrupt example.

You can fetch and store your data in the interrupt routine. In loop() you will continue to update and check the size of arr_length. When it hits 250, copy the data (like in the interrupt example) reset your variables and then execute the POST. The interrupts should continue in the background filling up the array again while the data is being transmitted. (Unless the WiFi library does some blocking code... I don't know about that)

Thank you, that's a great idea.
Unfortunately, that library and all sinilar ones i found do not work for the MKR Wifi 1010 because it is a 32bit model.

I found something here:

avandalen:
See the full article here.

Without having a sophisticated library, using the Timer/Counter is a complicated task and you have to go into many details such as counter modes, compare/capture channels, prescaler values etc. Here, a proper library makes life easier. I'm lazy, the only thing I want to do is specify the period time and the output pin, and the rest should be figured out by the library.

SAMD21 Timer
I dont't kow, if it could be working in my case, because I don't use pin for the output of my data.
Any suggestions?

I found some alternatives: arduino-tasker and Scheduler

I tried implementing it (both) into my code, but it made no big difference.
I still don't get the wifi module to send the data while the sensor-reading is continued...

I am thankful for any help