Start a new topic

Arduino Communication protocols

Hi everyone. I'm hoping it's possible somehow to either read or write the system variables in Nextion from the Arduino. E.g: DIMS. 
What I'm trying to achieve is to know when the Nextion has entered/exited sleep so my sketch can enter/exit certain loops accordingly.

If not, it says in the instruction set that even in sleep, the Nextion sends serial data of x,y coordinates pressed, but not how to get this info. This would also be useful if the above isn't possible. I've tried Serial.readstring() to find this but it shows up blank.
Many thanks

You will need to do more coding in your local Arduino library files to accomplish this

Currently the posted library capture the touch event 0x65 Nextion Return Codes.

This provides Pressed Events via .attachPush and Released Events via .attachPop

The same methodology would need to be implemented for the Coordinate 0x67 Nextion Return Codes.


The way to trap the 0x67 

where [0] is 0x67  [5] is 0x00 and [6] [7] and [8] are 0xFF

  [1]*256+[2] is x  and  [3]*256+[4] are y ...

remove [0] to [8] as they are being processed and have been received

send x,y as params to your function


Hi, thanks for reply, but all of what you said is completely over my head. I don't know anything when it comes to modifying libraries and am really struggling to make sense of any of what you said. I don't really need to know the coordinates, just to know if the screen has been touched or not. I may go another way and just have a hotspot covering the whole screen and just set DIM to 0 with a timer and variable, as SLEEP seems to be a bit buggy - making the Arduino slow down when entered.

Hey Dan

I do apologize.  The second part of your question suggested you wanted to know how to get the x,y coordinates when the sendxy command had been initiated - as per the Nextion Instruction Set. 


As for receiving numeric values outside of the functionality currently provided in the IteadLib, it unfortunately follows the same kind of principle.  In the Nextion Instruction Set, when the first byte is being received ...

if 0x65 - this is touch event data ... as this is currently what the IteadLib is trapping.

To get the touch coordinates, which is not part of the current functionality of IteadLib then changing some code in the local library is necessary, as the touch coordinates first byte is 0x67.

Likewise to receive numeric data needed for things like the value of system variables such as dim and sleep, you would need to change some code in your local library to trap the numeric data as these start with the first byte of 0x71


If you were trapping such additional return codes

you could use sendCommand("get sleep") to retrieve the sleep value, 0 for awake and 1 for sleeping.


You might not understand everything right away, and that is alright.  Most things have learning curves.

Programming in Arduino is based on the C languages.  Your Arduino sketch is a C program that looks for a format where there are at least two functions setup() and loop().  A library is merely some starter code that has extra functions already defined to help get you started quicker.  But it is all C programming.


To understand the Nextion communications

Your mcu sends commands to the Nextion mostly in text.

get sleep

t0.txt="How are You"

n1.val=153543

Almost all commands are followed by three bytes of 0xFF hexadecimal or 255 in decimal.

When the three 0xFF bytes are received this tells the Nextion to process the command before them.


Depending on the command there are various answers the Nextion might send back, and each have their own little format.  Your mcu programming should be capturing each byte that is being received from the Nextion and storing them until the reply is terminated by the three 0xFF bytes.  At this time, you can discard the last three bytes and start processing what was received before those three bytes.


In the Nextion Instruction Set, there is two charts with First Byte of Returned data.

This is letting you know what each kind of message contains, and how it is formatted.

If your program is looking for various types of messages to work with, then you trap them, and once you have decoded their contents, forward it to a function you have written for working in your program.

You must also include the trap function into your listen list so your MCU can be expecting this message type and which function to execute when the message type comes in (So, the nex_listen_list function is something like a glorified switchboard operator which will direct the message to your function)


In the case of the IteadLib, the largest of these groups, 0x65 Touch Event return data as been done for you already.  These listen for the SendComponent ID return codes inside your Nextion HMI design.

If you have a button, and in its Touch Pressed Event you check the SendComponent ID box, then the Nextion will send out the 0x65 Touch Event Return data for that button. 


If in your Arduino code, you set up your button into the nex_listen_list and register the button's .attachPush event to point to your function, then ...

When a user presses your button

Nextion sees the SendComponent ID checked

Nextion sends 0x65 0x00 0x02 0x01 0xFF 0xFF 0xFF // assumes the button was component 2 page0

Your Arduino receives this message

your MCU directs the message to your function for .attachPush for that specific button

where you can have your program do whatever is needed when the user presses that button.


This is a brief overview, I hope it is helpful

Thanks for all this info, very helpful. I didn't know of certain things which you have mentioned as there is much documentation missing from the tutorials and example sketches. I would like to know for example how to properly retrieve and set Nextion variables.

For instance, I know how to set NexPicture, NexText NexHotspot etc, but what is the commend for declaring a variable that I have set in my HMI?

If I could do something like

brightness.val = 15; in my Arduino sketch, this would solve everything.



Also, the  sendCommand("get sleep"); in Arduino... what does this do exactly?


Thanks for all your help. I have attached a picture of my project which is nearly finished. It controls my radiator with a remote control.

IMG2013.jpg
(300 KB)

What the sendCommand does is send to the Nextion the text within its first (and only) parameter, and automatically as part of this function, adds the three 0xFF bytes needed to truncate the command.

So, if you used SendCommand("brightness.val=15") it would send what's in quotes + the three 0xFF bytes.

If you have a numeric variable on the Nextion called brightness, this would set that variable to 15.


If you used SendCommand("dim=15") it would send what's in quotes + the three 0xFF bytes.

This would change the dim system variable to 15, and in effect change the TFT backlight to 15%.


If you used SendCommand("get sleep") it would send what's in quotes + the three 0xFF bytes.

This requests the Nextion to send the value of the sleep system variable to the MCU in 0x71 numeric return codes.

As this is either 0 when awake and 1 when sleeping, the following the bytes would be sent:

0X71 0X00 0X00 0X00 0X00 0XFF 0XFF 0XFF  // the number 0, for a sleep value meaning awake

0X71 0X01 0X00 0X00 0X00 0XFF 0XFF 0XFF  // the number 1, for a sleep value meaning sleeping

For variables look at the variables.h and variables.cpp files in your library and you see how it gets done.

OMG:

SendCommand("dim=15") is perfect! This is exactly what I needed. It's a shame this command isn't included in any example Arduino sketch or any documentation I could find as it's such a powerful and useful command for Arduino. I've tried what you said and It also works when you convert numbers to a char[] and use a For loop to have a screen fade effect.

Thank you very much!

It IS documented.  The library code is not something that is to be ignored in the background, but a source to see how things are being conducted.

But there are about 35 files in the ITEAD library folder. Which one reveals the commands such as this? I've looked at most of them and couldn't find anything useful.

 Hi Dan,


nobody ever claimed, that coding is an easy task :-) Seriousely, a good point to start is always, to read all available documentations. Very often, needed info is not concentrated at one place, morelike spread over many different documents and documentations. MSDN contains a few 100000 documents ... what a pain to find exactly what you may look for ...

The Nextion pdf contains all info which is needed. The Arduino library does nothing more, than to implement that described commands, more or less good. So, when reading the doc, the next step would be to just read one tiime the library source. Many things you maybe don't understand, but after reading the command set inside the normal doc you also will find and understand many common inside the sources ...


Just a few considerations



Gerry

The sendCommand is defined in NexHardware.h and its function is in NexHardware.cpp.


At 35 files it is relatively quite small. You will run across libraries that are 1000's of files.

Okay, so I looked in NexHardware.h and see it, but looking at this does not describe proper syntax or the parameters of the command. I would never have known that you could put "dim=0" inside quotes and it would set the system variable of the screen.

I have scoured the internet before now with others asking questions like this and they never got the answer either. It wasn't until I decided to create an account here and you mentioned this command that I was able to use it.

You mention "The Nextion pdf"? - Please can you advise me where this is? I only have the instruction set on your website and example sketches  from Github as well as the fishtank example. None of these reveal this command.

Many thanks

Dan

Everything is well documented ...  even the dim and dims command  :-)

https://www.itead.cc/wiki/Nextion_HMI_Solution#Nextion_Instruction_Set
https://www.itead.cc/wiki/Nextion_Instruction_Set

When read and study any sourcecode, the sourcecode itself is your documentation. Any used function, any used sub already tells the reader, how to use in general, which parameters are needed and how to implement ...


Hopefully this helps


Gerry

Login or Signup to post a comment