Nextion Display sending corrupt data

Hi,

My Nextion display is sending corrupt data to the arduino ONLY if the here commented out code is added to the loop. The same problem occurs if I am using normal serial instead of Softserial.

The corrupt data looks like this in the serial Monitor:


⸮⸮
⸮⸮⸮
⸮⸮⸮⸮
⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮⸮⸮⸮h
⸮⸮⸮⸮⸮⸮⸮⸮⸮he
⸮⸮⸮⸮⸮⸮⸮⸮⸮he⸮

Every row appears at a new button press on the Nextion touchscreen.

If I comment out the stuff which is also commented out in the attached code here it works as intended, the word "hello" is visiable in the serial monitor and the variable "trip" inside the if-condtion is changed to 3 as wanted.

Thanks in advance,
Johannes

#include <SoftwareSerial.h>
SoftwareSerial nextion(5, 6); // RX, TX
String data = "";
int trip,x;


void setup() {
Serial.begin(9600);
nextion.begin(9600);
 

 
}

void loop() {


  if (nextion.available())

  {
    while (nextion.available())
    {
      data += char (nextion.read());
      Serial.println(data);
      
    }

  }

  if (data == "hello")
  {
    
    trip=3;
    data = "";
    
  }
/*
   nextion.print("rpm.val=");
   nextion.print(x);
   nextion.write(0xff);
   nextion.write(0xff);
   nextion.write(0xff);

   nextion.print("trip.val=");
   nextion.print(trip);
   nextion.write(0xff);
   nextion.write(0xff);
   nextion.write(0xff);
*/

   x++;

    if(x==100)
    {
      x=0;
    }
}

The code works as you wrote it:

You print the same string again and again once the Serial received a new byte.

Hi,
Yes it does, but thats just for me debugging. If I comment out the stuff which is also commented out in my code the serial monitor looks like this (for two button presses), which I guess is correct and without these wiered "⸮⸮⸮⸮⸮".

h
he
hel
hell
hello
h
he
hel
hell
hello

Are you saying that when you write to the Nextion on each pass of the loop, you can not read correctly from the Nextion?

How fast do you need to send the rpm and trip value. What if you send on a millis( ) timer every 500 ms? Can you read correctly then?

Every row appears at a new button press on the Nextion touchscreen.

What is the button on the Nextion programmed to send? Is it a release or press event?

This may be the problem, yes. But it should normally work like this right? There shouldnt be anything wrong with sending stuff to the nextion on every loop?
Unfortunatly I need to send the rpm and other values to the nextion as often as possible in the final code since these values will be for dynamic dials and stuff like that.

There is no point to update dynamic dials more than 20-30 times at second.

I have had issues in the past where frequent writing has affected reading.

With 9800 baud and software serial, you may need to do some experimentation as to how fast you can update the display.

I also have this exact same problem with normal serial ports. There is any other way to fix it?

Thanks very much for your replies!

Every loop is too fast

You don't. A human can only read maybe 2 or 3 changes per second, there's no point updating a display faster than that because you can't read it any faster. Slow your update rate to no more than once every half second.

Can you upload your hmi file please? You will need to put it in a zip file.

The button for the operation is the "Ja" button on page "Confirmation".

The problem is that "rpm" will be given to the progress bar on the "Main" page. If I would have a slow update rate the progress bar would be laggy and wouldnt be smooth if I rev the engine for example of which the rpm will be measured.

Thanks for your reply!

2024 - Test.zip (704.0 KB)

Thanks.
Please post a photo (or 2 or 3) clearly showing how the Nextion is connected to your Uno and power.

Hi, the power cable coming in and connecting underneath the Arduino comes from a 5W power supply. (Circuit only draws around 1W). The cable connecting the Arduino and nextion is an approx. 1m long shield USB cable. I also tried it once to connect the nextion Tx to Arduino RX with a shorter cable but it's the same result.





Thanks!

Thanks for the photos, the wiring looks correct but you definitely need more practice on soldering!

I tried your code and HMI file, there is an error in your HHM file, however, the effect of that error should be that you get no output at all, but you say you are getting lots of question marks, which makes me think the HMI file you uploaded isn't the one you are using. The error is on b2 'Ja' on the confirmation page, the button you are pressing. As 'touch release event' you have:

page Menü
print "hello"

So the 'print "hello" never happens because it is after the page has changed to Menü

I swapped these round to:

print "hello"
page Menü

I used a Nextion connected to a Uno, ran your code and the serial monitor gave me:

10:47:58.170 -> h
10:47:58.170 -> he
10:47:58.217 -> hel
10:47:58.217 -> hell
10:47:58.217 -> hello

So, apart from the mistake in the HMI file, your code and Nextion configuration is good.
Are you sure the serial monitor is set for 9600 Baud?
Although your wiring looks OK in the photos I obviously cannot check it, so please check for shorts or bad connections.

Shielded cable only works if you connect the shield at one end only, which should be the micro-controller end. However, at 9600 Baud serial will got a lot further than 1m without a problem without using special cable, so whatever your problem is, it is not the cable or the shield.

Not related to your problem but something I still think will cause you problems is the update rate for 'rpm', you need to review that. Maybe 3 times per second isn't fast enough but it can't be as fast as possible, it needs to be controlled. At 9600 Baud characters take about 1ms each, so no more than about 1000 per second. If your Nextion update string is 10 characters that's 100 updates per second absolute maximum. I suggest you aim for 10 per second, see how it looks then play around with it. However, given what you have done so far I think you have more to learn before you get to that point.

Strings (capital S) have a bad reputation on Arduino, search the forum for discussion on this. The recommendation is to use 'C strings' (lower case s) AKA char arrays. All my example code in my Nextion tutorial used c strings.

Thanks for the reply and your time!!

Yes, sorry the hmi is not exactly the same, I had a test button on the main page just with the command "print "hello" " which I deleted.

But is it actually possible to be a wire problem if the system works correctly when I just change the code? As I said I also get the same output in the serial monitor if I disable the writing to the display.

h
he
hel
hell
hello

So this exact setup works for you even with sending data to Arduino on every loop? That's frustrating:(

I will definitely try your suggestions though with C strings and Limited update rate!

No, good point.

Then that's your problem, too many writes to the display is clogging something up. I would guess that you are sending more characters than software serial can cope with and so it's not dealing with the incoming characters properly. I suggest you buy a board with a spare hardware serial port, such as a Mega or a Nano Every, and forget software serial. You still need to control the rate you write to the Nextion. You also need to control when you send stuff, the RPM display isn't on the confirmation page, so you should not be sending RPM data when the confirmation page is not being displayed. In fact, come to think of it, I think if you send RPM data when the page is not displayed the Nextion will send back data complaining you are doing it (I think, not sure), further clogging up the serial port.

1 Like

Alright. I just tired it with hardware serial again without the PC connected and it sometimes works (like 1 out of 5 times) after freshly restarting the arduino with the reset button, it changes the variable "trip" on the screen if the button is pressed.

Since it seems to work for you, might there be a defect with my screen in particular or are there differences between the Basic/ enhanced/ intelligent displays which would cause this? I have a Basic display. I also replaced the arduino board onces with the exact same issues.

Seriously, you are making life hard for yourself by only having one hardware serial port, buy a board with at least one spare one and slow down the updates to RPM.

1 Like

Given what you are trying to do, I too would recommend that you use an Arduino with additional hardware Serial ports. I use a Nano Every (running at 20 MHz) in my Nextion projects.

With a hardware serial port you can more successfully raise the baud rate from the default 9600.

I would think that if you update the rpm bar every 50ms using one of the Nextion timers (tim) to read a value which you update from the Arduino you will be able to follow your throttle smoothly.

The Arduino could even write to the value faster than every 50ms and since it looks like you are intending to write a scaled value 0=100 for the rpm it should be quite fast to send a single byte without bogging the system down.

You would be better off with a Discovery or Intelligent model as the processor on board is faster.

1 Like

Alright thanks, I will try that! One last thing I want to say. I put this code into my final project code which only loops at roughly 18 times per second which i measured with millis(). The button works sometimes, so sometimes garbage is send to arduino and sometimes the correct data (roughly 1 in 5-10 times it works). This final code sends a total of 4 values to the nextion.
Is this behavior consistent with your previous assumtions?

Johannes

I'm a little unclear as to why the button needs to send a message like "hello" instead of a single character or command number.

Moreover, If you are on the page to send the button message, you do not need to be updating the display of the rpm as it is not visible. Only send the data to the rpm bar when you are on that page.

You may want to post more complete Arduino code and associated hmi file as I'm not understanding the complete picture.