Problems with serial communication between Nextion and Arduino

I have been struggling to get smooth communication between my Nextion LCD and an Arduino Nano for quite some time now. I have finally got the Nextion to send the string I want it to, over serial, but the problem is: Every time I change a value on the nextion by toggling a button, more gibberish is added to the serial. Is there any way to eliminate this?

Here is my code:

String Data = "";

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

}

void loop() {

  while (Serial.available())
    {
        char character = Serial.read();        
        Data.concat(character);
        if (character == '*')
        {
            Serial.print("Received: ");
            Serial.println(Data);
            Data = "";
        }
    }

}

The Nextion code and some screenshots of the problem is in the attachments.

test4.zip (120 KB)

Is there any way to eliminate this?

Certainly. Stop using Strings.

Whydoyouprintthreepiecesoftextwithnodelimiters?Ifitisnotimportanttounderstandwhatyouaresending,whysendit?

It looks to me like the problem is really on the Nextion end.

OK, I am really new to this stuff so if you could maybe give me some examples of what I should do instead?
It is important to understand what I am sending, I want to break up the string into arrays later and use them as commands to turn some LED's on and off.

Thanks allot for the reply :slight_smile:

OK, I am really new to this stuff so if you could maybe give me some examples of what I should do instead?

Use char arrays.

It is important to understand what I am sending,

That'swhyeverythingisjammedtogeterwithnopossiblyofparsingitmeaninfully? I see.

http://forum.arduino.cc/index.php?topic=396450.0

All right, I got it working buttery smooth and without any hiccups.
I changed the timer interval in the Nextion to 50 millis.
I used the third example in the page you linked, thanks alot.

Here is my final code:

#include "Nextion.h"
NexWaveform s0 = NexWaveform(1, 1, "s0");

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    nexInit();
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(A0, INPUT);
}

void loop() {
    unsigned long curntMillis = millis();
    if(Serial.available()<1){    
    progBar();
    }
    recvWithStartEndMarkers();
    useNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void useNewData() {
    if (newData == true) {        
        String nexDat = receivedChars;
        String xval = getValue(nexDat, ',', 0);
        String yval = getValue(nexDat, ',', 1);
        String zval = getValue(nexDat, ',', 2);

        if(xval == "as"){digitalWrite(11, LOW);}
        if(xval == "af"){digitalWrite(11, HIGH);}
        if(yval == "bs"){digitalWrite(10, LOW);}
        if(yval == "bf"){digitalWrite(10, HIGH);}
        if(zval == "cs"){digitalWrite(9, LOW);}
        if(zval == "cf"){digitalWrite(9, HIGH);}
        
        newData = false;  
        //Serial.println(receivedChars);      
    }
}
void progBar() {

  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 100);
  
  Serial.print("j0.val=");
  Serial.print(val/2);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);  

  s0.addValue(0, val);

  if (val > 50){
  Serial.print("p2.pic=57");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  }else if  (val < 50){
  Serial.print("p2.pic=56");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

}
}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

HI.
Did you use softwareSerial.h?
motti

jluarduino:
All right, I got it working buttery smooth and without any hiccups.
I changed the timer interval in the Nextion to 50 millis.
I used the third example in the page you linked, thanks alot.

Here is my final code:

#include "Nextion.h"

NexWaveform s0 = NexWaveform(1, 1, "s0");

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    nexInit();
    Serial.begin(9600);
    Serial.println("");
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(A0, INPUT);
}

void loop() {
    unsigned long curntMillis = millis();
    if(Serial.available()<1){   
    progBar();
    }
    recvWithStartEndMarkers();
    useNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void useNewData() {
    if (newData == true) {       
        String nexDat = receivedChars;
        String xval = getValue(nexDat, ',', 0);
        String yval = getValue(nexDat, ',', 1);
        String zval = getValue(nexDat, ',', 2);

if(xval == "as"){digitalWrite(11, LOW);}
        if(xval == "af"){digitalWrite(11, HIGH);}
        if(yval == "bs"){digitalWrite(10, LOW);}
        if(yval == "bf"){digitalWrite(10, HIGH);}
        if(zval == "cs"){digitalWrite(9, LOW);}
        if(zval == "cf"){digitalWrite(9, HIGH);}
       
        newData = false; 
        //Serial.println(receivedChars);     
    }
}
void progBar() {

int val = analogRead(0);
  val = map(val, 0, 1023, 0, 100);
 
  Serial.print("j0.val=");
  Serial.print(val/2);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

s0.addValue(0, val);

if (val > 50){
  Serial.print("p2.pic=57");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  }else if  (val < 50){
  Serial.print("p2.pic=56");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

}
}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

There are many ways to "code" serial data collection - ranging from simple to what I call "convoluted".
Eventually it works , but why complicating things ( Google "KISS" ) ?

Check Resources -> Serial for functions collecting data from Serial buffer using passed parameters such as checking for terminating character, expected length of data to receive and also (optionally) returning timeout error of -1.

PS
OT
If String causes "problems" in Arduino - could it be Arduino "problem"?
It would not be first Arduino IDE anomaly.
Did not Arduino IDE / .ino had issues with #define?

I have prepared series of video tutorial based on Nextion display. Here is channel.

While inhterfacing with arduino, you don't requires any stack or library. You can create your own. I have explained all the toolbox.

Following video shows how to interface with arduino and shared the code too..