ESP32 prints data very slowly

Board: ESP32 30 Pin

Issue: The code execution is very slow, the data is printed at random time in serial monitor. I haven't connected anything with the esp. I also tested this code with 4-5 different esp and still the same issue occurs.

I ran the same code on an RP2040-Zero board from Waveshare and it ran perfectly, no such delays.

Images:

ESP32 (See the time difference between zeroes)

RP2040

Code:

#include <DFRobot_BME680_I2C.h>
#include <Wire.h>
#include <TinyGPS++.h>
#include <RTClib.h>
#include <math.h>
#include <SimpleKalmanFilter.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <uEEPROMLib.h>
#include <Adafruit_VL53L0X.h>

#define S1_RXD 12
#define S1_TXD 14
#define S2_RXD 27
#define S2_TXD 26

const byte M2 = 18, M1 = 19, MQ1 = 2, MQ2 = 4, VS = 25; //, servo1Pin = 5, servo2Pin = 17;
//Servo servo1,servo2;

byte a, sensor_status[4] = { 1, 1, 1, 1 }, leg_status[6] = { 0, 0, 0, 0, 0, 0 };
bool count = 0, motor = 0, arm_up = 0, arm_down = 0;
long unsigned int time1000 = 0, packetcount = 0;
unsigned int hour, minute, tof;
float voltage_cs, ReferencePressure, b, altitude, volt = 0.0, pressure, kalman_pres, percent;

typedef struct struct_message {
  int a;
  float roll;
  float pitch;
  float yaw;
} struct_message;

struct_message myData;
//esp_now_peer_info_t peerInfo;
sensors_event_t orientationData;
SimpleKalmanFilter pressureKalmanFilter(1, 1, 1);

DFRobot_BME680_I2C bme(0x77);  //0x77 I2C address
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);
RTC_DS3231 rtc;
TinyGPSPlus gps;
uEEPROMLib eeprom(0x50);

void setup() {
  
  Serial.begin(115200);
  Serial1.begin(230400);
  Serial2.begin(9600);
  delay(100);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);

  digitalWrite(M1, LOW);
  digitalWrite(M2, LOW);

 /* Wire.begin();
   WiFi.mode(WIFI_STA);*/

  if (!rtc.begin())
    sensor_status[0] = 0;
  //Serial.println("RTC Error");

  if (!bno.begin())  //Initialize IMU sensor
    sensor_status[1] = 0;
  //Serial.println("IMU Error");

  if (bme.begin())  //Initialize Pressure sensor
    sensor_status[2] = 0;
  //Serial.println("Pressure Sensor Error");

  if (!lox.begin())
    sensor_status[3] = 0;
  //Serial.println("Distance Sensor Error");

  lox.startRangeContinuous();

  if (rtc.lostPower())
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  for (int i = 0; i < 50; i++) {
    bme.startConvert();
    bme.update();
    b += bme.readPressure();
    Serial.println(b);
    delay(100);
  }
  ReferencePressure = b / 50;
  b = 0;
}

void loop() {

  bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER);
  myData.roll = orientationData.orientation.y;
  myData.pitch = orientationData.orientation.z;
  myData.yaw = orientationData.orientation.x;

  a = Serial1.read();  // Serial.read();  Send 1-12 nos.

  if (a > 0) {
    myData.a = a;
    motor = 0;
    arm_up = 0;
    arm_down = 0;
  }

   motion();
 if (millis() - time1000 >=1000) {
  
  time1000+=1000;
    if (lox.isRangeComplete())
      tof = lox.readRange() - 8;

    smartDelay(0);
    bme.startConvert();
    bme.update();
    DateTime now = rtc.now();
    kalman_pres = pressureKalmanFilter.updateEstimate(bme.readPressure());

    altitude = 44330 * (1 - pow(kalman_pres / ReferencePressure, 0.1903));
    volt = (analogRead(VS) * 3.30) / (0.2 * 4095.0) + 0.66;
    percent = (voltage_cs/12.6)*100;
    hour = gps.time.hour() + 5;
    minute = gps.time.minute() + 30;

    if (minute > 59) {
      minute = minute - 60;
      hour++;
    }
    if (hour > 23)
      hour = hour - 24;

    String tele= String("Team_xyz") + ',' + String(now.hour()) + ':' + String(now.minute()) + ':' + String(now.second())  + ',' + 
                 String(packetcount) + ',' + String(altitude,2) + ',' + String(kalman_pres) + ',' + String(bme.readTemperature()/100, 2)
                  + ',' + String(bme.readHumidity()/1000, 2) + ',' + String(bme.readGasResistance())+ ',' + String(analogRead(MQ1)) + ',' 
                  + String(analogRead(MQ2)) + ',' + String(volt,2) + ',' + String("0") +',' + String(tof) + ',' + String(myData.roll) + ','
                  + String(myData.pitch) + ',' + String(myData.yaw) + ',' + String(hour) + ':' + String(minute) + ':' + String(gps.time.second()) + ',' + 
                  String(gps.location.lat(), 4) + ',' + String(gps.location.lng(), 4) + ',' + String(gps.altitude.meters(), 1) + ',' 
                  + String(gps.satellites.value()) + ',' + String(percent,0);

    Serial2.println(tele);
    Serial1.println(tele);
    Serial.println(tele);
    Serial.println(myData.a);
    packetcount++;
 }
}

void motion() {
  switch (myData.a) {

    case 57:  //Wheel_down
      if (motor == 0) {
        delay(2000);
        digitalWrite(M1, HIGH);
        digitalWrite(M2, HIGH);
        motor = 1;
      }
      break;

    case 58:  //Wheel_up
      digitalWrite(M1, LOW);
      digitalWrite(M2, LOW);
      break;

    case 59:
      if (arm_down == 0) {
        ARM_DOWN();
        arm_down = 1;
      }
      break;

    case 60:
      if (arm_up == 0) {
        ARM_UP();
        arm_up = 1;
      }
      break;

    default:

      break;
  }
}

void ARM_UP() {                                                                                                                                   
}

void ARM_DOWN() {
}

static void smartDelay(unsigned long ms) {
  unsigned long start = millis();
  do {
    while (Serial2.available())
      gps.encode(Serial2.read());
  } while (millis() - start < ms);
}

unsigned long or unsigned int
but not long unsigned int

and unsigned long is not the same as unsigned int

anything using millis(), calculations based on results of millis(), should be unsigned long

If it's truly freezing then it will chuck an error log (which will pop up in Serial Monitor).

I modified it, still the problem persists

Serial monitor output:

00:28:17.886 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
00:28:17.886 -> configsip: 0, SPIWP:0xee
00:28:17.886 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
00:28:17.886 -> mode:DIO, clock div:1
00:28:17.886 -> load:0x3fff0030,len:1184
00:28:17.886 -> load:0x40078000,len:13260
00:28:17.886 -> load:0x40080400,len:3028
00:28:17.886 -> entry 0x400805e4
00:29:15.135 -> 0.00
00:29:25.337 -> 0.00
00:29:35.511 -> 0.00
00:29:45.722 -> 0.00
00:29:55.938 -> 0.00
00:30:06.132 -> 0.00
00:30:16.320 -> 0.00
00:30:26.547 -> 0.00
00:30:36.747 -> 0.00

The delay between each new value of 0 is nearly 10 sec. In RP2040, these zeroes are printed instantly.

It wasn't meant to be a complete cure.
I don't have all those libraries.
I'm wondering if any warnings and so on were made by the compiler (about memory).
If it 'freezes', if it doesn't loop, then it will watchdog - start over again.
If that happens there's an error log (previously mentioned).

It doesn't freeze, (sorry for wrong meaning) but the data is printed very slowly in the serial monitor. I don't get an error code.
But I don't know why the data prints so slowly. And the amount of delay between new data displayed in serial monitor also remains fixed (about 10 secs). I have reset the board multiple time and the delay duration is consistent.

Libraries:
Adafruit BNO055: GitHub - adafruit/Adafruit_BNO055: Unified sensor driver for the Adafruit BNO055 orientation sensor breakout
Remaining lib:
Libraries.zip (1.6 MB)

I ran the code for about an hour and here is the serial monitor log:

00:29:25.337 -> 0.00
00:29:35.511 -> 0.00
00:29:45.722 -> 0.00
00:29:55.938 -> 0.00
00:30:06.132 -> 0.00
00:30:16.320 -> 0.00
00:30:26.547 -> 0.00
00:30:36.747 -> 0.00
00:30:46.931 -> 0.00
00:30:57.123 -> 0.00
00:31:07.346 -> 0.00
00:31:17.541 -> 0.00
00:31:27.745 -> 0.00
00:31:37.944 -> 0.00
00:31:48.138 -> 0.00
00:31:58.342 -> 0.00
00:32:08.542 -> 0.00
00:32:18.723 -> 0.00
01:23:06.330 -> 255
01:26:36.418 -> Team_xyz,45:162:0,13,nan,0.00,0.00,0.00,0.00,0,147,2.59,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:26:36.418 -> 255
01:30:06.530 -> Team_xyz,45:162:0,14,nan,0.00,0.00,0.00,0.00,0,148,2.59,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:30:06.530 -> 255
01:33:36.634 -> Team_xyz,45:162:0,15,nan,0.00,0.00,0.00,0.00,0,159,2.62,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:33:36.634 -> 255
01:37:06.723 -> Team_xyz,45:162:0,16,nan,0.00,0.00,0.00,0.00,0,144,2.57,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:37:06.723 -> 255
01:40:36.828 -> Team_xyz,45:162:0,17,nan,0.00,0.00,0.00,0.00,0,147,2.59,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:40:36.828 -> 255
01:44:06.924 -> Team_xyz,45:162:0,18,nan,0.00,0.00,0.00,0.00,0,144,2.59,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:44:06.924 -> 255
01:47:37.031 -> Team_xyz,45:162:0,19,nan,0.00,0.00,0.00,0.00,0,139,2.55,0,65527,0.00,0.00,0.00,5:30:0,0.0000,0.0000,0.0,0, 0
01:47:37.031 -> 255

The problem was with the libraries <DFRobot_BME680_I2C.h> and <Adafruit_VL53L0X.h>. I think they are not compatible with ESP32, even if they compile without any errors, since using the same libraries, the code ran perfectly on RP2040.
I changed the libraries to <Adafruit_BME680.h> and <DFRobot_VL53L0X.h> and now the code runs perfectly.
Still, I would like to know the cause of this, if possible
Code:

#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
#include <Wire.h>
#include <TinyGPS++.h>
#include <RTClib.h>
#include <math.h>
#include <SimpleKalmanFilter.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <uEEPROMLib.h>
#include <DFRobot_VL53L0X.h>

const byte M2 = 18, M1 = 19, MQ1 = 2, MQ2 = 4, VS = 25; //, servo1Pin = 5, servo2Pin = 17;
//Servo servo1,servo2;

byte a, sensor_status[4] = { 1, 1, 1, 1 }, leg_status[6] = { 0, 0, 0, 0, 0, 0 };
bool count = 0, motor = 0, arm_up = 0, arm_down = 0;
unsigned long time1000 = 0;
unsigned int hour, minute, packetcount = 0;
float voltage_cs, ReferencePressure, b, altitude, volt = 0.0, pressure, kalman_pres, percent;

typedef struct struct_message {
  int a;
  float roll;
  float pitch;
  float yaw;
} struct_message;

struct_message myData;
//esp_now_peer_info_t peerInfo;
sensors_event_t orientationData;
SimpleKalmanFilter pressureKalmanFilter(1, 1, 1);

Adafruit_BME680 bme; // I2C
DFRobot_VL53L0X sensor;
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);
RTC_DS3231 rtc;
TinyGPSPlus gps;
uEEPROMLib eeprom(0x50);

void setup() {
  
  Serial.begin(115200);
  Serial1.begin(230400);
  Serial2.begin(9600);
  delay(100);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);

  digitalWrite(M1, LOW);
  digitalWrite(M2, LOW);

  Wire.begin();

  if (!rtc.begin())
    sensor_status[0] = 0;
  //Serial.println("RTC Error");

  if (!bno.begin())  //Initialize IMU sensor
    sensor_status[1] = 0;
  //Serial.println("IMU Error");

  if (bme.begin())  //Initialize Pressure sensor
    sensor_status[2] = 0;
  //Serial.println("Pressure Sensor Error");

  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms
  
  sensor.begin(0x50);
  if (1)
    sensor_status[3] = 0;
  //Serial.println("Distance Sensor Error");

  sensor.setMode(sensor.eContinuous,sensor.eLow);
  sensor.start();

  if (rtc.lostPower())
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  for (int i = 0; i < 50; i++) {
    bme.performReading();
    b += bme.pressure;
    Serial.println(b);
    delay(100);
  }
  ReferencePressure = b / 50;
  b = 0;
}

void loop() {

  bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER);
  myData.roll = orientationData.orientation.y;
  myData.pitch = orientationData.orientation.z;
  myData.yaw = orientationData.orientation.x;

  a = Serial1.read();  // Serial.read();  Send 1-12 nos.

  if (a > 0) {
    myData.a = a;
    motor = 0;
    arm_up = 0;
    arm_down = 0;
  }

   motion();

 if (millis() - time1000 >=1000) {
  
  time1000+=1000;

    smartDelay(0);
    bme.performReading();
    DateTime now = rtc.now();
    kalman_pres = pressureKalmanFilter.updateEstimate(bme.pressure);

    altitude = 44330 * (1 - pow(kalman_pres / ReferencePressure, 0.1903));
    volt = (analogRead(VS) * 3.30) / (0.2 * 4095.0) + 0.66;
    percent = (voltage_cs/12.6)*100;
    hour = gps.time.hour() + 5;
    minute = gps.time.minute() + 30;

    if (minute > 59) {
      minute = minute - 60;
      hour++;
    }
    if (hour > 23)
      hour = hour - 24;

    String tele= String("Team_xyz") + ',' + String(now.hour()) + ':' + String(now.minute()) + ':' + String(now.second())  + ',' + 
                 String(packetcount) + ',' + String(altitude,2) + ',' + String(kalman_pres) + ',' + String(bme.temperature, 2)
                  + ',' + String(bme.humidity, 2) + ',' + String(bme.gas_resistance/1000.0)+ ',' + String(analogRead(MQ1)) + ',' 
                  + String(analogRead(MQ2)) + ',' + String(volt,2) + ',' + String("0") +',' + String(sensor.getDistance()) + ',' + String(myData.roll) + ','
                  + String(myData.pitch) + ',' + String(myData.yaw) + ',' + String(hour) + ':' + String(minute) + ':' + String(gps.time.second()) + ',' + 
                  String(gps.location.lat(), 4) + ',' + String(gps.location.lng(), 4) + ',' + String(gps.altitude.meters(), 1) + ',' 
                  + String(gps.satellites.value()) + ',' + String(percent,0);

    Serial2.println(tele);
    Serial1.println(tele);
    Serial.println(tele);
    Serial.println(myData.a);
    packetcount++;
 }
}

void motion() {
  switch (myData.a) {

    case 57:  //Wheel_down
      if (motor == 0) {
        delay(2000);
        digitalWrite(M1, HIGH);
        digitalWrite(M2, HIGH);
        motor = 1;
      }
      break;

    case 58:  //Wheel_up
      digitalWrite(M1, LOW);
      digitalWrite(M2, LOW);
      break;

    case 59:
      if (arm_down == 0) {
        ARM_DOWN();
        arm_down = 1;
      }
      break;

    case 60:
      if (arm_up == 0) {
        ARM_UP();
        arm_up = 1;
      }
      break;

    default:

      break;
  }
}

void ARM_UP() {                                                                                                                                   
}

void ARM_DOWN() {
}

static void smartDelay(unsigned long ms) {
  unsigned long start = millis();
  do {
    while (Serial2.available())
      gps.encode(Serial2.read());
  } while (millis() - start < ms);
}

Good job.
To find out why -- you'd have to analyze, pick apart, the libraries.
I have encountered instances of lack of ESP compatibility too.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.