Serial overload- Sensor readout in conflict with Nextion display communication?

Hello

attached you find the .ino file which establishes the communication between I2c sensors, Nextion display and a Controllino Maxi Automation. The Controllino uses a MEGA 2560 as MCU. The Serial2 port is set at 115200 baud. Initially I am trying to readout a BME280 sensor and want to display the values on a Nextion enhanced display. Therefore I use void pressure_temperature_humidity(). Next to reading out the values I also want to let follow actions on touching buttons.
If pressure_temperature_humidity(); is commented out in the loop() the button touching is followed by correct actions(i.e.setting Digital outputs).
The serial monitor returns no recvRetNumber err for Nextion com. This is not the case if pressure_temperature_humidity();
is implemented. So I guess that the serial communication is overloaded and correct messaging is not possibly anymore. How can I prevent such overload. I already added a delay behind the bme readouts without success.

RMC_HMI.ino (39.2 KB)

As conflict seems to occur with BME280 sensor readout I added library files as well

Adafruit_BME280.cpp (16.9 KB)

Adafruit_BME280.h (8.89 KB)

Maybe there is a conflict in the registry between Nextion and sensor actions.

Would a timer interrupt for reading the sensor data be more successful?

Hello marcel00,
You are probably disappointed not to get any response. I can tell you why I didn't respond and give you some very limited help.

You posted your code as an attachment, I am sure by now you have read the forum rules and know how to post your code into the forum directly.

From what you say I think you are using the Nextion libraries, about which I know next to nothing other then they are full of bugs and cause problems. If you want to use the libraries follow the link from 'Using Nextion displays with Arduino' to Ray Livingston's versions of the libraries. I've not tried them but I understand they work much better.

Maybe there is a conflict in the registry between Nextion and sensor actions.

If you mean the registry on your computer I cannot imagine how that could be the case, if you mean the registry in the Arduino, there isn't one.

Would a timer interrupt for reading the sensor data be more successful?

Not a clue, try it.

I already added a delay behind the bme readouts without success.

If you mean delay(); then remove it. If delay(); is the solution to a problem then it is masking other problems elsewhere. You could however arrange that the sensors are only read every few seconds or whatever using millis(); as a timer.

Thank you Perry. I will check it out.

PerryBebbington:
You posted your code as an attachment, I am sure by now you have read the forum rules and know how to post your code into the forum directly.

Not with 39k of code

...R

marcel00:
attached you find the .ino file

I'm far too lazy to try to figure out 39k of code.

Can you write a short program that illustrates the problem and post that?

Alternatively, nobody writes 39k of code without testing many many times during its development. You must have had an earlier version of the program which did work properly - what was the addition to the code that cause it to break.

...R

Not with 39k of code

Well, good point, but it helps to mention that this is the reason for not posting it in the message.

I'm far too lazy to try to figure out 39k of code.

Me too!

Now I have installed Ray Livingston's versions of the nextion itead library.
I have the following setup and loop in the program I already loaded up:

void setup() {   
  // input pins
  pinMode(wheelsensorPin, INPUT_PULLUP);
  pinMode(hallsensor_leftPin, INPUT_PULLUP);
  pinMode(hallsensor_rightPin, INPUT_PULLUP);
  pinMode(speedgripPin, INPUT);
  pinMode(keyswitchPin, INPUT);
  pinMode(vitotalPin, INPUT);
  // output pins
  pinMode(activationPin, OUTPUT);
  pinMode(cooling1Pin, OUTPUT);
  pinMode(cooling2Pin, OUTPUT);
  pinMode(foilheater1Pin, OUTPUT); 
  pinMode(foilheater2Pin, OUTPUT);  
  pinMode(cirrusheaterPin, OUTPUT); 
  pinMode(cirrusfanPin, OUTPUT);  
  pinMode(lightPin, OUTPUT);
  pinMode(directionPin, OUTPUT);
  pinMode(honkPin, OUTPUT);
  pinMode(leftblinkerPin, OUTPUT);
  pinMode(rightblinkerPin, OUTPUT);

  // set outputs to low -> relays are off
  digitalWrite(activationPin, HIGH);// Motor controllers are off at setup
  digitalWrite(cooling1Pin, LOW);
  digitalWrite(cooling2Pin, LOW);
  digitalWrite(foilheater1Pin, LOW);
  digitalWrite(foilheater2Pin, LOW);
  digitalWrite(cirrusheaterPin, LOW);
  digitalWrite(cirrusfanPin, LOW);
  digitalWrite(lightPin, LOW);
  digitalWrite(directionPin, HIGH);
  digitalWrite(honkPin, LOW);
  digitalWrite(leftblinkerPin, LOW);
  digitalWrite(rightblinkerPin, LOW);

  wheelsensorCounter = maxwheelsensorCounter;
  trip();
  
  Serial.begin(115200);
  Serial2.begin(115200);
  dac_L.begin(0x62);
  dac_R.begin(0x63);
  //bme.begin(0x76);
  nexInit();

void loop() {
//nexLoop(nex_listen_list);
Serial.println("TEST");
/*  int switchonoff = digitalRead(keyswitchPin);
  if (switchonoff == HIGH && setswitch == 0){ //initially get stored values from EEPROM if keyswitch turned on
  Serial.print("TEST");
  setswitch = 1; 
  }
  if (switchonoff == HIGH && setswitch == 1){
  Serial.print("TEST2"); 
  //setpoint_angular_velocities();
   pageid.getValue(&pId);
   if (pId == 1)
   {  
     if (initialize == 1)
     {
       get_stored_values(); 
       digitalWrite(activationPin, LOW); // motor controllers are activated by Low signal
       initialize = 0;
     } 
     btx_state_page1();
    // electric_current_and_charge();
     blinker();
     lightonoff();
     honkonoff();
     autocruise();
   //  trip();
   // actualrpm(5,1);
  //  actualrpm(4,2);
   //  electronic_differential();
    // display_speed();
   //  if( blinkermarker == false){
      pressure_temperature_humidity();
    // }
   }
   if (pId == 2){
     btx_state_page2(); 
   }
   if (pId == 3){
    btx_state_page3();
   }
   if (pId == 4){ 
    btx_state_page4(); 
   }

  if (pId == 5)
  {
    btx_state_page5();
  }
 }
if (switchonoff == LOW && setswitch == 1){ //store only changed values to EEPROM if keyswitch turned off
  Serial.print("TEST off");
  store_values();
  digitalWrite(activationPin, HIGH); //Motor controllers are switched off
  setswitch = 0; 
  initialize = 1;
 }*/
}

}

If I comment out nexInit(); and start the serial monitor "TEST" is displayed at 115200 baud
If nexInit(); is executed I get nothing at 115200 baud but "TEST" at 9600 baud
Thus nexInit has somehow changed the baud rate of serial ports.
As Nextion display is set to 115200 baud as well I get a time out as communication is set to 9600 baud.
I already tried to reinstall the original Iteadlib from Nextion without success.

Where can I change the baud rate at Nextion library such that communication starts at 115200 baud?

It can be changed in Nexhardware.cpp. In the past I did not need to change it? I guess Nextion lib took over baud settings from prior defined Serial.begin(); or Serial2.begin();

After changing baud rate in Nexhardware.cpp I get the following error if I try to open the serial monitor

Error at setting of parameters for serial port 115.200 N 8 1

???