Start a new topic

How to capture the wake-up event from Arduno

I want my project to go to sleep, after inactivity.  So I send:


thup=1 (followed by 3 0xFF)

sleep=1 (followed by 3 0xFF)


Works good.  Ok so now I want to capture the WAKE event, to reset the sleep timer on Arduino.  I see classes for buttons, pages, image buttons, etc.. all the rest of the controls.  I am lost when it comes to receiving the wake-command.  Can please get some help?


 Hi Erich,


after a sleep=1, the Nextion still stays active and responds to a touch ... means, when you touch the panel, no matter thup is 0 or 1, if any touch operation in sleep mode happens, the device will send touch coordinates to the serial port.


And that's what you must catch with your Arduino code ...

Via a polling loop, via uart interrupt, ... there are many different possibilities how to do ...


Gerry

In the NexHardware.cpp file of the Nextion library,

- this functionality would need to be added to the NexLoop procedure.


As we can see the library is currently

- capturing the Nextion Touch Pressed and Released Events 0x65 Return Code Data


You probably want to capture the 0x86 and 0x87 Nextion Return Codes


As per the Nextion Instruction Set: Format_of_Device_Return_Data

0X86 Device automatically enters into sleep mode 0X86+End

: Only when the device automatically enters into sleep mode will return this data. If execute serial command “sleep = 1” to enter into sleep mode, it will not return this data.

0X87 Device automatically wake up 0X87+End

: Only when the device automatically wake up will return this data. If execute serial command “sleep=0” to wake up, it will not return this data.


So first these defines would need to be added at the top

#define NEX_RET_EVENT_AUTOSLEEP     (0x86)

#define NEX_RET_EVENT_AUTOWAKE      (0x87)


Then in the NexLoop the code for this needs to be entered into the while loop.

It would resemble something like

 

 

 

        if (NEX_RET_EVENT_AUTOSLEEP  == c)
        {
            if (nexSerial.available() >= 3)
            {
                __buffer[0] = c;  
                for (i = 1; i < 4; i++)
                {
                    __buffer[i] = nexSerial.read();
                }
                __buffer[i] = 0x00;              
                if (0xFF == __buffer[1] && 0xFF == __buffer[2] && 0xFF == __buffer[3])
                {
                    // Need to create a new function to call when falls asleep
                    // call this new onsleep function
                }
            }
        }

        if (NEX_RET_EVENT_AUTOWAKE  == c)
        {
            if (nexSerial.available() >= 3)
            {
                __buffer[0] = c;  
                for (i = 1; i < 4; i++)
                {
                    __buffer[i] = nexSerial.read();
                }
                __buffer[i] = 0x00;              
                if (0xFF == __buffer[1] && 0xFF == __buffer[2] && 0xFF == __buffer[3])
                {
                    // Need to create a new function to call when now wakes up
                    // call this new onwake function
                }
            }
        }

 

 

 


Ok, so I must modify NexHardware.cpp add the code to handle this function.  Thank you for the reply.

Dear Patrick Martin or anybody how can I do to make when nextion go sleep automatically go to main page, because sometimes the user go to setup page and they went out and the screen is always on this page


I put this but doesn't work properly


if (NEX_RET_EVENT_AUTOSLEEP == c)

        {

            if (nexSerial.available() >= 3)

            {

                __buffer[0] = c;

                for (i = 1; i < 4; i++)

                {

                    __buffer[i] = nexSerial.read();

                }

                __buffer[i] = 0x00;

                if (0xFF == __buffer[1] && 0xFF == __buffer[2] && 0xFF == __buffer[3])

                {

                    // Need to create a new function to call when falls asleep

                    // call this new onsleep function


     //Serial.print("sleep mode");

     sendCommand("sleep=0"); // wake up

     Serial.write(0xff);

     Serial.write(0xff);

     Serial.write(0xff);

     Serial.print("page 0");

     Serial.write(0xff);

     Serial.write(0xff);

     Serial.write(0xff);

     sendCommand("sleep=1"); // turn sleep

     Serial.write(0xff);

     Serial.write(0xff);

     Serial.write(0xff);

                }

            }

        }

Use Arduino Serial monitor to locate your issue.

I apologize, I won't be debugging code for free this week.


I am not sure which version of the Nextion Editor you are using,

but you can benefit from reviewing

1) The library code

2) The Nextion Instruction Set.


1) SendCommand already appends the three 0xFFs

2) while it is still very useful to listen to Nextion Return Data

    you are going through unneeded steps - such as in #1


What you have would be proper, shortened to

SendCommand("sleep=0");
SendCommand("page  0");

SendCommand("sleep=1");


I question where you are putting it

 - an external function is what I was suggesting, perhaps no matter.

I would question when it doesn't work, you have other conflicting logic.


But as stated, I won't be debugging this week.

The forum closes on the 15th

The new forum is open

Thank you very much, I put this code in my NexHardware.cpp file of the Nextion library.

What I'm looking for is when in turn sleep mode always returns to page 0

Is very strange I only put 

     sendCommand("sleep=0"); // wake up

     sendCommand("page 0");

so when it go sleep , the screen wake up, but never go to page 0 


Thanks again,  I will try reading more about

You're not listening to the Nextion Return Data

and yes, there is more coding you need to look at.

If page 0 is not going to page 0 then it is because you are

indiscriminately sending the command before Nextion is ready.


How do you know when Nextion is ready

 - Nextion Return Data.

recvRetCommandFinished

 

I found the solution
, I put a delay between the sendcommand and run ok, thanks and have a nice weekend


 delay() is not the correct way

 - it means halt everything and do absolutely nothing.

This will also mean no reading serial, no interrupts, etc.


recvRetCommandFinished will look for the Nextion Return

 0x01 0xFF 0xFF 0xFF and last command is successful,

 not 0x01 0xFF 0xFF 0xFF and last command failed.

Yea that delay() is a tough stumbling block to overcome for new programmers.  It is not easy.  If I remember right, my code ran the commands in functions.  In the main loop the functions checked for call-back responses from the Nextion, executed based on whatever it was - sleep, page commands, etc...