Start a new topic

ESP32 and Nextion Library 0.7

I am testing out the usage of the nextion library in Arduino IDE for my esp32. 

the ESP32 has another hardware serial interface that can be called in the ino as:

HardwareSerial Serial1(2);

I know that in the nexconfig.h you have to change nexSerial to use Serial1, but there is something else you have to do for my config? The error that comes up is that "Serial1 was not defined in this scope"

So i tried to define it. In my main sketch, in the nexConfig.h as both:

extern HardwareSerial Serial1(2); 


HardwareSerial Serial1(2);

both give me "multiple definitions of Serial1" 

I have even tried adding a line to NexHardware.cpp and in both cases, "Serial1 was not declared in this scope"

at this point, i am trying to throw things at a wall and hope it sticks. Anyone got any ideas?


Steve (indev2) has already provided.

Sorry, your duty to read forum thread. 

Search Bar at Top, Thread titles,

Less than 1 month ago (under 2 weeks even)

Steve's post was just 5 days ago:

  but was indeed ESP8266 Serial/Serial1

It may provide the clues needed to config serial

 - ESP32 has many Serial Hardware ports

 - check ESP32 docs


If you are referring to:

I have already looked at it. I currently have a working demo for using Serial1 to print directly to the display. that is not an issue. What i am attempting to do is be able to use the Nextion Library instead of the "manual" Approach.

There is another thread that mentioned the "extern SoftwareSerial Serial1" where i tried HardwareSerial in place to see if it works. I assure you, I have been searching most of the morning trying to find a resolution before posting a topic.

I have plenty of toys but haven't yet played with ESP32.

So your documentation states 3 UARTs

Serial1 should be one of those 3 UARTs, (check docs to confirm)


  change line #define nexSerial Serial2 to #define nexSerial Serial1

  or which ever Serial is free and clear.

SoftwareSerial - is that not an AVR specific library? 

 - how would AVR code work on a 32-bit ESP32?

You will have to dig into the documentation for the ESP32 for serial specifics

relating to Arduino within the Arduino IDE

ie: ESP8286 Serial1 RX1 is not usable as that pin was purposed for SPI flash

    - no SPI flash on an ESP8266 and there is no running the SPI

    as such only TX is available on Serial1

    on an ESP8285 with internal flash, unless external SPI flash is present 

       then this pin is available for use in full RX/TX mode for Serial1

But on an ESP32, as there are 3 UARTs you could chose an alternate Serial for use.

ESP32 chip specifics may also have pins consumed by the board manufacture

   purposed for your other onboard peripherals.

But HardwareSerial Serial1(2); with a single param?

  My skimming leads me to think that you either

    defined RX pin to 2 for Serial1, or set Serial1 baudrate to 2 bits/sec

  - I don't know your pinout - but I think you will need to dig in

Once you nail down your Serial port to use, the change in nexSerial should suffice

dug in a bit further - espressif has your answers

you can indeed use

   HardwareSerial Serial1(1)

and use any of the available two pins but you need to config this Serial1(1) instance.

Not configured, well you haven't set baudrate, config, or which pins to use for it.

I would also upgrade to 0.90

  it may be indeed listed as experimental unstable

  but I think a better more complete lib


Thanks for the response. I have already changed the line in NexConfig.h. It says that Serial1 is not declared in this scope. I went to show documentation on what the (2) parameter for serial1 is, and think I may have stumbled on something.

on the ESP32, if i want to declare a hardware Serial port, I will write this:

HardwareSerial Serial1(2)

where Serial1 is just a variable name, and the "2" is UART2 (pins 16 and 17) So adding Serial1 to nexConfig wont work in my instance because I never declared what Serial1 is. At least, that is what I am thinking. In the other examples Serial1 was declared somewhere else. So that finally makes sense to me.

above is what I am referring to with UART2. So i did some digging in the arduino-esp32 repository in github and figured it out after staring at HardwareSerial.h

so, for NexConfig.h


#define nexSerial Serial2


#define nexSerial HardwareSerial(2)

This will default to pins 16 and 17. 

oops you had 2 comments while i was researching and typing

Ill do the upgrade also.

upgraded to 0.90 and NexUpload.cpp includes SoftwareSerial.h and my sketch doesn't have or need it. is it ok to comment out?

I am less certain there isn't a conflict

But a #define has a definite purpose.

I don't think you will find as much success with 

#define nexSerial HardwareSerial(2);

This would change all written cases of nexSerial into HardwareSerial(2) just before compile.

if you are going to actually use Serial2 then I would think the default

#define nexSerial Serial2 should even suffice.

My thought is that your HardwareSerial Serial2(2) if needed 

should be placed in the NexHardware.cpp::nexInit() function before line using nexSerial.begin(9600);

 - which you will probably desire to change the default baudrates on a more capable MCU.

NexUpload is not really needed,  it is an application to swap TFT files out on the fly.

But if not needed and not adjusting it to use HardwareSerial (over default SoftwareSerial) then

 - then remove it completely from the folder *.h/*.cpp

   (Arduino has habit of still using all files it finds in the folder)

 - you will also need to remove it from Nextion.h


I think it's safe to remove both NexUpload.cpp and NexUpload.h from the folder.

With an ESP32 you can do something similar with an OTA Nextion update, should you ever have the need.

The development was discussed here...

I did see it through to a nice clean method in the end :) the ack issue was sorted properly.


I tried the default NexConfig.h and Serial2 was not declared. I, then, added HardwareSerial Serial2(2); to the nexInit function of NexHardware.cpp and got the same result. 


bool nexInit(void)
    bool ret1 = false;
    bool ret2 = false;
	HardwareSerial Serial2(2);

    ret1 = recvRetCommandFinished();
    sendCommand("page 0");
    ret2 = recvRetCommandFinished();
    return ret1 && ret2;



 * Define nexSerial for communicate with Nextion touch panel. 
#define nexSerial Serial2


C:\Users\skaz\Documents\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexConfig.h:37:19: error: 'Serial2' was not declared in this scope


 #define nexSerial Serial2