hi everyone,
Iam using nextion display in my project. I have a little knowledge about nextion display codes.my aim
is to send a particular data to a text box in display when a particular button is pressed.I also got the result
successfully but my problem is the data is sent only for the first time a button is pressed,if I try to press
another button no result...Iam not getting where I did wrong.
Here is the code below
if(Serial.available() > 0){
int thisChar = Serial.read();
value += thisChar;
Serial.print("\ ");
Serial.println(thisChar);
}
if (value.length() > 0) {
if (value == "101030255255255")
{
value = "";
Serial.write("page0.t0.txt=\"65\""); //arduino to nextion set the text value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101040255255255")
{
value = "";
Serial.write("get page0.t0.txt"); // arduino to nextion get value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101110255255255")
{
Serial.write("page1.t0.txt=\"5\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101120255255255")
{
Serial.write("page1.t1.txt=\"1\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101130255255255")
{
Serial.write("page1.t2.txt=\"8\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
}
Hello vydehi,
It's impossible to help from a code snippet, please post you whole code, or better still a short program that illustrates the problem.
You don't normally need to specify the page in the data you send to the Nextion because it assumes the data is for the current page.
Beyond that, support for Nextion on these fora is pretty much as follows:
You can follow the methods I set out in using Nextion displays with Arduino. My methods do not use the Nextion libraries, so while I am happy to offer help on using my methods I cannot offer anything very helpful for any Nextion library.
The original Nextion libraries are full of bugs. There is a link from my tutorial to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.
There's also a separate Easy Nextion Library by Seithan, his methods are different to mine, choose which works best for you.
Beyond that the odd person occasionally offers a bit of help but not much.
String value;
int i = 0,n = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0){
int thisChar = Serial.read();
value += thisChar;
Serial.print("\ ");
Serial.println(thisChar);
//Serial.print("\xFF\xFF\xFF"); //101 0 3 0 255 255 255,101 0 4 0 255 255 255,101 1 1 0 255 255 255,101 1 2 0 255 255 255,101 1 3 0 255 255 255 unique code values of buttons
// value = "";
}
if (value.length() > 0) {
if (value == "101030255255255")
{
Serial.write("page0.t0.txt=\"75\""); //arduino to nextion set the text value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101040255255255")
{
Serial.write("get page0.t0.txt"); // arduino to nextion get value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101110255255255")
{
Serial.write("page1.t0.txt=\"5\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101120255255255")
{
Serial.write("page1.t1.txt=\"4\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else if(value == "101130255255255")
{
Serial.write("page1.t2.txt=\"3\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
}
}
here is the full code and yesterday when Iam using serial monitor for checking sent and receiving codes I observed one thing when I first pressed the button its particular value is sent to nextion text box correctly and when the second button is pressed nothing happened...but if I close and opened the serial monitor again press the second one its sending data...that means I have to close and open serial monitor for every button press event. It is not sending continuosly....why this weird behavior, Is it any coding mistake or am I doing anything wrong. please help 
That code does not compile so cannot be the code you are using.
I'm guessing you are using a Uno, which means the serial monitor and the Nextion are sharing the same serial port. This can work but is asking for problems, especially if you want to use the serial monitor to see what is happening. You can't have prints to the serial monitor that are separate from the prints to the Nextion.
If you are using the serial monitor on a Uno it will only be displaying what it sent from the Uno to the Nextion.
Ideally you should use something like a Mega with separate serial ports for the serial monitor and the Nextion.
Please describe what you think each section of code does. I strongly suspect that it does not do what you think it does.
PerryBebbington:
That code does not compile so cannot be the code you are using.
Actually I forgot to put closing braces in setup function while posting..but it was compiled.
PerryBebbington:
Please describe what you think each section of code does. I strongly suspect that it does not do what you think it does
I had taken a string variable 'value' in which we store unique code of button received when pressed in display.
if(Serial.available() > 0) or if any data has arrived in buffer to read then read the data and store it in string. print the data received from the display.
when the data is stored in string or if(value.length() > 0) compare the string value received with predefined unique codes of buttons.If the first one is pressed send 75 to "t0" text box in page0, for second button pressed get the value stored in "t0" text box and for other button send some value to either "t0" or "t1" or "t2" text boxes in page1 etc.,
like this I have to send some data when a particular button is pressed.
OK, here's what I am struggling with to help you...
It would help a lot if you had an Arduino with a separate serial port for the Nextion, you've not said what you have but I suspect it's a Uno from your code. As it is the prints to the serial monitor and to the Nextion get mixed up, which makes it difficult to diagnose what your code really does and means some of what is sent to the Nextion will be be stuff that should go to the serial monitor, which will cause problems.
Have you seen my tutorial using Nextion displays with Arduno? I linked to it above. In there you will find code that works. You can use it, modify it, do what you like with it always knowing that it's tested and works. It does work best on a Mega, not a Uno though.
Finally, you should not use String (capital S), I'm not going to go into details because there are lots of threads on these fora that explain what problems it causes and what to use instead. Do a search of the fora to find out more. In summary use C strings instead.