DanNixon / NeoNextion

Arduino library for the Nextion displays
https://dannixon.github.io/NeoNextion
GNU General Public License v2.0
48 stars 34 forks source link

button callback not working #32

Open rangathedon opened 7 years ago

rangathedon commented 7 years ago

I have followed the example but button callbacks dont seem to work; Any idea? does anything special need to be done on the nextion editor to the HMI file to get callbacks working? This is both on arduino nano and mega;

DanNixon commented 7 years ago

Have you enabled the button press/release events in the Nextion Editor?

rangathedon commented 7 years ago

I have enabled the release event since I only need that; do both events need to be enabled? I am using the latest nextion editor;

DanNixon commented 7 years ago

That should be enough.

Not too sure of the issue then, I haven't used these displays for a while now and have no hardware to test with.

rangathedon commented 7 years ago

yeah, really strange; I checked the official library and it seems cumbersome to use but not sure where to head now;

On Wed, Aug 16, 2017 at 4:42 PM, Dan Nixon notifications@github.com wrote:

That should be enough.

Not too sure of the issue then, I haven't used these displays for a while now and have no hardware to test with.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DanNixon/NeoNextion/issues/32#issuecomment-322738607, or mute the thread https://github.com/notifications/unsubscribe-auth/AWd8IePH4xHGZ0oC5H8I-SJ8eIeqmmTwks5sYs6ZgaJpZM4O4BEB .

viktor1970 commented 7 years ago

I'm using this library since a while, no issues in button callback (or other components) Check the component ID in variable declaration, I experienced that 90% of times the issue is a wrong component id.

ranganathp commented 7 years ago

I figured the issue; it does work with simple examples but it does not work with examples where I read or write values from other components in the loop() method after nex.poll(); Not sure how to fix this issue; I need to be able to set values to some components in loop method and also be able to handle button click callbacks; Please share your insights;

viktor1970 commented 7 years ago

I experienced is not a good method to update components in loop: the serial will be always (or almost) committed to send data (anche check return values). In my projects, components update is always done either after a touch event or on a timer basis (using millis or TimeAlarm library).

rangathedon commented 7 years ago

but even in that case it would definitely conflict right??? Also in my case, data is from sensors so I have to either update it in loop or timer anyway and in arduino, loop is like running a timer itself;

On Sat, Sep 9, 2017 at 6:23 PM, Vittorio notifications@github.com wrote:

I experienced is not a good method to update components in loop: the serial will be always (or almost) committed to send data (anche check return values). In my projects, components update is always done either after a touch event or on a timer basis (using millis or TimeAlarm library).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DanNixon/NeoNextion/issues/32#issuecomment-328275522, or mute the thread https://github.com/notifications/unsubscribe-auth/AWd8IfvolnvQVInV6kqOq6zzxqCw8CA6ks5sgopBgaJpZM4O4BEB .

viktor1970 commented 7 years ago

Yes, loop is like a timer but, even with get sensor data and other operations, it's too fast. If you update data, for example, every 2-3 seconds there will be no conflict because the library (and the display) can deal with that amount of data speed.

In one of my project I've a timer on screen and, of course, it's updated every second (using TimeAlarms library, but will be the same using millis() ). The serial is set to 38400 (software serial, cannot be faster), and the touch buttons (stop, pause, restart) are working perfectly without any conflict.

viktor1970 commented 7 years ago

Another tip, I don't know if it's your case, is to update components only on the page currently displayed (even with components set as global). I've a variabile that tracks the current page (page switch is managed by Arduino) so if, for example, I'm on the page 2 it's not necessary to update date/time and sensor data on page 0. When I'll go back on page 0, the callback attached will call the function to update the display.

ranganathp commented 7 years ago

loop is definitely fast if we dont put a delay but it can be used like a timer with delays; I already have 2-3 second delays;

Also, None of the things you mentioned above are issues I am facing and I am already following those practices; I have even tried updating only one visible text field and the callbacks wont work even with setting or getting one value after a poll; This must be something to do with how its handled in the library for sure;

On Sat, Sep 9, 2017 at 6:57 PM, Vittorio notifications@github.com wrote:

Another tip, I don't know if it's your case, is to update components only on the page currently displayed (even with components set as global). I've a variabile that tracks the current page (page switch is managed by Arduino) so if, for example, I'm on the page 2 it's not necessary to update date/time and sensor data on page 0. When I'll go back on page 0, the callback attached will call the function to update the display.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/DanNixon/NeoNextion/issues/32#issuecomment-328277328, or mute the thread https://github.com/notifications/unsubscribe-auth/AFCFefVyJGCs0G-uWukQuus7P7ruIAgfks5sgpJJgaJpZM4O4BEB .

-- http://techniche.blogspot.com

viktor1970 commented 7 years ago

The most probable issue that can cause what you're experiencing is wrong ID component in variable declarations. But are you sure the display is communicating with Arduino? A simple code to show what Nextion is sending while(sr.available()) { Serial.print((uint8_t)sr.read()); }

Where sr is the serial attached to Nextion.

rangathedon commented 7 years ago

Buddy, I told you it is working fine if I dont write values to the display components or read values from them; I have fair amount of experience with this library and nextion display to understand and make sure the correctness of component ids, component names and page ids;

Please dont give speculative answers without really reading what I wrote; I clearly wrote it works if I am not getting or setting a component value in loop;

I will wait for the library author's response; You are welcome to provide a reasoning if any but please try out my scenario first; You can start with the Button example, just add one more number field on the screen and set or get its value in the loop and you will clearly see that the callback does not get fired no matter whatever delay you put in loop;

On Sat, Sep 9, 2017 at 8:30 PM, Vittorio notifications@github.com wrote:

The most probable issue that can cause what you're experiencing is wrong ID component in variable declarations. But are you sure the display is communicating with Arduino? A simple code to show what Nextion is sending while(sr.available()) { Serial.print((uint8_t)sr.read()); }

Where sr is the serial attached to Nextion.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DanNixon/NeoNextion/issues/32#issuecomment-328282444, or mute the thread https://github.com/notifications/unsubscribe-auth/AWd8IVcJQN-ZbdV-9Jmb8x9JujKx7cGhks5sgqf5gaJpZM4O4BEB .

rangathedon commented 7 years ago

Any insights Dan?

viktor1970 commented 7 years ago

I did: 3 buttons, 3 number field and 2 text field. 2 numbers are updated every seconds by RTC; as expected, the callbacks ARE fired, so there's no issue on the library.

Here's the video: https://youtu.be/ozAMaVkeW9c

And here's the code:

#include <SoftwareSerial.h>
#include <Nextion.h>
#include <NextionNumber.h>
#include <NextionText.h>
#include <NextionButton.h>
#include <RTClib.h>

RTC_DS3231 rtc;
SoftwareSerial sr(3,2);
Nextion nex(sr);

NextionNumber n0(nex,0,3,"n0");
NextionNumber n1(nex,0,4,"n1");
NextionNumber n2(nex,0,6,"n2");
NextionText t1(nex,0,8,"t1");
NextionText t2(nex,0,9,"t2");
NextionButton b0(nex,0,1,"b0");
NextionButton b1(nex,0,2,"b1");
NextionButton b3(nex,0,7,"b3");

unsigned long tm1;
char bf[20];

void cbData(NextionEventType type, INextionTouchable *widget)
{
 if (type == NEX_EVENT_POP)
 {
  sprintf(bf,"Val: %d",n1.getValue()); t1.setText(bf);
  sprintf(bf,"%d",n0.getValue()+n2.getValue()); t2.setText(bf);
 }
}

void cbUp(NextionEventType type, INextionTouchable *widget)
{
 if (type == NEX_EVENT_POP) { n2.setValue(n2.getValue()+1);}
}
void cbDown(NextionEventType type, INextionTouchable *widget)
{
 if (type == NEX_EVENT_POP) { n2.setValue(n2.getValue()-1);}
}

void setup() {
  rtc.begin();
  sr.begin(38400);
  nex.init();
  Serial.begin(115200);
  b0.attachCallback(&cbData);
  b3.attachCallback(&cbUp);
  b1.attachCallback(&cbDown);
  tm1=millis();
}

void loop() {
 nex.poll();
 if((millis()-tm1)>1000)
 {
  Serial.println(rtc.now().second());
  n1.setValue(rtc.now().second()); n0.setValue(rtc.now().minute());
  tm1=millis();
 }
}
DanNixon commented 7 years ago

@rangathedon You are probably expecting things to happen much faster than what they can. Nextion displays are really slow.

There are not many reasons to be updating values periodically let alone in the main program loop, save this for when the values actually change (or at least change a sufficient amount to be relevant to the user).

Even if you do your updating periodically I'd guess you have a cluster of update calls which is probably leading to the display not reading the full payloads of subsequent messages.

Without looking at your code it is difficult to tell and the best I can offer is speculation.

If you really need a value to be updated at less than 1 second intervals then Nextion displays are not the solution.

viktor1970 commented 7 years ago

@DanNixon you're right. That's the same I wrote (loop too fast); I modified my code above: it works even with a 100ms interval, below that value it's unresponsive.

But @rangathedon wrote he has 2-3 seconds delays (I hope not delay(2000) ) so my thoughts is an error in his code, since no one else has reported this "issue".

kebien6020 commented 5 years ago

I had a similar problem and in my case it was because I did a page x command in the Press (Push) event on the Nextion editor, and was listening for the Release (Pop) event on the arduino program. It never triggered since the screen was no longer there.

Moving the code from the Press event to the Release event in the editor solved it for me.