nextion


Namenextion JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/yozik04/nextion
SummaryNextion display serial client
upload_time2024-07-28 19:30:11
maintainerNone
docs_urlNone
authorJevgeni Kiski
requires_python<4,>=3.8.0
licenseLGPL 3
keywords nextion serial async asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Nextion serial client [![Build](https://github.com/yozik04/nextion/actions/workflows/python-package.yml/badge.svg)](https://github.com/yozik04/nextion/actions/workflows/python-package.yml)
Lightweight Python 3.8+ async library to control Nextion displays.

## Installation
### Pypi
`pip3 install nextion`

## Simple usage:
```python
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 coming 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.
```bash
nextion-fw-upload -h
```

Otherwise use next command in the root of the project:
```bash
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:
- https://nextion.tech/instruction-set/
- [PyPI](https://pypi.org/project/nextion/)

                   GNU LESSER GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.


  This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

  0. Additional Definitions.

  As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.

  "The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

  An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

  A "Combined Work" is a work produced by combining or linking an
Application with the Library.  The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".

  The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

  The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

  1. Exception to Section 3 of the GNU GPL.

  You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

  2. Conveying Modified Versions.

  If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

   a) under this License, provided that you make a good faith effort to
   ensure that, in the event an Application does not supply the
   function or data, the facility still operates, and performs
   whatever part of its purpose remains meaningful, or

   b) under the GNU GPL, with none of the additional permissions of
   this License applicable to that copy.

  3. Object Code Incorporating Material from Library Header Files.

  The object code form of an Application may incorporate material from
a header file that is part of the Library.  You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

   a) Give prominent notice with each copy of the object code that the
   Library is used in it and that the Library and its use are
   covered by this License.

   b) Accompany the object code with a copy of the GNU GPL and this license
   document.

  4. Combined Works.

  You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

   a) Give prominent notice with each copy of the Combined Work that
   the Library is used in it and that the Library and its use are
   covered by this License.

   b) Accompany the Combined Work with a copy of the GNU GPL and this license
   document.

   c) For a Combined Work that displays copyright notices during
   execution, include the copyright notice for the Library among
   these notices, as well as a reference directing the user to the
   copies of the GNU GPL and this license document.

   d) Do one of the following:

       0) Convey the Minimal Corresponding Source under the terms of this
       License, and the Corresponding Application Code in a form
       suitable for, and under terms that permit, the user to
       recombine or relink the Application with a modified version of
       the Linked Version to produce a modified Combined Work, in the
       manner specified by section 6 of the GNU GPL for conveying
       Corresponding Source.

       1) Use a suitable shared library mechanism for linking with the
       Library.  A suitable mechanism is one that (a) uses at run time
       a copy of the Library already present on the user's computer
       system, and (b) will operate properly with a modified version
       of the Library that is interface-compatible with the Linked
       Version.

   e) Provide Installation Information, but only if you would otherwise
   be required to provide such information under section 6 of the
   GNU GPL, and only to the extent that such information is
   necessary to install and execute a modified version of the
   Combined Work produced by recombining or relinking the
   Application with a modified version of the Linked Version. (If
   you use option 4d0, the Installation Information must accompany
   the Minimal Corresponding Source and Corresponding Application
   Code. If you use option 4d1, you must provide the Installation
   Information in the manner specified by section 6 of the GNU GPL
   for conveying Corresponding Source.)

  5. Combined Libraries.

  You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

   a) Accompany the combined library with a copy of the same work based
   on the Library, uncombined with any other library facilities,
   conveyed under the terms of this License.

   b) Give prominent notice with the combined library that part of it
   is a work based on the Library, and explaining where to find the
   accompanying uncombined form of the same work.

  6. Revised Versions of the GNU Lesser General Public License.

  The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

  Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

  If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yozik04/nextion",
    "name": "nextion",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8.0",
    "maintainer_email": null,
    "keywords": "nextion serial async asyncio",
    "author": "Jevgeni Kiski",
    "author_email": "yozik04@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/dc/fd/97ba3123df735624950c32c493d7dc3dfdb199b46577ceb9b9733b57768d/nextion-2.0.0.tar.gz",
    "platform": null,
    "description": "# Nextion serial client [![Build](https://github.com/yozik04/nextion/actions/workflows/python-package.yml/badge.svg)](https://github.com/yozik04/nextion/actions/workflows/python-package.yml)\nLightweight Python 3.8+ async library to control Nextion displays.\n\n## Installation\n### Pypi\n`pip3 install nextion`\n\n## Simple usage:\n```python\nimport asyncio\nimport logging\nimport random\n\nfrom nextion import Nextion, EventType\n\nclass App:\n    def __init__(self):\n        self.client = Nextion('/dev/ttyS1', 9600, self.event_handler)\n\n    # Note: async event_handler can be used only in versions 1.8.0+ (versions 1.8.0+ supports both sync and async versions)\n    async def event_handler(self, type_, data):\n        if type_ == EventType.STARTUP:\n            print('We have booted up!')\n        elif type_ == EventType.TOUCH:\n            print('A button (id: %d) was touched on page %d' % (data.component_id, data.page_id))\n\n        logging.info('Event %s data: %s', type, str(data))\n\n        print(await self.client.get('field1.txt'))\n\n    async def run(self):\n        await self.client.connect()\n\n        # await client.sleep()\n        # await client.wakeup()\n\n        # await client.command('sendxy=0')\n\n        print(await self.client.get('sleep'))\n        print(await self.client.get('field1.txt'))\n\n        await self.client.set('field1.txt', \"%.1f\" % (random.randint(0, 1000) / 10))\n        await self.client.set('field2.txt', \"%.1f\" % (random.randint(0, 1000) / 10))\n\n        await self.client.set('field3.txt', random.randint(0, 100))\n\n        print('finished')\n\nif __name__ == '__main__':\n    logging.basicConfig(\n        format='%(asctime)s - %(levelname)s - %(message)s',\n        level=logging.DEBUG,\n        handlers=[\n            logging.StreamHandler()\n        ])\n    loop = asyncio.get_event_loop()\n    app = App()\n    asyncio.ensure_future(app.run())\n    loop.run_forever()\n```\n\n### Nextion constructor parameters\n```\nurl: str # serial dev\nbaudrate: int # baud rate\nevent_handler: typing.Callable[[EventType, any], None] # Event handler function\nloop=asyncio.get_event_loop() # your own event loop\nreconnect_attempts: int = 3 # how many times to try to retry command in case of failure\nencoding: str = 'ascii' # Nextion encoding\n```\n\n### Nextion parameters\n#### Encoding\nYou can update encoding on fly (This changes encoding of serial communication only):\n\n`client.encoding = 'latin-1'`\n\nGet current set encoding (Not fetched from the device)\n\n`print(client.encoding)`\n\n## Event handling\n\n```event_handler``` method in the example above will be called on every event coming from the display.\n\n| EventType        | Data                       | Data attributes                    |\n|------------------|----------------------------|------------------------------------|\n| TOUCH            | TouchDataPayload           | page_id, component_id, touch_event |\n| TOUCH_COORDINATE | TouchCoordinateDataPayload | x, y, touch_event                  |\n| TOUCH_IN_SLEEP   | TouchCoordinateDataPayload | x, y, touch_event                  |\n| AUTO_SLEEP       | None                       | -                                  |\n| AUTO_WAKE        | None                       | -                                  |\n| STARTUP          | None                       | -                                  |\n| SD_CARD_UPGRADE  | None                       | -                                  |\n\nFor some components in the Nextion Editor you need to check `Send Component ID` for required event.\n\n# Firmware uploading\nIf you installed the library you should have `nextion-fw-upload` command in your PATH.\n```bash\nnextion-fw-upload -h\n```\n\nOtherwise use next command in the root of the project:\n```bash\npython -m nextion.console_scripts.upload_firmware -h\n```\n\nUsage (check with option `-h`)\n```\nusage: nextion-fw-upload [-h]\n                         [-b {2400,4800,9600,19200,38400,57600,115200,230400}]\n                         [-ub {2400,4800,9600,19200,38400,57600,115200,230400}]\n                         [-v]\n                         device file\n\npositional arguments:\n  device                device serial port\n  file                  firmware file *.tft\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -b {2400,4800,9600,19200,38400,57600,115200,230400}, --baud {2400,4800,9600,19200,38400,57600,115200,230400}\n                        baud rate\n  -ub {2400,4800,9600,19200,38400,57600,115200,230400}, --upload_baud {2400,4800,9600,19200,38400,57600,115200,230400}\n                        upload baud rate\n  -v, --verbose         output debug messages\n```\n\n# Additional resources:\n- https://nextion.tech/instruction-set/\n- [PyPI](https://pypi.org/project/nextion/)\n\n                   GNU LESSER GENERAL PUBLIC LICENSE\n                       Version 3, 29 June 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n\n  This version of the GNU Lesser General Public License incorporates\nthe terms and conditions of version 3 of the GNU General Public\nLicense, supplemented by the additional permissions listed below.\n\n  0. Additional Definitions.\n\n  As used herein, \"this License\" refers to version 3 of the GNU Lesser\nGeneral Public License, and the \"GNU GPL\" refers to version 3 of the GNU\nGeneral Public License.\n\n  \"The Library\" refers to a covered work governed by this License,\nother than an Application or a Combined Work as defined below.\n\n  An \"Application\" is any work that makes use of an interface provided\nby the Library, but which is not otherwise based on the Library.\nDefining a subclass of a class defined by the Library is deemed a mode\nof using an interface provided by the Library.\n\n  A \"Combined Work\" is a work produced by combining or linking an\nApplication with the Library.  The particular version of the Library\nwith which the Combined Work was made is also called the \"Linked\nVersion\".\n\n  The \"Minimal Corresponding Source\" for a Combined Work means the\nCorresponding Source for the Combined Work, excluding any source code\nfor portions of the Combined Work that, considered in isolation, are\nbased on the Application, and not on the Linked Version.\n\n  The \"Corresponding Application Code\" for a Combined Work means the\nobject code and/or source code for the Application, including any data\nand utility programs needed for reproducing the Combined Work from the\nApplication, but excluding the System Libraries of the Combined Work.\n\n  1. Exception to Section 3 of the GNU GPL.\n\n  You may convey a covered work under sections 3 and 4 of this License\nwithout being bound by section 3 of the GNU GPL.\n\n  2. Conveying Modified Versions.\n\n  If you modify a copy of the Library, and, in your modifications, a\nfacility refers to a function or data to be supplied by an Application\nthat uses the facility (other than as an argument passed when the\nfacility is invoked), then you may convey a copy of the modified\nversion:\n\n   a) under this License, provided that you make a good faith effort to\n   ensure that, in the event an Application does not supply the\n   function or data, the facility still operates, and performs\n   whatever part of its purpose remains meaningful, or\n\n   b) under the GNU GPL, with none of the additional permissions of\n   this License applicable to that copy.\n\n  3. Object Code Incorporating Material from Library Header Files.\n\n  The object code form of an Application may incorporate material from\na header file that is part of the Library.  You may convey such object\ncode under terms of your choice, provided that, if the incorporated\nmaterial is not limited to numerical parameters, data structure\nlayouts and accessors, or small macros, inline functions and templates\n(ten or fewer lines in length), you do both of the following:\n\n   a) Give prominent notice with each copy of the object code that the\n   Library is used in it and that the Library and its use are\n   covered by this License.\n\n   b) Accompany the object code with a copy of the GNU GPL and this license\n   document.\n\n  4. Combined Works.\n\n  You may convey a Combined Work under terms of your choice that,\ntaken together, effectively do not restrict modification of the\nportions of the Library contained in the Combined Work and reverse\nengineering for debugging such modifications, if you also do each of\nthe following:\n\n   a) Give prominent notice with each copy of the Combined Work that\n   the Library is used in it and that the Library and its use are\n   covered by this License.\n\n   b) Accompany the Combined Work with a copy of the GNU GPL and this license\n   document.\n\n   c) For a Combined Work that displays copyright notices during\n   execution, include the copyright notice for the Library among\n   these notices, as well as a reference directing the user to the\n   copies of the GNU GPL and this license document.\n\n   d) Do one of the following:\n\n       0) Convey the Minimal Corresponding Source under the terms of this\n       License, and the Corresponding Application Code in a form\n       suitable for, and under terms that permit, the user to\n       recombine or relink the Application with a modified version of\n       the Linked Version to produce a modified Combined Work, in the\n       manner specified by section 6 of the GNU GPL for conveying\n       Corresponding Source.\n\n       1) Use a suitable shared library mechanism for linking with the\n       Library.  A suitable mechanism is one that (a) uses at run time\n       a copy of the Library already present on the user's computer\n       system, and (b) will operate properly with a modified version\n       of the Library that is interface-compatible with the Linked\n       Version.\n\n   e) Provide Installation Information, but only if you would otherwise\n   be required to provide such information under section 6 of the\n   GNU GPL, and only to the extent that such information is\n   necessary to install and execute a modified version of the\n   Combined Work produced by recombining or relinking the\n   Application with a modified version of the Linked Version. (If\n   you use option 4d0, the Installation Information must accompany\n   the Minimal Corresponding Source and Corresponding Application\n   Code. If you use option 4d1, you must provide the Installation\n   Information in the manner specified by section 6 of the GNU GPL\n   for conveying Corresponding Source.)\n\n  5. Combined Libraries.\n\n  You may place library facilities that are a work based on the\nLibrary side by side in a single library together with other library\nfacilities that are not Applications and are not covered by this\nLicense, and convey such a combined library under terms of your\nchoice, if you do both of the following:\n\n   a) Accompany the combined library with a copy of the same work based\n   on the Library, uncombined with any other library facilities,\n   conveyed under the terms of this License.\n\n   b) Give prominent notice with the combined library that part of it\n   is a work based on the Library, and explaining where to find the\n   accompanying uncombined form of the same work.\n\n  6. Revised Versions of the GNU Lesser General Public License.\n\n  The Free Software Foundation may publish revised and/or new versions\nof the GNU Lesser General Public License from time to time. Such new\nversions will be similar in spirit to the present version, but may\ndiffer in detail to address new problems or concerns.\n\n  Each version is given a distinguishing version number. If the\nLibrary as you received it specifies that a certain numbered version\nof the GNU Lesser General Public License \"or any later version\"\napplies to it, you have the option of following the terms and\nconditions either of that published version or of any later version\npublished by the Free Software Foundation. If the Library as you\nreceived it does not specify a version number of the GNU Lesser\nGeneral Public License, you may choose any version of the GNU Lesser\nGeneral Public License ever published by the Free Software Foundation.\n\n  If the Library as you received it specifies that a proxy can decide\nwhether future versions of the GNU Lesser General Public License shall\napply, that proxy's public statement of acceptance of any version is\npermanent authorization for you to choose that version for the\nLibrary.\n",
    "bugtrack_url": null,
    "license": "LGPL 3",
    "summary": "Nextion display serial client",
    "version": "2.0.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/yozik04/nextion/issues",
        "Homepage": "https://github.com/yozik04/nextion"
    },
    "split_keywords": [
        "nextion",
        "serial",
        "async",
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d2d02069065cbd3e1a14ec860d20658ef813925913c6dddbb275a4ddce3e9613",
                "md5": "69414853c654e492bc6c1a25e910664b",
                "sha256": "19bacdfca971f3ce13a2304482eea7ca00f6d672bd9d4270b6bcd66ee18fe923"
            },
            "downloads": -1,
            "filename": "nextion-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "69414853c654e492bc6c1a25e910664b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8.0",
            "size": 17592,
            "upload_time": "2024-07-28T19:30:09",
            "upload_time_iso_8601": "2024-07-28T19:30:09.681051Z",
            "url": "https://files.pythonhosted.org/packages/d2/d0/2069065cbd3e1a14ec860d20658ef813925913c6dddbb275a4ddce3e9613/nextion-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dcfd97ba3123df735624950c32c493d7dc3dfdb199b46577ceb9b9733b57768d",
                "md5": "8b6c1ec624bd2a419b2525f313ad4a12",
                "sha256": "a9030ca1f48a7365b22d7e07d46a917ff8d2cd3849b2764759cf405260bacd06"
            },
            "downloads": -1,
            "filename": "nextion-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8b6c1ec624bd2a419b2525f313ad4a12",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8.0",
            "size": 19069,
            "upload_time": "2024-07-28T19:30:11",
            "upload_time_iso_8601": "2024-07-28T19:30:11.592129Z",
            "url": "https://files.pythonhosted.org/packages/dc/fd/97ba3123df735624950c32c493d7dc3dfdb199b46577ceb9b9733b57768d/nextion-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-28 19:30:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yozik04",
    "github_project": "nextion",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "nextion"
}
        
Elapsed time: 0.36977s