Transmit Data (Button) to PIC (receive) in Nextion

Started by Aries_Ryan, Sep 15, 2025, 10:49 AM

Previous topic - Next topic

Aries_Ryan

Hi,
I wish to transmit data from the Nextion Button to PIC.
I tried to do this way but still won't work, what is wrong with my code below:
'So far I still use the previous configuration as below

'----------------------------------- Device and Configuration -----------------------------------------------
Device = 16F628A                                                                      'Microprocessor Use   
Config INTRC_OSC_CLKOUT, MCLRE_OFF, LVP_OFF, WDT_OFF, PWRTE_OFF, CP_OFF, BODEN_OFF    'Configuration of bits
Declare Xtal = 4                                                                      'Use Crystal 4 Mhz
'------------------------------------- End Configuration -----------------------------------------------------

'--------------------------------------LCD Configuration -----------------------------------------------------
'--- Declare LCD Function here
'------------------------------------- End LCD Configuration --------------------------------------------------

Declare Hserial_Baud = 9600 ' Set to 9600 for communication Receiver and Transmitter baud rate
Declare Hserial_RCSTA = %10010000       ' Enable serial port and continuous receive
Declare Hserial_TXSTA = %00100100       ' Enable transmit and asynchronous mode
Declare HRSOut_Pin = PORTB.2            ' TX Pin 8 will connect to RX pin side to Nextion TFT Board
Declare HRSIn_Pin = PORTB.1             ' RX Pin 7 will connect to TX pin side to Nextion TFT Board
Declare Hserial_Clear = On ' Enable Error clearing on received characters

'------------------------------------- Prepare Comparator CMCON -----------------------------------------------
CMCON = 7                       'CMCON = 0b00100101
'--------------------------- Variable Addressing --------------------------------------------------------------
Dim Var1 As Byte
Dim Var2 As Byte

Dim nextion_byte As Byte
Dim page_id As Byte
Dim component_id As Byte

main:
DelayMS 10
'Var1 = HRSIn 1000 Time is Over "b0.val=1"
Var1 = HRSIn "b0.val=1"   'bo is Object Name of Nextion Editor
Var1 = HRSIn ("e")        'Send Button Command (serial byte e) Nextion to PIC, transmit and receive it
Var1 = HRSIn If PORTA.0 = 1 Then Setup_Counter_Time    'Get this button into PIC then
The scenario, once I press the touch button into Nextion TFT side, it will receive and toggle PORTA.0 = 1.
If anyone, have experiences with playing this Nextion instruction button, please help.

Thanks,
ARyan.



RGV250

Hi,
Is that all the code as it will just do the 4 lines after Main and stop?

Bob

Aries_Ryan

Hi Bob,
Firstly, thank you for your question.
That one is not a complete code, I wish to tested first to basic access transmit on Nextion Touch Button to received in PIC, once it working well then continue to next function.


Thanks,
ARyan.

RGV250

Hi,
What i am saying is with the code you have posted it will stop in milliseconds as you have no loop so probably before you press the button.

Bob

Stephen Moss

Quote from: Aries_Ryan on Sep 15, 2025, 11:51 AMThat one is not a complete code, I wish to tested first to basic access transmit on Nextion Touch Button to received in PIC, once it working well then continue to next function.
To clarify, he was wanting to know if what you posted is only part of a larger code where there is a Goto Main statement that you did not show in your example that will make the code you have provided loop repeatedly, because if the code you provided is all you have then it will only execute once and so the line
If PORTA.0 = 1 Then Setup_Counter_Time    'Get this button into PIC thenwill probably executes before you press the button and so will never detect the button press.

Also, I don't realy use serial so I may be wrong but to me you code...
Var1 = HRSIn "b0.val=1"   'bo is Object Name of Nextion Editor
Var1 = HRSIn ("e")        'Send Button Command (serial byte e) Nextion to PIC, transmit and receive it
look like you are trying to send data but are using the recieve data command instead of HRSout.
If the value after the HRSin command is a reminder of what you are expecting to recive then it should be after the comment character, additionally you have have defined Var1 as a byte and "b0.val=1" would be a string and cannot be stored in the single byte variable Var1.

Finally, If PORTA.0 = 1 Then Setup_Counter_Time    'Get this button into PIC then is not necessarily a good way of detecting the button press as there could be a large delay in detecting it if your final code is large and takes a long time to loop, and it only really works if the button state is latched (not changing until the button is pressed again), so consider using an input pin with IOC (Interrupt on Change) capabilities so that an interrupt is triggered when the button output changes state for a faster responce to the button press.

joesaliba

#5
I did some complex code with Nextion displays and it worked.

Your code is not going to work. It takes a while until you understand how the data is being sent, and I would suggest to read the Nextion Instruction set from here before you proceed with any Nextion code.

For some help, from Nextion Instruction manual, a touch event comprise of the following data: -

0x65 0x00 0x01 0x01 0xFF 0xFF 0xFF

For a beginner and to not get into so much problems, discard the 0xFF 0xFF 0xFF for now. Sometimes I use it to make sure I received the correct data.

A touch event could be a button, a text box or whatever there is on the screen. Touch to any component can be turned OFF, (disabled), in Nextion code.

Components on different pages have the same component ID, unless you manually change them.

So, how we capture a button, or better a component?

I will wait for a 350ms delay, sometimes even less than that, depends the main code I am in. If 350ms are elapsed I must continue with main code, cannot wait here forever.

Assume that you have a button with a component ID 0x01

First we must make some byte variables: -

Dim L1_Data [7]     As Byte = 0     ' Variable to hold receiving serial data from Nextion display
    Dim Pge         As L1_Data#0    ' Variable to hold Page No. from array L1_Data
    Dim Cmp         As L1_Data#1    ' Variable to hold component No. from array L1_Data; Component ID
    Dim Evt         As L1_Data#2    ' Variable to hold event; 0x01 Press and 0x00 Release from array L1_Data

Then I proceed to wait and capture the data: -

Clear L1_Data                           ' Clear L1_Data array
    HRSIn {350, Ex_get_Butt}, Wait (0x65)   ' Wait for 0x65 and then continue retrieving data; A Touch Event is 0x65
    HRSIn Str L1_Data\7                     ' then capture the next 7 bytes

    Ex_Get_Butt:                            ' Exit Get_Page procedure if data did not arrived

We then need to see if you pressed the correct button, on the correct page to turn ON or OFF an LED. At this point, I am not interested if the button has been pressed or released, therefore, I ignore Evt, (L1_Data#2). All I am interested in was which component has been touched.

Therefore, if I am only looking at the page number, and the component, I ended up looking for the following reduced Nextion data: -

0x00 0x01

If Pge = 0x00 then
    If Cmp = 0x01 then
        Toggle PORTA.0
        Delayms 100
    Endif
 EndIf

If you have multiple buttons, (components), on same page then the following code can be done: -

If Pge = 0x00 then
    If Cmp = 0x01 then
        Toggle PORTA.0
    ElseIf Cmp = 0x02 then
        Toggle PORTA.1
    Endif
    Delayms 100
EndIf

Hope this help get you started.