Skip to main content

Nextion display serial client

Project description

Nextion serial client Build Status

Lightweight Python 3.5+ async library to control Nextion displays.

Installation

Pypi

pip install nextion

Simple usage:

import asyncio
import logging
import random

from nextion import Nextion, EventType

def event_handler(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))

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

    # await client.sleep()
    # await client.wakeup()

    # await client.command('sendxy=0')

    print(await client.get('sleep'))
    print(await client.get('field1.txt'))

    await client.set('field1.txt', "%.1f" % (random.randint(0, 1000) / 10))
    await client.set('field2.txt', "%.1f" % (random.randint(0, 1000) / 10))

    await client.set('field3.txt', random.randint(0, 100))

    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()
    asyncio.ensure_future(run())
    loop.run_forever()

Nextion constructor parameters

url: str # serial dev
baudrate: int # baud rate
event_handler: typing.Callable[[EventType, any], None] # Event handler function
loop=asyncio.get_event_loop() # your own event loop
reconnect_attempts: int = 3 # how many times to try to retry command in case of failure
encoding: str = 'ascii' # Nextion encoding

Nextion parameters

Encoding

You can update encoding on fly (This changes encoding of serial communication only):

client.encoding = 'latin-1'

Get current set encoding (Not fetched from the device)

print(client.encoding)

Event handling

event_handler method in the example above will be called on every event comming from the display.

EventType Data Data attributes
TOUCH TouchDataPayload page_id, component_id, touch_event
TOUCH_COORDINATE TouchCoordinateDataPayload x, y, touch_event
TOUCH_IN_SLEEP TouchCoordinateDataPayload x, y, touch_event
AUTO_SLEEP None -
AUTO_WAKE None -
STARTUP None -
SD_CARD_UPGRADE None -

For some components in the Nextion Editor you need to check Send Component ID for required event.

Firmware uploading

If you installed the library you should have nextion-fw-upload command in your PATH.

nextion-fw-upload -h

Otherwise use next command in the root of the project:

python -m nextion.console_scripts.upload_firmware -h

Usage (check with option -h)

usage: nextion-fw-upload [-h]
                         [-b {2400,4800,9600,19200,38400,57600,115200,230400}]
                         [-ub {2400,4800,9600,19200,38400,57600,115200,230400}]
                         [-v]
                         device file

positional arguments:
  device                device serial port
  file                  firmware file *.tft

optional arguments:
  -h, --help            show this help message and exit
  -b {2400,4800,9600,19200,38400,57600,115200,230400}, --baud {2400,4800,9600,19200,38400,57600,115200,230400}
                        baud rate
  -ub {2400,4800,9600,19200,38400,57600,115200,230400}, --upload_baud {2400,4800,9600,19200,38400,57600,115200,230400}
                        upload baud rate
  -v, --verbose         output debug messages

Additional resources:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nextion-1.7.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

nextion-1.7.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file nextion-1.7.1.tar.gz.

File metadata

  • Download URL: nextion-1.7.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.5.6

File hashes

Hashes for nextion-1.7.1.tar.gz
Algorithm Hash digest
SHA256 05f4b01557147790d65789fc7afc3fb58859705d18dc8d0d8f9b8e134e02de00
MD5 114a034ab0230df86ef8fa16ea253320
BLAKE2b-256 dab4d755aed0b02f36f90b7e85867ed3a340c2197a183dda0a3524d3f88db957

See more details on using hashes here.

File details

Details for the file nextion-1.7.1-py3-none-any.whl.

File metadata

  • Download URL: nextion-1.7.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.5.6

File hashes

Hashes for nextion-1.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f20a8b3830e00ba9566c3936f49b8d58b41273752c8c35b92b8c83679b43d535
MD5 b2f0edef440210793e7372b428b2ca30
BLAKE2b-256 981c10e3e69f37ad2a6e10729bc0a8e8b92b1a4ba11e42ba92bf155c47114b27

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page