Package: nextion
Nextion serial clientLightweight Python 3.6+ async library to control Nextion displays.InstallationPypipip3 install nextionSimple usage:importasyncioimportloggingimportrandomfromnextionimportNextion,EventTypeclassApp: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)asyncdefevent_handler(self,type_,data):iftype_==EventType.STARTUP:print('We have booted up!')eliftype_==EventType.TOUCH:print('A button (id:%d) was touched on page%d'%(data.component_id,data.page_id))logging.info('Event%sdata:%s',type,str(data))print(awaitself.client.get('field1.txt'))asyncdefrun(self):awaitself.client.connect()# await client.sleep()# await client.wakeup()# await client.command('sendxy=0')print(awaitself.client.get('sleep'))print(awaitself.client.get('field1.txt'))awaitself.client.set('field1.txt',"%.1f"%(random.randint(0,1000)/10))awaitself.client.set('field2.txt',"%.1f"%(random.randint(0,1000)/10))awaitself.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 parametersurl: 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 encodingNextion parametersEncodingYou 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 handlingevent_handlermethod in the example above will be called on every event comming from the display.EventTypeDataData attributesTOUCHTouchDataPayloadpage_id, component_id, touch_eventTOUCH_COORDINATETouchCoordinateDataPayloadx, y, touch_eventTOUCH_IN_SLEEPTouchCoordinateDataPayloadx, y, touch_eventAUTO_SLEEPNone-AUTO_WAKENone-STARTUPNone-SD_CARD_UPGRADENone-For some components in the Nextion Editor you need to checkSend Component IDfor required event.Firmware uploadingIf you installed the library you should havenextion-fw-uploadcommand in your PATH.nextion-fw-upload-hOtherwise use next command in the root of the project:python-mnextion.console_scripts.upload_firmware-hUsage (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 messagesAdditional resources:https://nextion.tech/instruction-set/PyPI