yozik04 / nextion

Nextion serial client
GNU Lesser General Public License v3.0
25 stars 10 forks source link

How to send nextion command in event_handler function? #2

Closed A-tichat closed 4 years ago

A-tichat commented 4 years ago

Hello

I'm using nextion display to make some projects. I'm trying so much to find out about library nextion for python then I found your library.

It easy to understand for me. but have some functions that I don't know how to use.

A simple code is I want to go to the next page when I press a button. This I trying.

async def event_handler(type_, data):
    if type_ == EventType.TOUCH:
        await client.command('page 1')

and I add global to run()

async def run():
    global client
    client = Nextion('/dev/ttyAMA0', 9600, event_handler)
    await client.connect()
    await client.wakeup()

It dosen't work and I don't any have idea.

yozik04 commented 4 years ago

A simple code is I want to go to the next page when I press a button.

It is better to code this in nextion editor.

For library to receive any touch events you need to check Send Component ID for required event. Otherwise no event is transmitted.

A-tichat commented 4 years ago

I sorry that makes you misunderstand. A code next page is an example of simple nextion command.

Actually, my code is a password checker. I must get the password from a text block then check it with my database before going to the next page. So, I must take action within python code.

Please, help me solve this problem.

yozik04 commented 4 years ago

I guess you have a button that need to start fetching password from a text block then decide what to do. On this button in onPress event you need to check Send Component ID checkmark. Then when you press it on the nextion, panel will send this touch event to python event_handler function

A-tichat commented 4 years ago

Alright, This is my nextion screen. https://drive.google.com/file/d/16cd0n6_nT9fj2T4caBunfEFUIOYmIXON/view?usp=sharing

and this is my python code https://raw.githubusercontent.com/A-tichat/raspberry-pi-mrz/master/display/simple.py

I use this code on raspberry pi 4 b that output is error https://drive.google.com/file/d/1zlUrVSDPjKkEwmcesgFCsTGdwWpBHZwF/view?usp=sharing

yozik04 commented 4 years ago

Ahh, event_handler should not be async.

yozik04 commented 4 years ago

it is a callback. If you want async you can do inside event_handler asyncio.create_task(async_event_handler(....)) But make sure you catch all exceptions there, otherwise they will be just supressed.

A-tichat commented 4 years ago

That is what I wanted. Thank you very much for your help.