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);
}
}
}