Nextion - triggering a click event

Hey friends

You can trigger a event in Nextion via <<click "thingToBeClicked",0>>.
So my idea was now, that if my page was opened, the button should be clicked automaticly and my ESP-code should be execudet. So i am now a little bit confused.
When i open the page, the click event for the button (initValStartPopCallback) gets triggered, BUT my code in my scetch isnt execuded.

Does anyone know why this is the case

Here is the code:

#include <Arduino.h>
#include <Nextion.h>
//SD-card
#include "FS.h"
#include "SD.h"
#include "SPI.h"
//RTC-clock
#include <Wire.h>
#include "RTClib.h"
//ESP32-Eprom
#include "EEPROM.h"
//Classes
#include <Classes.h>

/****** Definition of NEXTION-objects*******/
//buttons
NexButton initValStart      = NexButton(0, 2, "initVal");   //page initialization
NexPage displayInit = NexPage(11, 0, "Display");
//numbers
NexNumber brigtNs = NexNumber(11, 7, "n0");



/******Declaration of Nextion-Callbacks********/
void initValStartPopCallback(void *ptr);
void displayInitPopCallback(void *ptr);

/**** all Nextion objects that need to be listened */
NexTouch *nex_listen_list[] =
{
  //buttons
  &initValStart,
  //pages
  &displayInit,
  NULL
};

/******Definition of Nextion-Callbacks*****/
//Init-button. Will be clicked automaticly to initialize saved values from ESP32 EPROM
void initValStartPopCallback(void *ptr) 
{
  uint32_t f;
  brigtNs.getValueGlobal("Display",&f);
  currentSettings.brigtness = f;
  Serial.println("lllll");
}

void displayInitPopCallback(void *ptr) 
{
    uint32_t tbrightness;
  brigtNs.getValueGlobal("Display",&tbrightness);
  currentSettings.brigtness = tbrightness;
  Serial.println("HHHHH");
}


void setup() {
nexInit();
  //Serial COM
  Serial.begin(9600);
   // while the serial stream is not open, do nothing:
   while (!Serial) ;
  Serial2.begin(9600);
  // put your setup code here, to run once:
  initValStart.attachPop(initValStartPopCallback);
  displayInit.attachPop(displayInitPopCallback);

  

}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println(currentSettings.brigtness);


  nexLoop(nex_listen_list);
  delay(2000);
}

When i click the button initValStartPopCallback manualy, it is working and the scetch is execuded. When i use the nextion-click-command it isnt.

I have no idea why

Thank you

You can trigger a event in Nextion via <<click "thingToBeClicked",0>>.

Not quite, you need to use:

click "thingToBeClicked",1
click "thingToBeClicked",0

You can't use 'half' a click, a complete click is click (1) followed buy 'unclick' (0), otherwise the Nextion doesn't respond as you'd expect.

Whandall:
I would start with removing the delay(2000) and moderating the output with millis().

Manuel_o:

void loop() {

Serial.println(currentSettings.brigtness);
  nexLoop(nex_listen_list);
  delay(2000);
}

So you don't listen to advice?

Thank you for your answers

Sorry i dont get it. I have on my page0 my button initVal. There in the preinitialize event of the page0 i put the commands click initVal,1 click initVal,0.
So from my thoughts now, when i unplug/replug my nextion, uplouad my scetch oder visit page0 the button initVal should get triggered.

#include <Arduino.h>
#include <Nextion.h>
//SD-card
#include "FS.h"
#include "SD.h"
#include "SPI.h"
//RTC-clock
#include <Wire.h>
#include "RTClib.h"
//ESP32-Eprom
#include "EEPROM.h"
//Classes
#include <Classes.h>

//Clock
unsigned long Clocktimer = 1000;  // Gibt an wie oft die Uhr ins Display geschrieben wird
unsigned long startChrono_clock;
char tempString[100];

/********Objecte*********/
RTC_DS3231 rtc;


/****** Definition of NEXTION-objects*******/
//Other Objects
NexButton initValStart       = NexButton(0, 2, "initVal");   //page initialization
NexPage displayInit          = NexPage(11, 0, "Display");
NexPage displayInitdd          = NexPage(6, 0, "Messintervall");
// Zeit
NexText Zeit                 = NexText(2, 5, "Zeit");

//page Display
NexNumber brigtNs            = NexNumber(11, 7, "n0");
//page Time
NexButton speichernTime      = NexButton(9, 3, "b1");
NexNumber tag                = NexNumber(9,5,"n0");
NexNumber monat              = NexNumber(9,7,"n1");
NexNumber jahr               = NexNumber(9,8,"n2");
NexNumber stunden            = NexNumber(9,9,"n3");
NexNumber minuten            = NexNumber(9,10,"n4");
NexNumber sekunden           = NexNumber(9,11,"n5");
//page Intervall
NexNumber Zeitintervall_sek             = NexNumber(6, 7, "n0");    //sekunden
NexNumber Zeitintervall_min             = NexNumber(6, 8, "n1");    //minuten
NexNumber Zeitintervall_full            = NexNumber(5, 9, "n2");    //Zeitintervall



/******Declaration of Nextion-Callbacks********/
void initValStartPopCallback(void *ptr);
void displayInitPopCallback(void *ptr);
void speichernTimePopCallback(void *ptr);

/**** all Nextion objects that need to be listened */
NexTouch *nex_listen_list[] =
{
  //buttons
  &initValStart,
  //pages
  &displayInit,
  &speichernTime,
  NULL
};

void SaveSettings()
{
  //Display
  uint32_t tbrightness;
  brigtNs.getValueGlobal("Display",&tbrightness);
  currentSettings.brigtness = tbrightness;
  Serial.println(currentSettings.brigtness);
  
  //Messintervall
  uint32_t tsecondMi;
  Zeitintervall_sek.getValueGlobal("Messintervall",&tsecondMi);
  currentSettings.secondMi = tsecondMi;

  uint32_t tminuteMi;
  Zeitintervall_min.getValueGlobal("Messintervall",&tminuteMi);
  currentSettings.minuteMi = tminuteMi;

  uint32_t tfullMi;
  Zeitintervall_full.getValueGlobal("Messintervall",&tfullMi);
  currentSettings.fullMi = tfullMi;


}


/******Definition of Nextion-Callbacks*****/
//Init-button. Will be clicked automaticly to initialize saved values from ESP32 EPROM
void initValStartPopCallback(void *ptr) 
{
  uint32_t f;
  brigtNs.getValueGlobal("Display",&f);
  currentSettings.brigtness = f;
  Serial.println("lllll");
}

void displayInitPopCallback(void *ptr) 
{
    uint32_t tbrightness;
  brigtNs.getValueGlobal("Display",&tbrightness);
  currentSettings.brigtness = tbrightness;
  Serial.println("HHHHH");
}

void speichernTimePopCallback(void *ptr)
{ 
  uint32_t stag;
  uint32_t smonat;
  uint32_t sjahr;
  uint32_t sstunden;
  uint32_t sminuten;
  uint32_t ssekunden;
  tag.getValue(&stag);
  monat.getValue(&smonat);
  jahr.getValue(&sjahr);
  stunden.getValue(&sstunden);
  minuten.getValue(&sminuten);
  sekunden.getValue(&ssekunden);

  rtc.adjust(DateTime(sjahr, smonat, stag, sstunden, sminuten, ssekunden));

  Serial2.print("vis t2,1");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}


void setup() {
nexInit();
  //Serial COM
  Serial.begin(9600);
   // while the serial stream is not open, do nothing:
  while (!Serial) ;
  Serial2.begin(9600);

  if (! rtc.begin()) {
  Serial.println("Couldn't find RTC");
  while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //rtc.adjust(DateTime(2019, 6, 13, 13, 33, 0));
  }

  //SaveSettings();

  startChrono_clock = millis();


  // put your setup code here, to run once:
  initValStart.attachPop(initValStartPopCallback);
  displayInit.attachPop(displayInitPopCallback);
  speichernTime.attachPop(speichernTimePopCallback);

}

void loop() {
  // put your main code here, to run repeatedly:
  DateTime now = rtc.now();

  if (millis() - startChrono_clock >= Clocktimer)
  {
  sprintf(tempString, "%02d.%02d.%02d/%02d:%02d:%02d",now.day(),now.month(),now.year(),now.hour(), now.minute(),now.second());
  Zeit.setText(tempString);
  startChrono_clock=millis();

  }
  
/* 
  daysPast = now.unixtime()/86400UL;
    if(serviceintervall > daysPast)
    {
      serviceInt.setValue((serviceintervall-daysPast));
    }
        else
    {
      alarm1.setPic(5);
      serviceInt.setValue(0);
    }
  
  startChrono_clock=millis();
  }*/

  Serial.println(currentSettings.brigtness);
  //Serial.println(currentSettings.secondMi);
  //Serial.println(currentSettings.minuteMi);
  //Serial.println(currentSettings.fullMi);


  nexLoop(nex_listen_list);
  delay(1000);
  
}

but

void initValStartPopCallback(void *ptr) 
{
  uint32_t f;
  brigtNs.getValueGlobal("Display",&f);
  currentSettings.brigtness = f;
  Serial.println("lllll");
}

the buttons just does that what i am expecting when i touch it on my display. But isnt doing anything when i visit page0.

What do i miss?

PerryBebbington:
Not quite, you need to use:
You can't use 'half' a click, a complete click is click (1) followed buy 'unclick' (0), otherwise the Nextion doesn't respond as you'd expect.

No sir, it isnt about that. the delay is realy just here because this scetch is just for learning purpose, and i dont see why the delay should cause my problems. Shouldnt be disrespectfull or anything.

Sorry i dont get it. I have on my page0 my button initVal. There in the preinitialize event of the page0 i put the commands click initVal,1 click initVal,0.

In your original post you said:

You can trigger a event in Nextion via <<click "thingToBeClicked",0>>.

Which is what I responded to. You are now saying that you are using click initVal,1 click initVal,0, which is fine but not what you originally told me.

There in the preinitialize event of the page0 i put the commands click initVal,1 click initVal,0.

I'm not really sure what putting anything in the pre initialize event box does, but I would not expect clicks in there to work because, as the name says, events in there are before the page has initialised. I would think that trying to click something on the page before it has initialised is probably not going to work. Try putting in post initialzation and see if that makes any difference.

I'm afraid I can't help you much beyond this point because you are using the Nextion libraries and I don't use them and don't know much about them. I was just commenting on the bit I do know about.

Hi friends,

I encountered the same problem today. I also use the "Press" and "Release" events. I would like to share the information I have obtained from my own reviews:

I haven't tested it on other components, but the Button object is definitely like this:

When the button object is triggered by the "click" command within the code, "Send Component ID" does not send this information to the arduino side even if the checkbox is checked. The "Send Component ID" is sent with a physical click only. :frowning:

The video I recorded for this bug:

Respects.

edit: I just tested it with the "DSButton" (dual-state button) object, but the result is negative!

Hello ersinkecis,

I don't understand what you are expecting in your video. I have created a button in the Nextion editor, checked 'send component ID' under 'Touch press event', clicked 'debug' and tested it, the component ID is sent and the data shows in the simulator return window.

In your video you seem to be expecting something to happen when you are in edit mode in the editor. At no point do I see you use the debug screen to test. Am I missing something or misunderstanding what you are doing?

Hello

I am still struggling with the click command in Nextion

What i still want. When i am switching to a site, a button should be pressed.

I created a button A. Send Component ID is checked (touch release event)

In the Preinitalize Event of my site i inserted click b0,1
click b0,0

When i am testing it in my Nextion editor debugger. Everything is fine and when i switch to the site, the button A gets pressed automaticly.

When i am texting it in my programm. Nothings happens. It seems like ersinkecis is right, and the arduino (or ESP) does not get the "signal"

Any ideas Perry?

Thank you

Sorry Manuel,
I am struggling to understand your description. What do you mean by a 'site'?

When I am texting it in my program. Nothings happens.

Please show me your code.

ok sorry Perry.

So i have a button b0. The button does this:

void alarmlistPopCallback(void *ptr)
{
  int z = 1;
  
  for (int i= 0; i < 3; i++)            
    {
      if(errorMsgs[i][100]!='\0')
      {
      String str = String("ErrorMsg_")+String(z)+String(".txt=")+String("\"")+String(errorMsgs[i][100])+String("\"");
      Serial.println(str);
      Serial2.print(str);
      Serial2.write(0xFF);
      Serial2.write(0xFF);
      Serial2.write(0xFF);

      String str_2 = String("Timestamp_")+String(z)+String(".txt=")+String("\"")+String(errorMsgsTimestamp[0][50])+String("\"");
      Serial.println(str_2);
      Serial2.print(str_2);
      Serial2.write(0xFF);
      Serial2.write(0xFF);
      Serial2.write(0xFF);
      z++;
      delay(100);
      }
    }
}

I want that this button is pushed automaticly, as soon the site (on which the button is on) is opened. I dont want to push it by myselft. At the end, i want to hide the button.

So my idea was to write the click event in the preinitalize-field on the site in my nextion editor.

click b0,0<.

I testet the button. When i push the button per hand, everything works. But when i try to manage it, that it is pushed automaticly via click-event, it seems that my ardunio (esp32) does not get the serialcommand from the nextion.

Can you confirm, that a click-event in your nextion sends a "signal" to your ardunio?

Thanks a lot Perry!

Manuel_o:
So my idea was to write the click event in the preinitalize-field on the site in my nextion editor.

click b0,0<.

So the button should do something before it is initialized? Seems pretty strange to me.

Hello Manuel,
Would I be correct to assume English is not your first language? you use the word 'site' when I think you mean 'page', as in page on the Nextion display.

4 things...

  • You appear to be using the Nextion libraries, about which I know very little, so my help will be limited.
  • As Whandall says, preinitialisation event is the wrong place, put it in post initialisation event
  • Do you even need to click the button? Anything you put under the button can be put under post initialisation event to do the same thing.
  • To hide a button make it as small as the IDE allows (I think 2 x 2 pixels), make the background to the button the same as the background to the page and put it in on corner.

I hope that helps

PerryBebbington:
Hello ersinkecis,

I don't understand what you are expecting in your video. I have created a button in the Nextion editor, checked 'send component ID' under 'Touch press event', clicked 'debug' and tested it, the component ID is sent and the data shows in the simulator return window.

In your video you seem to be expecting something to happen when you are in edit mode in the editor. At no point do I see you use the debug screen to test. Am I missing something or misunderstanding what you are doing?

00:03 between 00:21
during this time, the buttonClick code runs continuously from within the timer, but the code you write into the buttonClick event only works. that is, the nextion is running its own code (the text of the button named 'SET' on the bottom left converted 'AAAA'), but the arduino cannot fill the textbox on the screen because nextion is not sent.

00:22
When you physically click the button in this second, the notification goes to arduino from nextion and all the textboxes on the screen are filled with the information from the arduino.

00:29 and 00:53
this section shows the codes in the timer which continuously triggers the upper right button on the screen.
click D_bPageReady, 1
click D_bPageReady, 0
commands are available here but only the code under the button is running. nextion is not sending information to arduino.

00:46
The timer that I mentioned in the PostInitializeEvent section becomes active on page opening.

01:11
You can see the buttonClick code at here. This command works only with the 'click' command, but the nextion is not sent the information to arduino. that the button is triggered. so that the information on the screen without physically clicking the button can not filled from arduino!

Hello ersinkecis,

I perhaps should have spotted this earlier but you seem to be asking your own question one someone else's discussion. This makes is difficult to keep up with which answers belong to which question and is thus against the forum rules. Please click on 'report to moderator' and ask them to put your posts into a separate question. However, will point out that I am not keen on the idea of watching your video to understand your problems and offer advice, that just doesn't work for me.
You need to post code.

Please read and follow the forum rules before posting anything else.

Thank you.

Manuel_o:
I am still struggling with the click command in Nextion
What i still want. When i am switching to a site, a button should be pressed.
I created a button A. Send Component ID is checked (touch release event)
In the Preinitalize Event of my site i inserted click b0,1
click b0,0

When i am testing it in my Nextion editor debugger. Everything is fine and when i switch to the site, the button A gets pressed automaticly.

hi, i think resolved this problem. this code is in the page1.b0

printh 65 00 02 00 FF FF FF //this hex code is sending page0.b1 component id
//click page0.b1,1
//click page0.b1,0

the nextion editor updated to version 0.59 today. in the old version, it allowed a command such as "click page0.b0,1" in page1. this command now gives error even if page0 and page0.b0 are global. it can not compiled. as a solution, I'm manually sending the component id that belongs to page0.b0 while in page1. hopefully it benefits your business. :slight_smile: