Code: Select all
from machine import UART
from micropython import const
import ustruct
COMMAND_PAGE_ID = 'page'
COMMAND_COMPONENT = 'ref'
COMMAND_CLICK = 'click'
COMMAND_STOP_REFRESH = 'ref_stop'
COMMAND_REFRESH_START = 'ref_star'
COMMAND_GET = 'get'
COMMAND_GET_PAGE = 'sendme'
COMMAND_VISIBLE = 'vis'
COMMAND_TOUCH_ENABLED = 'tsw'
COMMAND_ADD_DATA = 'add'
COMMAND_ADD_DATA_BULK = 'addt'
COMMAND_CLEAR = 'cle'
COMMAND_RESET = 'rest'
R_COMMAND_TOUCH_EVENT = const(0x65)
R_COMMAND_PAGE_ID = const(0x66)
R_COMMAND_TOUCH_COORDINATE = const(0x67)
R_COMMAND_TOUCH_COORDINATE_SLEEP_MODE = const(0x68)
R_COMMAND_STRING_DATA = const(0x70)
R_COMMAND_NUMERIC_DATA = const(0x71)
R_COMMAND_AUTO_SLEEP = const(0x86)
R_COMMAND_AUTO_WAKE_UP = const(0x87)
R_COMMAND_SYSTEM_STARTUP = const(0x88)
R_COMMAND_SD_UPGRADE = const(0x89)
R_COMMAND_TRANSPARENT_TRANSMIT_FINISHED = const(0xFD)
R_COMMAND_TRANSPARENT_TRANSMIT_READY = const(0xFE)
class Nextion_Dieter(UART):
read_buffer = bytearray(100)
command = None
page = None
id = None
touch_event = None
x = None
y = None
data = None
def __init__(self, port=1,tx=4,rx=5, baud=9600):
# def __init__(self, port=1, baud=9600):
super(Nextion_Dieter, self).__init__(port, baud)
def send(self, command, *args):
length = len(args)
for arg in args:
length += len(str(arg))
write_buffer = bytearray(len(command) + length + 3)
write_buffer[0: len(command)] = bytes(command, 'ASCII')
index = 0
for arg in args:
if index > 0:
write_buffer[len(command) + index:len(command) + index + 1] = b','
else:
write_buffer[len(command) + index:len(command) + index + 1] = b' '
#write_buffer[len(command) + index:len(command) + index + 1] = b' '
write_buffer[len(command) + index + 1:len(command) + index + 1 + len(str(arg))] = bytes(str(arg), 'ASCII')
index += 1 + len(str(arg))
write_buffer[-3:] = b'\xff\xff\xff'
## print(write_buffer)
self.write(write_buffer)
def check_data(self, callback):
if self.any():
# print(self.any())
data = b''
while not data.endswith(b'\xff\xff\xff'):
data = b''.join((data, self.read(self.any())))
self.read_buffer = data
# print("erkannt------- ", self.read_buffer, "-----")
# print(len (data))
chars = (len (data))
# print(self.read_buffer)
# print(chars, "Zeichen")
index = 0
while index < (chars):
self.command = self.read_buffer[index]
index += 1
if self.command in [R_COMMAND_TOUCH_EVENT, R_COMMAND_PAGE_ID]:
self.page = self.read_buffer[index]
index += 1
if self.command == R_COMMAND_TOUCH_EVENT:
self.id = self.read_buffer[index]
index += 1
if self.command in [R_COMMAND_TOUCH_COORDINATE, R_COMMAND_TOUCH_COORDINATE_SLEEP_MODE]:
self.x = self.read_buffer[index:index + 2]
self.y = self.read_buffer[index + 2:index + 4]
index += 4
if self.command in [R_COMMAND_TOUCH_EVENT, R_COMMAND_TOUCH_COORDINATE, R_COMMAND_TOUCH_COORDINATE_SLEEP_MODE]:
self.touch_event = self.read_buffer[index]
index += 1
if self.command == R_COMMAND_STRING_DATA:
length = 0
while self.read_buffer[index + length:index + length + 3] != b'\xff\xff\xff':
length += 1
self.data = self.read_buffer[index:index + length]
index += length
self.page = self.data
if self.command == R_COMMAND_NUMERIC_DATA:
self.data = ustruct.unpack('<i', self.read_buffer[index:index + 4])[0]
index += 4
self.page = self.data
callback(command=self.command, page=self.page, id=self.id, touch_event=self.touch_event)
self.command = None
self.page = None
self.id = None
self.touch_event = None
self.x = None
self.y = None
self.data = None
return
if self.read_buffer[index: index + 3] == b'\xff\xff\xff':
index += 3
callback(command=self.command, page=self.page, id=self.id, touch_event=self.touch_event)
# Reset
self.command = None
self.page = None
self.id = None
self.touch_event = None
self.x = None
self.y = None
self.data = None
else:
index = 0
self.command = None
self.page = None
self.id = None
self.touch_event = None
self.x = None
self.y = None
self.data = None
print('ungültiger Befehl')
callback(command=self.command, page="ungültiger", id="Befehl", touch_event=self.touch_event)
return
Code: Select all
import machine
from machine import Pin
import utime
import nextion_dieter
#oder UART0 (RX Nextion) auf GP0 (TX Nextion) auf GP1
n = nextion_dieter.Nextion_Dieter(1)
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / 65535
#wenn vom Nextion gesendet wird 1 sonst 0
sendung = 0
Volt = 200
Amper = 10000
egal='egal'
#RX-Pin durch Interrupt abfragen
gesendet = machine.Pin(5)
def int_handler(pin):
gesendet.irq(handler=None)
global sendung
# print(sendung)
sendung = 1
gesendet.irq(trigger=machine.Pin.IRQ_RISING, handler=int_handler)
#gesendet.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_handler)#Pin.IRQ_RISING | Pin.IRQ_FALLING
#gesendet.irq(trigger=machine.Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=int_handler)
n.send('page','Daten')# wechselt zur Seite Daten
# #n.send('Daten.t0.txt="9ab"')# im Textfeld als Text einfügen
n.send('Daten.t0.txt="'+str(Volt)+'"')# im Textfeld als Text einfügen
# #n.send('Daten.x0.val=1')# im FloatFeld als formatierte Zahl einfügen
n.send('Daten.x0.val='+str(Amper)+'')# im FloatFeld als formatierte Zahl einfügen
n.send('vis', 'b0', 1)# im Button b0 sichtbar 1 unsichtbar 0
#n.send('b0.txt="Button1"')# Button beschriften
def nextion_callback(command, page, id, touch_event):
#print('command: %s page: %s id: %s, touch_event: %s' % (command, page, id, touch_event))
if (str(command)) == "101":
komando = int(command)
Seite = int(page)
id_num = int(id)
touch = int(touch_event)
print("erkanntes Komando =", komando, "auf Seite =", Seite, "id. =", id_num, "touch Ereigniss =", touch)
if (str(command)) == "113":
Zahl = int(page)
print("erkannte Zahl = ", Zahl)
if (str(command)) == "112":
Text = page.decode('utf-8')
print("erkannte Text = ", Text)
while True:
utime.sleep(1)
reading = sensor_temp.read_u16() * conversion_factor
temperature = round(27 - (reading - 0.706) / 0.001721, 2)
print(temperature)
n.send('Daten.t0.txt="'+str(temperature)+" grad"+'"')# im Textfeld als Text einfügen
n.check_data(nextion_callback)
if sendung == 1:
# n.check_data(nextion_callback)
gesendet.irq(handler=int_handler)
sendung = 0
Code: Select all
n.check_data(nextion_callback)
if sendung == 1:
# n.check_data(nextion_callback)
gesendet.irq(handler=int_handler)
sendung = 0
Code: Select all
# n.check_data(nextion_callback)
if sendung == 1:
n.check_data(nextion_callback)
gesendet.irq(handler=int_handler)
sendung = 0
if I
# n.check_data (nextion_callback)
Execute before the If condition everything is great.
It is no longer going well in the IF condition executed,
not every command is recognized.
In both cases the interrupt is executed.
Even pauses before and after the call do not change anything.