yozik04 / nextion

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

Async event handler is not working #27

Closed tbrodbeck closed 2 years ago

tbrodbeck commented 2 years ago

Hi. This library looks really promising. And everything works for me but one thing: the events.

So, I just create a Minimum non-Working Example from your instructional script ans saved it as nextionTutorial.py:

import asyncio
import logging

from nextion import Nextion, EventType

class App:
    def __init__(self):
        self.client = Nextion('/dev/ttyS0', 115200, self.event_handler)

    # Note: async event_handler can be used only in versions 1.8.0+ (versions 1.8.0+ supports both sync and async versions)
    async def event_handler(self, type_, data):
        print('EVENT!', type_, data)
        if type_ == EventType.STARTUP:
            print('We have booted up!')
        elif type_ == EventType.TOUCH:
            print('A button (id: %d) was touched on page %d' % (data.component_id, data.page_id))

        logging.info('Event %s data: %s', type, str(data))

        print(await self.client.get('b_about.txt'))

    async def run(self):
        await self.client.connect()

        print(await self.client.get('b_about.txt'))

        print('finished')

if __name__ == '__main__':
    logging.basicConfig(
        format='%(asctime)s - %(levelname)s - %(message)s',
        level=logging.DEBUG,
        handlers=[
            logging.StreamHandler()
        ])
    loop = asyncio.get_event_loop()
    app = App()
    asyncio.ensure_future(app.run())
    loop.run_forever()

Then I ran python nextionTutorial.py which yields the following output:

2022-03-17 10:28:46,618 - INFO - Connecting: /dev/ttyS0, baud: 115200
2022-03-17 10:28:46,619 - INFO - Connected to serial
2022-03-17 10:28:46,620 - DEBUG - sent: b'DRAKJHSUYDGBNCJHGJKSHBDN'
2022-03-17 10:28:46,623 - DEBUG - received: b'1a'
2022-03-17 10:28:46,663 - DEBUG - sent: b'connect'
2022-03-17 10:28:46,671 - DEBUG - received: b'636f6d6f6b20332c313739332d302c4e5838303438503037305f303131432c3136332c31303530312c393636323334303143374332334435352c3133313037323030302d30'
2022-03-17 10:28:46,672 - INFO - Address: 1793-0
2022-03-17 10:28:46,672 - INFO - Detected model: NX8048P070_011C
2022-03-17 10:28:46,673 - INFO - Firmware version: 163
2022-03-17 10:28:46,673 - INFO - Serial number: 96623401C7C23D55
2022-03-17 10:28:46,673 - INFO - Flash size: 131072000-0
2022-03-17 10:28:46,674 - DEBUG - sent: b'bkcmd=3'
2022-03-17 10:28:46,676 - DEBUG - received: b'01'
2022-03-17 10:28:46,676 - DEBUG - sent: b'get sleep'
2022-03-17 10:28:46,679 - DEBUG - received: b'7100000000'
2022-03-17 10:28:46,680 - INFO - Successfully connected to the device
2022-03-17 10:28:46,680 - DEBUG - sent: b'get b_about.txt'
2022-03-17 10:28:46,684 - DEBUG - received: b'7041626f7574'
About
finished

You can see here, that eveything works fine - for example it could read the string About from b_about.txt.

But no events whatsoever get registered. I do not get a printout of the STARTUP event and neither do I when I press buttons on the screen.

Thanks in advance for any help!

My System:

OS: Raspbian GNU/Linux 11 (bullseye) armv7l Host: Raspberry Pi 4 Model B Rev 1.2 Kernel: 5.10.92-v7l+ Shell: bash 5.1.4 Python 3.9.2 nextion==1.8.1 nest-asyncio==1.5.4 pyserial-asyncio==0.6 Nextion Display: NX8048P070-011C

yozik04 commented 2 years ago

Hi Till,

Try to change async def event_handler(self, type_, data): to def event_handler(self, type_, data): and comment out last line with await in that function. Does it work this way, with sync event_handler?

tbrodbeck commented 2 years ago

Hi @yozik04 yes it works with the sync event handler! That is already good new. Nevertheless, I would prefer having it async

yozik04 commented 2 years ago

Very strange. Because I use async version of that function and no problems. Are you using venv?

tbrodbeck commented 2 years ago

I have no idea why. But I just tested it again today and it works in the same environment... (Just the STARTUP is still not showing up, but I do need it)

I found the connection to the Nextion display sometimes a little bit unpredictable. Sometimes I cannot even create a connection and need to reboot the controller. So I guess, maybe there was an issue from the hardware side.

yozik04 commented 2 years ago

STARTUP event only appears when unit reboots while you are connected. Your connection speed might be too high. Try to lower baud rate on Nextion side.

tbrodbeck commented 2 years ago

Okay thanks. I will try that if I encounter issues again! Thanks for your quick support :)