Unofficial Nextion/TJC User Forum

How to read a Nextion variable from a Arduino or Picaxe?

Hi,

I buid a DIY project to control my house’heater. The GUI Nextion works fine.

However, I am stopped, because I am unable to read a Nextion variable or a Nextion Textbox properties, from my Picaxe 20M2 with the serial.

What is the syntax for that. I see nothing in the Nextion documentation.

However, lighting a led from the Nextion Button works fine…

Many thanks for your help

Best regards,

RovDan

How are you sending the variable or textbox properties to the MCU?

For a project like this a set button would be the way I would do it.
With two text box for example
in the set button Touch Press Event(0)
prints t0.txt,0
prints t1.txt,0
When you press the set button nextion will send the text in the two text boxes

Many thanks Paul and Elf for your reply.

In the meantime, I spent all my evening studing and testing the “Get” instruction, and I successed.

To read a nextion number variable var1, whose content is 600 and located on page1 : my Picaxe must send for example :
Serout SortieNextion,T9600_16, (“get page1.var1.var”,255,255,255)

The Nextion reply is : 113,88,2,0,0 (in decimal, I prefer than in Hex)
113 is the Nextion Code of the decimal reply ($71)
88 is 600 mod 256
2 is 600/256
0,0 are used only on greater variables. Nextion allows to send up to 4 bytes numbers.

To read a nextion Button Caption Bn1, whose caption is “ON” and located on page2 : my Picaxe must send :
Serout SortieNextion,T9600_16, (“get page2.Bn1.txt”,255,255,255)

The Nextion reply is : 112,79,78 (In decimal)
112 is the Nextion Code of the text reply ($70)
79 is ASCII of the character “O”
78 is ASCII of the character “N”

One more time, thanks for the replies. I go on to my heating control project.
Maybe, I posted my messagein the wrong Thread. If somebody can move it…

Best regards,

RovDan

If you have a number of text boxes you would need to send a lot of requests.
If you created a timer that is not enabled and put the prints in the timer
all you need to do is enable that timer , in the timer code you disable it at the end.
There are a lot of examples in my How to use Reparse mode.

Hi Paul, It’s a good idea to use a timer to enable number of objects. I note this tip !

Today, I went on my project. I noticed that sending a request for read any variable works (see upper) … only if this variable was a “global” variable. It seems not working when the variable is “normal”.

Useless to say, I spent a lot of time before understanding this…

Best regards,

RovDan

Its a long road to learning about these displays as the information is so little.
They only expect a page to be used while on display and globals use up ram where other variables free up ram when not in use. The display can do a lot of the converting of variables , maths and logical work before sending it to the serial port. In my case I completely did away with needing another micro in between my solar inverters and the display.

Hi everybody,

I discovered the “Get” function a few months ago. It works fine… However, designing another project ( a DIY clock with a Picaxe and a DS3231), sometimes, the Picaxe read unexpected bytes.

Below is my Picaxe code :

Serout SortieNextion,T9600_16, (“get page4.Hr.val”,255,255,255)
Serin [8000, timeOut], EntreeNextion, T9600_16, Oc0, Oc1, Oc2, Oc3

I spent a whole day do find what was wrong. With the help of the Picaxe Terminal, I could see the Nextion reply was good. However, the picaxe was unable to read properly the first byte of the reply. The three other bytes were properly read.

Then, I discovered that the Nextion reply was to speed after the Picaxe request. It seems the Picaxe was not ready when the reply came.

I resolve the problem by cancelling the first byte and using only the last three bytes. It works fine now. Another way was to overclocking the Picaxe up to 32Mhz. I would wish to avoid this overclocking for a poor basic clock !.

My question :

Is there a way to add a little delay between request (get) and the reply ?
I try using the print, prints instructions, too and there is not big differences.

Many thanks for your tips.

RovDan

To me this sounds like a problem that needs to be solved on the Picaxe side. It‘s hard to believe that there‘s a microcontroller environment that‘s unable to handle 9600baud/s serial without missing bytes.
If you‘ve other stuff running ‘simultaneously’ try reducing the code to the bare minimum. As I said, I‘d be surprised if such a micro was unable to do simple (slow!) serial IO.

Edit: to answer the question, no, I don‘t know any practical way for delaying the response - except for trying even lower baudrates (maybe).

Kind regards,
Max

I agree with Max, here. I’d go a step further and say that if a MCU (or its OS in the Picaxe case) is not able to keep up with 9600 baud, although the underlying PIC could do much better without the Picaxe firmware, it’s simply time to abandon this unreliable platform and to turn towards something more efficient.

By using a timer to send the value you can put in a delay , you enable the timer with the pic and disable it in the timer code. The other way is the reparse mode where you take control of the nextion then you may not even need the pic.

Thanks for your replies,

This morning, I tested the “Timer system” with 20 ms.

First I set a Timer1 (20 ms, global) on my main page, Page0
The problem is that this global timer is unknown on the three others pages (no error messages !)
So, I decided to set a Timer1 (20 ms, local) in each of the 4 pages, with the same name…
In each of these timers, the event is :

Timer1.en=0
get Page0.Statut.val

My Picaxe code is :

“Timer1.en=1” (whatever the current page is)

… eureka, it works fine. I can see at my scope the 20 ms between request and reply !!
and the poor and slow Picaxe can do its job properly…

Have a nice day…

RovDan

Adding for my previous message, it seems that the minimum value of the timer control is 100 ms. It is not annoying in my case.

This said, Mister Nextion would have added a delay property to any request would be a good idea, I think.

Finally, all works like I wanted. Useless to overclocking my poor Picaxe :slight_smile:

Best regards,

RovDan

Yes best to treat all objects on each page as only in use when that page is up.
You can though set up a page with global variables and access them from any page
this I did as a test with a graph object, pages can continue to update the variables
and when the graph page is loaded code loads them to the graph but this has
the disadvantage of using a lot of ram and I had to set up 4 pages with global
variables as there is a limit of 256 per page.
Also have a look at the reparse mode it reduces the load on the external micro and
can give the same result for you as you have control of the serial of the nextion you could then have a delay for all serial on all pages.


This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).