ESP32 with Nextion display
ESP32 with Nextion display
Hello,
I'm new to micropython and I'm having some problems with my Nextion Display connected to my ESP32.
I already connected a BME280 sensor to the ESP32. From this sensor I'm able to read the values. I want to display these values on my Nextion Display.
The RX port of the display is connected to the TXD0 port of the ESP32.
I use the following code to connect to the display:
uart = UART(1, 9600)
uart.init(9600, bits=8, parity=None, stop=1, timeout=1000)
uart.write('temperature.txt=20')
uart.write('\xFF')
uart.write('\xFF')
uart.write('\xFF')
But nothing is happening. Any ideas? Maybe someone with experience with the nextion display and micropython?
I'm probably missing something simple, but I can't seem to find it.
Thanks in advance for the help.
Stijn
I'm new to micropython and I'm having some problems with my Nextion Display connected to my ESP32.
I already connected a BME280 sensor to the ESP32. From this sensor I'm able to read the values. I want to display these values on my Nextion Display.
The RX port of the display is connected to the TXD0 port of the ESP32.
I use the following code to connect to the display:
uart = UART(1, 9600)
uart.init(9600, bits=8, parity=None, stop=1, timeout=1000)
uart.write('temperature.txt=20')
uart.write('\xFF')
uart.write('\xFF')
uart.write('\xFF')
But nothing is happening. Any ideas? Maybe someone with experience with the nextion display and micropython?
I'm probably missing something simple, but I can't seem to find it.
Thanks in advance for the help.
Stijn
Re: ESP32 with Nextion display
Have you uploaded your HMI project to the Nextion display?
I don't think the uart.write("temperature.txt=20\xFF\xFF\xFF") will produce an output on the display.
You can test first on your PC, if it works, it will work with MicroPython too.
Nextion Editor Quick Start Guide
Nextion Instruction Set
I don't think the uart.write("temperature.txt=20\xFF\xFF\xFF") will produce an output on the display.
You can test first on your PC, if it works, it will work with MicroPython too.
Nextion Editor Quick Start Guide
Nextion Instruction Set
Re: ESP32 with Nextion display
Yes, the HMI is uploaded to the Nextion display with an SD-card.
I'm now testing this by connecting the ESP32 to my pc and executing this code. But the display doesn't change.
If I debug the nextion with the nextion editor. And I execute this to the simulator it works. To the COM device (my ESP32, it doesn't work)
I'm now testing this by connecting the ESP32 to my pc and executing this code. But the display doesn't change.
If I debug the nextion with the nextion editor. And I execute this to the simulator it works. To the COM device (my ESP32, it doesn't work)
Re: ESP32 with Nextion display
I've tested it, and it works as expected.
Here is the simple example code:
Use the bytearray for the end command sequence.
Tested with my MicroPython port.
Here is the simple example code:
Code: Select all
>>> import machine
>>> uart=machine.UART(1,tx=25,rx=26,baudrate=9600)
>>> end_cmd=b'\xFF\xFF\xFF'
>>> def send(cmd):
... uart.write(cmd)
... uart.write(end_cmd)
... time.sleep_ms(100)
... print("Response:", uart.read())
...
>>> send("get 65")
Response: b'qA\x00\x00\x00\xff\xff\xff'
>>> send('get "123"')
Response: b'p123\xff\xff\xff'
>>> send('get "Returned string"')
Response: b'pReturned string\xff\xff\xff'
>>> send("page 1")
Response: b'\x01\xff\xff\xff'
>>> send("sendme")
Response: b'f\x01\xff\xff\xff'
>>> send("page 2")
Response: b'\x01\xff\xff\xff'
>>> send("sendme")
Response: b'f\x02\xff\xff\xff'
Tested with my MicroPython port.
Re: ESP32 with Nextion display
Thanks. But I decided to use arduino and not micropython.
-
- Posts: 3
- Joined: Tue Mar 13, 2018 7:36 pm
Re: ESP32 with Nextion display
[quote=loboris post_id=25654 time=1519296117 user_id=923]
I've tested it, and it works as expected.
Here is the simple example code:
[code]>>> import machine
>>> uart=machine.UART(1,tx=25,rx=26,baudrate=9600)
>>> end_cmd=b'\xFF\xFF\xFF'
>>> def send(cmd):
... uart.write(cmd)
... uart.write(end_cmd)
... time.sleep_ms(100)
... print("Response:", uart.read())
...
>>> send("get 65")
Response: b'qA\x00\x00\x00\xff\xff\xff'
>>> send('get "123"')
Response: b'p123\xff\xff\xff'
>>> send('get "Returned string"')
Response: b'pReturned string\xff\xff\xff'
>>> send("page 1")
Response: b'\x01\xff\xff\xff'
>>> send("sendme")
Response: b'f\x01\xff\xff\xff'
>>> send("page 2")
Response: b'\x01\xff\xff\xff'
>>> send("sendme")
Response: b'f\x02\xff\xff\xff'
[/code]
Use the [b]bytearray[/b] for the end command sequence.
Tested with [url=https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo]my MicroPython port[/url].
[/quote]
Hey did it work with a wemos d1 mini? It freeze by my try?!
I've tested it, and it works as expected.
Here is the simple example code:
[code]>>> import machine
>>> uart=machine.UART(1,tx=25,rx=26,baudrate=9600)
>>> end_cmd=b'\xFF\xFF\xFF'
>>> def send(cmd):
... uart.write(cmd)
... uart.write(end_cmd)
... time.sleep_ms(100)
... print("Response:", uart.read())
...
>>> send("get 65")
Response: b'qA\x00\x00\x00\xff\xff\xff'
>>> send('get "123"')
Response: b'p123\xff\xff\xff'
>>> send('get "Returned string"')
Response: b'pReturned string\xff\xff\xff'
>>> send("page 1")
Response: b'\x01\xff\xff\xff'
>>> send("sendme")
Response: b'f\x01\xff\xff\xff'
>>> send("page 2")
Response: b'\x01\xff\xff\xff'
>>> send("sendme")
Response: b'f\x02\xff\xff\xff'
[/code]
Use the [b]bytearray[/b] for the end command sequence.
Tested with [url=https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo]my MicroPython port[/url].
[/quote]
Hey did it work with a wemos d1 mini? It freeze by my try?!
Re: ESP32 with Nextion display
On my side
[code]
MicroPython v1.9.3 on 2017-11-01; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import machine
>>> uart=machine.UART(1,tx=25,rx=26,baudrate=9600)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
[/code]
Maybe my firmware is too old?
What is your firmware version?
[code]
MicroPython v1.9.3 on 2017-11-01; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import machine
>>> uart=machine.UART(1,tx=25,rx=26,baudrate=9600)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
[/code]
Maybe my firmware is too old?
What is your firmware version?
Re: ESP32 with Nextion display
I came here when i had a similar problem on using micropython on ESP32 with nextion. I searched everywhere but to no avail. So i decided to use arduino. After understanding the code from https://randomnerdtutorials.com/nextion ... g-started/ i have successfully created the micropython equivalent below. I hope it helps someone. Please share if you optimise the code as i am a newbie to Python/Micropython.
Code: Select all
#I am Kobena Wiredu(kobenaw@yahoo.com)
#Just recently learnt python/Micropython and not an expert.
#Please email me if you optimise the code
# Below details from Nextion Editor
# I use 3.2 NX4024J032 Enhance nextion display but should work for yours too
#It is recommended to make the vscope attribute global
"""
// Declare your Nextion objects - Example (page id = 0, component id = 1, component name = "b0")
NexText tState = NexText(0, 3, "tState");
NexButton bOn = NexButton(0, 1, "bOn");
NexButton bOff = NexButton(0, 2, "bOff");
NexSlider h0 = NexSlider(0, 4, "h0");
NexText tSlider = NexText(0, 5, "tSlider"); #numeric slider used tSlider.val=h0.val set in nextion Editor
NexText tTempC = NexText(1, 3, "tTempC");
NexText tTempF = NexText(1, 6, "tTempF");
NexProgressBar jHumidity = NexProgressBar(1, 7, "jHumidity");
NexText tHumidity = NexText(1, 8, "tHumidity");
NexButton bUpdate = NexButton(1,10, "bUpdate");
"""
from machine import Pin,UART,PWM,ADC
import dht
import time
sensor = dht.DHT22(Pin(14))
#sensor = dht.DHT11(Pin(14))
led = Pin(13, Pin.OUT)
frequency = 5000
led_pwm = PWM(Pin(5), frequency)
#Function to read DHT22 Sensor by RandomNerdTutorials.com
#Modified slightly by Kobena
def read_sensor():
global temp, temp_percentage, hum,tempF
temp = temp_percentage = hum = tempF = 0
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum))
temp_percentage = (temp+6)/(40+6)*(100)
# uncomment for Fahrenheit
tempF = temp * (9/5) + 32.0
#temp_percentage = (temp-21)/(104-21)*(100)
hum = round(hum, 2)
return(msg)
else:
return('Invalid sensor readings.')
except OSError as e:
return('Failed to read sensor.')
end_cmd=b'\xFF\xFF\xFF'
global myframe
myframe = bytearray(7)
uart=UART(1,tx=25,rx=26,baudrate=9600)
def send(cmd):
global response1
uart.write(cmd)
uart.write(end_cmd)
time.sleep_ms(100)
response1 = uart.read()
#print("Response:", response1)
def send_and_get():
global processlist
#time.sleep_ms(100)
uart.readinto(myframe)
#print(myframe) #Enable this for debugging
processlist = list(myframe)
#print(processlist) #Enable this for debugging
#print(processlist[2]) #Enable this for debugging
#Just retrieve text on bON #Enable below for debugging
"""
send("get bOn.txt")
mylist = list(response1)
#delete the last 3 xffs
del mylist[-3:]
byteObj = bytes(mylist)
mychar = byteObj.decode("utf-8")
if (mychar == "pON"):
print('Everything is ok')
"""
while True:
while not uart.any():
if uart.any():
break
send_and_get()
#print(myframe)
#print(processlist)
#print(processlist[2])
#bOn is activated
if processlist[2] == 1:
led.value(1)
#tState.setText("State: on");
send("tState.txt=\"status:on\"")
print('LED is ON')
#bOff is activated
elif processlist[2] == 2:
led.value(0)
send("tState.txt=\"status:off\"")
print('LED is OFF')
#Slider
elif processlist[2] == 4:
send("get h0.val")
time.sleep_ms(100)
mylist = list(response1)
#delete the last 3 xffs
del mylist[-3:]
print(mylist)
print('Slider Position: ',mylist[1])
for duty_cycle in range(0, 100):
led_pwm.duty(mylist[1])
time.sleep(0.005)
elif processlist[2] == 10:
read_sensor()
#send("tTempC.txt=\"28.2\"")
send("tTempC.txt=\""+str(temp)+"\"")
send("tTempF.txt=\""+str(tempF)+"\"")
send("tHumidity.txt=\""+str(hum)+"\"")
send("jHumidity.val="+str(int(hum)))
else:
read_sensor()
send("tTempC.txt=\""+str(temp)+"\"")
send("tTempF.txt=\""+str(tempF)+"\"")
send("tHumidity.txt=\""+str(hum)+"\"")
send("jHumidity.val="+str(int(hum)))
Re: ESP32 with Nextion display
See full project codes:
https://github.com/kobena/ESP32_Micropython_Nextion.git
https://github.com/kobena/ESP32_Micropython_Nextion.git