Hi,
I have a custom ESP32-S2 board (schematics and PCB files here) with a build-in MPU6050 sensor. I want the board to continuously send me sensor values via Serial at the highest possible frequency. On my computer I want to save this Serial input to a txt file.
Here is my code that is running on the board:
#include "WiFi.h"
#include "time.h"
#include "Wire.h"
#include <Adafruit_MPU6050.h>
Adafruit_MPU6050 mpu;
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
#include "Freenove_WS2812_Lib_for_ESP32.h"
#define LED_PIN 1
Freenove_ESP32_WS2812 led = Freenove_ESP32_WS2812(1, LED_PIN, 0, TYPE_GRB);
const char* ssid = "SSID";
const char* password = "PASSWORD";
long timer = 0;
struct tm timeinfo;
time_t now;
sensors_event_t a, g, temp;
String append;
void setLED(uint8_t r,uint8_t g,uint8_t b) {
led.setLedColorData(0, r, g, b);
led.show();
}
void setup() {
// ******** LED for debugging *********
led.begin();
led.setBrightness(10);
setLED(60,60,0); // yellow
// **************************
Serial.begin(115200);
// ********* Sync time *********
//connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED);
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
// **************************
// ********* mpu6050 *********
Wire1.begin();
if (!mpu.begin(0x68, &Wire1)) {
Serial.println("MPU6050 Chip wurde nicht gefunden");
return;
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
// **************************
setLED(0,60,0); // green
}
void loop() {
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
time(&now);
mpu.getEvent(&a, &g, &temp);
append = "";
append += String(now)+","+String(millis());
append += ",";
append += String(a.acceleration.x, 6);
append += ",";
append += String(a.acceleration.y, 6);
append += ",";
append += String(a.acceleration.z, 6);
append += "\n";
setLED(60,0,0); // red
Serial.print(append);
delay(20);
setLED(0,60,0); // green
}
On my computer I have this python script running to read the serial input:
import serial
def readserial(baudrate):
try:
with serial.Serial(port='/dev/ttyACM0', baudrate=baudrate) as ser:
for line in ser:
line = line.decode('utf-8')
if line:
print(line.rsplit('\n')[0])
except:
with serial.Serial(port='/dev/ttyACM1', baudrate=baudrate) as ser:
for line in ser:
line = line.decode('utf-8')
if line:
print(line.rsplit('\n')[0])
if __name__ == '__main__':
readserial(115200)
After a while (perhaps 5 minutes, but sometimes also after several hours) I will suddenly not receive any new serial lines and the led on the board will just be red, meaning that the board is apparently stuck at the Serial.print command. I have tried to put a delay of 20ms after the Serial.print, but it doesnt help. I also tried to simply print three random floats instead of reading from the sensor, but it will still get stuck.
I am not sure if this is perhaps an issue with my computer. I am running on ubuntu for that matter.
Any ideas are appreciated. Thank you for your time