Unofficial Nextion/TJC User Forum

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);
 
}

A reliable baud rate is often dependent on the clock frequency of the microprocessor so only certain ones should be used as a test try 9600 and see if it works.
Now your doing something very similar to me that is changing voltages and currents, I am sending it to two solar inverters you may find my code useful I have up/down buttons for this that increment and decrement a value on the nextion then that is sent to the inverters. You can find it here Inverter Remote Screen with a Nextion - AEVA Forums

Also look at the commands for baud rate changes on the nextion best to set this when the page first loads as a command on the nextion.

Thanks for your reply, I will try what you said and will check out what this link says.

Double check what the configuration is set to within the NexConfig.h file. Possibly have to Change - #define nexSerial Serial to #define nexSerial Serial2.

Also never used nexInit(); mine works perfectly fine with Serial2.begin();

Thank you very much for your reply, I will check it.

Debugging Serial communication is easiest with a cheap logic analyzer ($8 on Amazon for a Chinese Saleae clone). This will show you within seconds if the ESP32 is sending correctly.

hi,
first what I have notized is that nexion not necessary start with defined baud rate even if new “defaut” is set. that why I keep default 9600 and then chane it higher. when I connect. I scan baud rate as it can be 9600 ot previously set (nextion not re-started) then I had also problems wit Esp harware serial when same time using debugger / pc connect. so use software serial for nextion. I have enhanced nextion library youcan find hints from there or use it if seen sutable… GitHub - jyberg/Enhanced-Nextion-Library: Enhanced Nextion library for Arduino, NodeMcu, Esp8266,...


This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).