ESP32 & Nextion Can't write to Nextion . Is it UART or Code?

This is my first attempt to write to a Nextion 3.2" display, but nothing shows up from the ESP32 . I was successful in creating my screen and defining some text and variable boxes on the Nextion. I set the Baud rate to 115200 on page 0 (only 1 page). When I run the ESP32 and utilize "Serial", it prints on the serial monitor. When I utilize Serial2.print nothing happens to the Nextion. I suspect it may be the UART. I have a separate USP powering the Nextion with common ground pins If I connect the TX on the Esp32 to the RX pin nothing shows up on the serial monitor. It's almost like the UART is not working. Here is the code:

#define ENHANCED  // 

#include "Nextion.h"

NexPage page0 = NexPage(0, 0, "page0");

NexText textNumber = NexText(0, 2, "CurrentControl");

NexText Amps = NexText(0, 1, "Amps"); //I named text block "Amps", ID=1
NexText Amps2 = NexText(0, 14, "Amps2");
NexNumber Temp =NexNumber(0,7,"Temp");

NexHotspot VoltsSet    = NexHotspot(0, 13, "VoltsSet"); //Couple of hotspots that do nothing now
NexHotspot AmpsSet    = NexHotspot(0, 14, "AmpsSet");
NexHotspot Volts12    = NexHotspot(0, 10, "Volts12");
NexHotspot Volts5    = NexHotspot(0,11, "Volts5");
NexHotspot Volts33    = NexHotspot(0, 12, "Volts33");



NexTouch *nex_listen_list[] =
{
  &VoltsSet,
  &AmpsSet,
  &Volts12,
  &Volts5,
  &Volts33,
  
  NULL
};



//----------------- S E T U P ----------------------------

void setup(void)
{
  nexInit(115200,16,17);      // Baud 115200,RX TX pins  Libary adjusted.
   Serial2.begin(115200,16,17);   //Yes the nexInit is supposed to set the pins but just desperate
 
}


//------------------- L O O P ----------------------------------------

void loop(void)
{
          Serial2.print(("Amps=\""));
          Serial2.print(("Nextion"));
          Serial2.print(("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);

         
        
 
  nexLoop(nex_listen_list);
 delay(1000);
 
}

Hello Redwire,
Support for Nextion on these fora is pretty much as follows:
You can follow the methods I set out in using Nextion displays with Arduino. My methods do not use the Nextion libraries, so while I am happy to offer help on using my methods I cannot offer anything very helpful for any Nextion library.
The original Nextion libraries are full of bugs. There is a link from my tutorial to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.
There's also a separate Easy Nextion Library by Seithan, his methods are different to mine, choose which works best for you.
Beyond that the odd person occasionally offers a bit of help but not much.

You are obviously using the Nextion library so I cannot help you much, but here goes:
I've not got an ESP32 either, so I have never tried to use one with a Nextion, however...

This is clearly wrong:

 Serial2.print(("Amps=\""));
   Serial2.print(("Nextion"));
   Serial2.print(("\""));
   Serial2.write(0xff);
   Serial2.write(0xff);
   Serial2.write(0xff);

This function is from my tutorial and illustrates what you need to do:

void HMI_startup_message() {
  Serial1.print(F("t0.txt=\""));
  Serial1.print(F("Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0"));
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

Note in particular

Serial1.print(F("t0.txt=\""));

you have nothing equivalent to t0.txt in your code.

I suggest you leave the baud rate at the default 9600 until you are sure you have code that works, changing the baud rate when you are having a problem is introducing one more potential thing that might be the cause of the problem.

I'm guessing you don't have an oscilloscope; if you are not sure if anything is being sent from a serial port connect an LED in series with a 1kOhm(ish) resistor between +5V (or +3V3 as appropriate) and the Tx pin. The LED should flicker each time data is sent. Make sure the cathode of the LED is on the Tx pin.

For some reason none of the links worked but I searched and downloaded the revised NexConfig.h, NexHardware.CCp and NexHardware.h files.

You noted that I didn’t have anything labeled t0. I changed the objectname to “Amps” instead of the default t0.

The program will not compile with multiple definitions errors of NexLoop, NexTouch, and so on…

 #define ENHANCED  
#include "Nextion.h"

NexPage page0 = NexPage(0, 0, "page0");

NexText textNumber = NexText(0, 2, "CurrentControl");

NexText Amps = NexText(0, 1, "Amps");
NexText Amps2 = NexText(0, 14, "Amps2");
NexNumber Temp =NexNumber(0,7,"Temp");

NexHotspot VoltsSet    = NexHotspot(0, 13, "VoltsSet");
NexHotspot AmpsSet    = NexHotspot(0, 14, "AmpsSet");
NexHotspot Volts12    = NexHotspot(0, 10, "Volts12");
NexHotspot Volts5    = NexHotspot(0,11, "Volts5");
NexHotspot Volts33    = NexHotspot(0, 12, "Volts33");



NexTouch *nex_listen_list[] =
{
  &VoltsSet,
  &AmpsSet,
  &Volts12,
  &Volts5,
  &Volts33,
  
  NULL
};
void HMI_startup_message() {
  Serial1.print(F("Amps.txt=\""));
  Serial1.print(F("Nextion "));
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}


//----------------- S E T U P ----------------------------

void setup(void)
{
  nexInit(115200);      // Baud 115200
   Serial2.begin(115200,16,17);
 
}

//------------------- L O O P ----------------------------------------

void loop(void)
{  
    HMI_startup_message() ;

 delay(1000);
 
}

I can’t help you with anything related to the Nextion libraries, beyond what I have already suggested, sorry.

The program will not compile with multiple definitions errors of NexLoop, NexTouch, and so on…

The somewhere you have multiple definitions of those, you need to find them and remove them, I can’t help with that, sorry.

I notice that you are now trying to print to serial port 1, before you were using serial port 2, have you changed the port the Nextion is connected to?

TBH, if you are struggling with the Nextion libraries you are very soon going to reach a point where there is no support here for you. I can help if you use my methods and general stuff but if you use the libraries then on this web site you probably won’t get much help because I rarely see anyone who knows about them contribute.

For some reason none of the links worked

Sorry about that, I've fixed the links. Sometimes this web site makes a mess of links and sometimes I don't notice that it has done so.

I went to the link

Just copy the three files to your Nextion library folder, and, if you want a different baud rate, add the baud rate as an argument to your nexInit() call. Everything else should work just as before.

I did. I don't know what else to edit, do, or change to simply print one line of text to Nextion.

This assumes you have the Nextion connected to serial port 2 (do ESP32s have a serial port 2?).

void setup(void)
{
   Serial2.begin(115200); // I suggest you change this to 9600 here and on the Nextion
}

void loop(void)
{ 
   HMI_startup_message() ;
   delay(1000);
}   

void HMI_startup_message() {
  Serial2.print(F("Amps.txt=\""));
  Serial2.print(F("Nextion "));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

The ESP32 doesn’t have 2 serial, it only has one, and it’s shared between its RX/TX pins and the USB communication. If you want to use both the Nextion display and the serial monitor, then you’ll have to attach the Nextion to pins other than the RX/TX and use software serial.

The ESP32 has three UARTS, two can be reached with the standard pins, one needs pin redefinition to work.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.