Seithan / EasyNextionLibrary

A simple library for Nextion display that uses only four functions. You can easily benefit from Nextion's wide range of features and advantages in just a few easy steps. The library uses a custom protocol that can prove to be a powerful tool for advanced users as it can be easily modified to meet one’s needs.
MIT License
116 stars 29 forks source link

Intermittent triggerX() detection and currentPage #45

Closed PeterBowater closed 1 year ago

PeterBowater commented 1 year ago

Hi!

Hope you can help, I've been working on a digital dash project and came across your library which makes things WAAAY cleaner for my use.

Firstly, a quick summary of the hardware/software:

Firstly, I was having trouble getting the currentPage value, and when trying to get it using the global dp variable it was REALLY slow, so abandoned that and went looking for a different solution.

My idea was to trigger a function by getting the Nextion to send a hex string as documented such as :

printh 23 02 54 01

as per:

Screenshot 2022-12-29 at 15 18 27

01 for page 1, 02 for page 2 on the button touch release event. That appears to work fine in the Nextion simulator/debug, and sometimes it works OK when changing page, the Arduino picks it up and will set the global Arduino Int to the current page, which I push out to the Nextion for testing inside the loop().

Sketch as an example:


#include "EasyNextionLibrary.h"

EasyNex myNex(Serial);

int currentPage=1;

void setup() {
  myNex.begin(115200);  
  delay(500);  // This delay is just in case the nextion display didn't start yet, to be sure it will receive the following command
}

void loop() {  
  myNex.NextionListen();
  myNex.writeStr("gearposition.txt", String(currentPage)); // these two fields are extant in the dash, just using for debug here
  myNex.writeNum("pageNum.val", currentPage);
}

void trigger1(){
  currentPage=1;
}
void trigger2(){
  currentPage=2;
}
void trigger3(){
  currentPage=3;
}

I've reduced the code to a minimum above (and have tried this here as well) with the same results, it doesn't always update the page number, I'll sometimes end up with page three showing '1' or '2', page one showing '2' or '3' etc.

Am I doing something wrong? Any ideas?

Thanks very much!

Peter.

PeterBowater commented 1 year ago

Realised I ought to add why I was having trouble with myNex.currentPageId, using the same code above, replacing the writeStr/writeNum values with:

myNex.currentPageId

Results in a 0 every time.

PeterBowater commented 1 year ago

Also after some further experimentation:

currentPage = myNex.readNumber("dp"); if(currentPage != lastCurrentPage){ myNex.writeStr("gearposition.txt", String(currentPage)); myNex.writeNum("pageNum.val", currentPage); lastCurrentPage = currentPage; }

Results in alternating -1 and actual page number. Is -1 a default return if undefined/not completed yet?

Seithan commented 1 year ago

Try this first NOTE: As these commands are using the Serial port to read and write, it is more preferred not to run them in the loop() without delay(); or some other method of not running them with the frequency of the loop and use them only when it is needed. Using them in a loop, a delay in the loop can be noticed, especially when reading from the Serial. A Serial buffer overflow can also be caused. Also NOTE: (from the Nextion Editor Guide)

https://github.com/Seithan/EasyNextionLibrary#function-documentation

Seithan commented 1 year ago

https://github.com/Seithan/EasyNextionLibrary#library-public-variables