Using Nextion displays with Arduino

PerryBebbington:
If you mean you want to send new line to a Nextion text box then I think you are wasting your time, I don't think they support it. (If I am mistaken and you know for sure that they do support it then I'd like to know). As far as I know the text boxes treat all text as a single line. Where I have needed multiple lines of text I have used multiple text boxes.

I'm not sure if this answer is what you are looking for, but there is a function right below txt_maxl called 'isbr'. Set to true, it allows, if you expand the text box down, at least text wrap. Not sure (haven't tried it myself) if it can be addressed directly from the MCU with a line break. Also not sure on how to address the new 'multiline' function from the outside.

Do you, or anybody else out there, know how to address the new xFloat function (sending a float number to the Display)? Couldn't find anything in their instruction set and NexNumber doesn't seem to work. (solved it for now by converting it to .txt)

I cannot help with anything related to the Nextion libraries, although there are others on here who might be able to help with them.

I tried:

x0.val=4567

Which works.

Sending data to the MCU with a button function followed by a page change isn't working. Had to place two buttons, one to send the data and the second one to change the page. Almost drove me nuts before I figured out what the problem was. If I remember it correctly did you have the same or a similar problem. What was your solution. Preinitialize? If so, how does it work?

Assuming you are using my methods look in the sample '2019-06-08 Arduino Nextion demo 43V1.zip' at the start of this tutorial; there are buttons to change the page. The button itself changes the page. On the new page under post initialisation event is the code to tell the Arduino that you have changed pages. The Arduino then responds with whatever data you want on the page. The reason for this is you have to change the page first, then wait for it to initialise then send whatever data you want on it. Putting the request to the Arduino for the data under post initialisation event guarantees that the page will be ready by the time that the data arrives. You can't have a button on page 0 that puts data on page 1 unless the variables on page 1 are global. The variables on page 1 are otherwise out of scope while page 1 is not displayed, so sending anything to page 1 from another page will not work. Sending data to a page before it has had chance to initialise will result in lost or errored data.

I'm not sure if this answer is what you are looking for, but there is a function right below txt_maxl called 'isbr'. Set to true, it allows, if you expand the text box down, at least text wrap. Not sure (haven't tried it myself) if it can be addressed directly from the MCU with a line break.

Yes, thanks, aware of that. I think what was being asked for was something that did new line within a text box. The old version certainly does not respond to anything with a new line, it just allows one long line of text which can scroll across multiple lines in the text box, but it is only a single line of text.

Also not sure on how to address the new 'multiline' function from the outside.

Me neither, please report back!

Oh, and ++Karma; for being so helpful and asking intelligent questions on your first visit to the forum.

PerryBebbington:
My pleasure! Glad to be of help and I appreciate the feedback :slight_smile:

I hate to bring back an old issue, but something I've just noticed (maybe the update changed something, or I've just not realized for this whole time):

When I send data in the fourth line of the serial out from the nextion (after index #) using "print t0.txt" or "print h0.val", and then I read it with HMI_read_data[4] and print it back out, I'm getting two digits that appear the same each time.

I have an h0 scrollbar with values from 500 to 5000, yet every time I print the value over the serial on release, I get a number between 41 and 53. Is this part of a hex code I'm not decoding properly? I'd like to convert it to a proper int.

I've read that several times and I am not clear what you are doing. Can you give me both the code on the scroll bar and the corresponding code on the Arduino please?

HMI_read_data[4]

Is 1 byte. You need 2 bytes for a range of 500 to 5000.

My heating controller has sliders to control the temperature setting with a range of 0 to 300. That gives me 30 degrees range in steps of 0.1 degree (yes, I know, I probably should be using 0 to 299). That means that the data sent from the sliders takes 2 bytes. The section of code that takes the slider values and updates the set temperature is this:

case 1:
       zone[HMI_read_data[3]].settemp = (HMI_read_data[4] | HMI_read_data[5] << 8) * 0.1; //Note 1
       zone[HMI_read_data[3]].flags_HMI |= HMI_text_settemp_update; //Note 2
       break;

Note 1, this is the update for the temperature setting:

(HMI_read_data[4] | HMI_read_data[5] << 8)

Combines 2 bytes into one integer, which is the value of the slider setting. I think you need something similar.

Note 2, this is a flag to tell the rest of the program that there has been an update

Although not the cause of your problem, I suggest you don't use that range. I don't know which Nextion you are using, but the highest number of pixels in one direction is 800, so the maximum possible number of unique values from a slider is 800. Any more than that cannot be resolved on the display, so while you might get meaningful values with a slider with a range of 4500, there must, I presume, be gaps in the numbers.

While 500 to 5000 should work from a software point of view, my suggestion would be to use 0 to 4500 and add the 500 in on the Arduino.

In case you are interested, this is another way to combine 2 or more bytes into and integer, or split an integer into bytes, something like:

union {
  struct {
     uint8_t data_0;
     uint8_t data_1;
  }
  uint16_t data;
}

If you write to data_0 and data_1 the two bytes will appear in data as a 16 bit int, if you write a 16 bit int to data then you can read the corresponding 2 byes from data_0 and data_1. You can of course expand this for larger ints and for floats.

I used the version in your demonstration file, it worked perfectly - I don't know why I didn't catch that before - I probably got lucky the first time I did it because that slider only goes from 1 to 6.

Got a bit 'side tracked' in the endless world of coding... especially as a newbie, sometimes it can be quite humbling and overwhelming looking at all those doors opening up in front of one's continuously growing eyes...

In a severe fit of megalomania I tried to write, well, re-write the NexNumber lib to a NexFloat and ended up in a very deep rabbit hole. took me a while to find my way out again, but got it from an endless list of errors down to a couple... and slowly a slightly broader spectrum of to this never ending subject.

But it 's definitely my turn to pass the flowers to you, Perry, for the help you are providing here. Nice little hints you are passing on, especially the last ones I found quite interesting, since it sort of unlocked a whole lot of new possibilities and you deserve every bit of Karma you can get for things like that...

Was using the Nex's own libraries and was a bit too lacy to re-write all the work I finished so fart, with your sample code in your first postings (nicely done, by the way). But I came to the conclusion, well better rephrase that, I was lucky enough to get my fit under control... ;), and now I'm humbly going to use your code and hope I can get back to you, in case I don't get it straight...but all in all work is progressing, even if it isn't 'quite' as fast as I first thought it might happen...but I find if always very satisfying if things slowly start to do the things they should.

Knowing now that I'm at least capable of asking reasonable intelligent questions, I'm glad I found somebody who is helpful enough to answer them. Thanks a lot for that. Hope I won't have to bother you too much.

I presume that other people on the 'big search' are passing by here as well, so I thought I might pass on a little helper to the Serial.print subject, simple enough that even I could have thought of it... :D, well, I didn't...but going to pass it on anyway, since it safes time and memory...

void nexDataSend() {
  Serial2.write(0xff);       // We always have to send this three lines after each command sent to the nextion display.
  Serial2.write(0xff);       //use the Serial(x) you are communicating with your Nex
  Serial2.write(0xff);
}

...and I find it quite handy, maybe it helps someone else as well.

will try my best to surface once in a while and pop by... :wink:

kind regards

Thank you Orion, that makes helping you and others worthwhile :slight_smile:

I have added to part 4 'Nextion additional features', which brings several different things together in one place. Enjoy!

@PerryBebbington et. all. I posted this in another section but not sure it will get attention there.

I have not yet been successful in understanding the communication from the Nextion. I have a few pages that are set up and I only need to capture the string that is sent when pages changes. from this I will create a CASE statement and execute accordingly .

For instance, when I am viewing the debug, and I am moving from page0 to page1, I get an simulator output (I am assuming I will see this as a serial string?) of "65 00 02 01 FF FF FF"
So I have recorded all of these strings associated with the page movements and created a table of string, pulse and direction. This will not be a high frequency event. (it is a shifter for a car)

Can I just use a simple serial program to read the string and then act upon the string in a CASE Statement?
Thank you for any assistance you may be able to render. I have some pictures but dont know how to or if I can post them

Hello altitudeap,
Sorry for the late reply, I have been on holiday :slight_smile:

For instance, when I am viewing the debug, and I am moving from page0 to page1, I get an simulator output (I am assuming I will see this as a serial string?) of "65 00 02 01 FF FF FF"

Yes, what you see in the simulator window is what is sent to the Nextion serial port. I would hesitate to call it a 'string' as that words has specific meaning in the C and C++ languages, and I don't think this data qualifies. Those bytes, as shown in the simulator window, will be sent to the serial port.

It is clear from the rest of your question that you have not tried my examples as set out in this tutorial, the answers are in there. Try the examples, then come back when you need further explanation.

(deleted)

Hello Orion444,

Thanks very much, much appreciated :slight_smile:

Very clear and well explained! Does anyone know if it's possible to move objects during runtime? I'm unable to figure out a way to set X and Y variables dynamically. Any suggestions?

Very clear and well explained!

Thank you :slight_smile:

Does anyone know if it's possible to move objects during runtime? I'm unable to figure out a way to set X and Y variables dynamically. Any suggestions?

The basic and enhanced versions do not support moving objects during run time. I'm not sure about the intelligent ones, I have not bought one.

If you look at attributes in the right of the IDE you will see that some of the attributes are in green and some in black. You can change the green ones during run time.

You could create multiple pages with the same objects in different places, or you could create multiple backgrounds with the same images in different places and use the cropped image feature to select the one you want.

Following on from a forum question a few weeks ago I have added to part 4 'Nextion additional features' a method of measuring how long a button has been pressed for. Pressing and releasing a button displays the duration of the press in milliseconds. You could use this to measure the length of a button press, then if it is less than some duration do one thing, it if it more than the duration do something else, giving different responses to short an long presses.
(If it was you who asked the question and you would like credit here then please send me a PM).

I have also identified and fixed the cause of the bug that crashed a Uno or Mega (and possibly others) when the scrolling list page was selected.

Please test these examples and let me know on here if you find any problems or have any suggestions for additional features.

Thank you.

I have updated part 4, I have added to Nextion additional features:

  • A simple calculator
  • A means to send the Nextion return codes to the serial monitor.

Enjoy.

For anyone active in this thread, in particular Perry and Ray: Thanks a million for these details!
This is incredible valuable (as well as the other threads).

Getting the screen to work was a struggle earlier, but now I got the hang of it. In fact I've been building an application containing 5 Nextion pages, communicating from and to the Nextion display successfully.
From the very beginning of my coding I’ve been using the library Nextion provides.

Question 1:
I noticed Perry and Ray Livingston have alternative approaches, which I'm willing to read and understand.
Unfortunately, I cannot find the alternative library Ray created, some help pointing that out to me is highly appreciated.

Furthermore: I ran into the issue doing everything sequential, using the Arduino loop.
To get around that I'm changing my code into a state machine solution at the moment.
For that to proceed I’d like to get (see) the serial communication content from the Nextion screen to the Arduino. The state machine allows me to read the serial communication, but I need to know what the screen is sending to allow me to filter on incoming data.
My best guess is to use the details the Nextion simulator is giving back, like: 65 00 01 00 FF FF FF or
e\x00\x01\x00ÿÿÿ.

Question 2: Is there a way to change the Nextion.h serial configuration (or elsewhere) in a way my serial window shows the incoming serial data? Or suggest me another way of looking into and understanding the serial communication details the Nextion gives me?

My functionality depends highly on the Nextion screen buttons and sliders. So handing me a suggestion how to filter the serial data solves my showstopper at the moment.

Thank you

For anyone active in this thread, in particular Perry and Ray: Thanks a million for these details!
This is incredible valuable.

Thank you :slight_smile:

Unfortunately, I cannot find the alternative library Ray created, some help pointing that out to me is highly appreciated.

I don't understand this, there is a link to Ray's tutorial on the very first post of this tutorial, on the first page of Ray's the files are there, waiting for you to download. I've just checked, the link works, the files are there.

Is there a way to change the Nextion.h serial configuration (or elsewhere) in a way my serial window shows the incoming serial data?

That (Nextion.h) suggests you are using the Nextion libraries, about which I know next to nothing. Ray might know.

Look at part 4 of this tutorial and download 'Nextion additional features'. In there I've added code to take return data from the Nextion and send it to the serial monitor. This is, of course, designed to work with my methods not the library.

My functionality depends highly on the Nextion screen buttons and sliders. So handing me a suggestion how to filter the serial data solves my show-stopper at the moment.

That should be simple with my methods; Nextion additional features has 5 pages including buttons and sliders, all working together.

I have found this incredibly helpful:

https://forum.arduino.cc/index.php?topic=634237.0

With it and what I have learned here I have a working project. Thanks Perry!