Nextion display serial client
Project description
Nextion serial client
Lightweight Python 3.5+ async library to control Nextion displays.
Installation
Pypi
pip3 install nextion
Simple usage:
import asyncio
import logging
import random
from nextion import Nextion, EventType
class App:
def __init__(self):
self.client = Nextion('/dev/ttyS1', 9600, 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):
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('field1.txt'))
async def run(self):
await self.client.connect()
# await client.sleep()
# await client.wakeup()
# await client.command('sendxy=0')
print(await self.client.get('sleep'))
print(await self.client.get('field1.txt'))
await self.client.set('field1.txt', "%.1f" % (random.randint(0, 1000) / 10))
await self.client.set('field2.txt', "%.1f" % (random.randint(0, 1000) / 10))
await self.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()
app = App()
asyncio.ensure_future(app.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nextion-1.8.0.tar.gz
.
File metadata
- Download URL: nextion-1.8.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b7e80ab397ed6293843129028a6a1c75be134ea3ac92d79727bed4f9de06e30 |
|
MD5 | 285712176e13786c42b6c2984f39b522 |
|
BLAKE2b-256 | b3989d6c352200bb90d1a57ef303147c249bce6a4eabb03167133b8e86d6670f |
File details
Details for the file nextion-1.8.0-py3-none-any.whl
.
File metadata
- Download URL: nextion-1.8.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48834dadc0e76e76c36048b7c85b69c0073451357c7ab9cd6656052d3c568c28 |
|
MD5 | 8b35c4d90f02119826c7e1090fdcf920 |
|
BLAKE2b-256 | a5d28d87ce4fe1da8c9dcb1d6a6c222319ef240304b0419f0898d5534751bafa |