Hi
I am busy with a project that requires communication between an arduino mega and a nextion display. I am quite new to working with arduino. I got my nextion to send data to my arduino that I can process, but when I tried to send instructions back to the nextion, I got no response. I checked various help pages, forms and video and I am on the point of thinking that it might be a faulty RX pi on the Nextion’s side, but I want to be certain. Could you check and see if i missed something.
Arduino: MEGA 2560
Nextion: NX8048K070_011
boolean PickedSeed = false;
int m1 = 0;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("rest"); //
Serial1.write(0Xff); //
Serial1.write(0Xff); //I tried this format
Serial1.write(0Xff); //
}
void loop() {
while (PickedSeed == false) {
if (Serial1.available() > 0) {
String sFromDisplay = "";
while (Serial1.available() > 0) {
sFromDisplay += char (Serial1.read());
delay(5);
}
Serial.println(sFromDisplay); //For debugging
Serial.println(sFromDisplay.substring(0, 2)); //
if (sFromDisplay.substring(0, 2) == "40") { // identifies a button
Serial.print("in if");
PickedSeed = true;
m1 = 20 ;
Serial1.print("page2.n1.val=" + String(m1)); //I also tried this format
Serial1.print("\xFF\xFF\xFF"); //
}
if (sFromDisplay.substring(0, 2) == "41") { // identifies a button
Serial1.print(F("page2.n1.val=30")); //and I tried this format
Serial1.write(0Xff);
Serial1.write(0Xff);
Serial1.write(0Xff);
}
}
}
}
I edited the post. This was a mistake I made in the post of the question.
Serial1.print(F("page2.n1.val=30"));
The research I did all indicated that I should end instructions to the Nextion with "ff ff ff" in hexadecimal I think, and this is one way they proposed.
Before I asked the question I had a look at your post in my research. I read the read it again and tried to run you first example to test if my Nextion's receive port if functional. It sadly did not work (the text of the textBox 't0' did not change).
void setup() {
//This is for the serial monitor. Remove the // if you need to use it.
//Serial.begin(9600);
//This is for serial port 1, which is the one used for the Nextion in this demonstration.
//You might need to change this depending on which serial port you are using for your display.
Serial1.begin(9600);
//For demonstration
HMI_startup_message();
}
void loop(){
}
void HMI_startup_message() {
Serial1.print(F("t0.txt=\""));
Serial1.print(F("Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0"));
Serial1.print(F("\""));
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
}
I had to add the empty viod loop for the code to configure.
I just tried your code exactly as posted and it worked and displayed the message. I put the code on a Nano Every, not a Mega, but that shouldn't matter. I had to press the reset button before the message was displayed.
Are you using my Nextion configuration?
Do you have a text box with objname set to t0 (NOT T0)?
I suggest you take all my sample code and my Nextion configuration exactly as it is and try it, not just a bit of it.
If you are still struggling post some photos of your kit and wiring.
I think you might be looking at a faulty serial Rx, but equally there might be some mistake in the wiring or something like that.
I did not use your Nextion configuration, I assumed, for that code, I only needed a text box with the objname t0 (it is t0 not T0).
I only used basicks to try to isolate the problem so i only had that one text box and my wiring is only the tx to rx and rx to tx. I double checked it with your post
" • Red - for connecting to +5V
• Black - for connecting to 0V
• Blue - Transmit data from the display to the Arduino, connect to RX on the Arduino
• Yellow - Receive data from the Arduino to the display. Connect to TX on the Arduino"
I will try to upload an image but the preview does not look promising.
Thank you Perry, for all the help but as this is a time sensitive project and we have not come to any other answer to the problem, I will work around the nextions rx pin.
This was my first experience using this platform and you made me feel welcome and motivated me to ask more questions. I will definitely use this platform in the future.
paul112:
I did not use your Nextion configuration, I assumed, for that code, I only needed a text box with the objname t0
You are correct that you don't need the complete configuration, however, if you are having problems one thing to do is to start with something known to be good exactly as it is and work from there. My configuration works so use that as your starting point. If you just create a simple configuration there is the possibility of some small mistake that is critical and is the reason for the problem.
I have 5 (I think!) Nextion displays and never had a problem with any of them. I can't think of anyone reporting a problem with the serial port on here. I'm not saying the serial port can't be faulty, of course it can, just that I doubt that it is.
Everything you've said sounds right so far, but there is a saying about a picture and 1000 words so if you can upload a photo that would be good.
Thank you Perry, for all the help but as this is a time sensitive project and we have not come to any other answer to the problem, I will work around the nextions rx pin.
This was my first experience using this platform and you made me feel welcome and motivated me to ask more questions. I will definitely use this platform in the future.
You're welcome! I hope you get your project finished and working on time