Using sprintf and nextion display

I am trying to use an Arduino Mega 2560 variable to change font and pco on Nextion TJC4832T035_011.

Using the direct mySerial.write both font and pco change correctly however but when I try to use
the sprintf code in Arduino no changes occur on the Nextion.

It would appear that I have a problem with the format of my sprint code, can anyone help please?

``
#include <SoftwareSerial.h>
SoftwareSerial mySerial(17, 16);//RX, TX
//................................................................ VARIABLES

int fonT = 0;
long int pco = 0;
char message [20];
int staT = 0;
//________________________________________________________________________________________________________________
//_______________________________________________________________________________________ VOID SETUP __________
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
//.............................................................. RTC START ............
}

//........................................................................... VOID NEXTION END ROUTINE ..............
void NexEnd() {
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
}

//_________________________________________________________________________________________________________________
//_________________________________________________________________________ VOID LOOP ___________
void loop() {
// FONT CHANGE

staT = 25;
fonT = 3;
pco = 65535;
sprintf(message, "n%d.font=%d", staT, fonT);
//sprintf(message, "n%d.font=%d/r", staT, fonT);
NexEnd();
delay(500);
Serial.print("FONT CHANGE MESSAGE "); Serial.println(message);
sprintf(message, "n%d.pco=%u", staT, pco);
//sprintf(message, "n%d.pco=%u\r", staT, pco);
NexEnd();
delay(500);
Serial.print("FONT CHANGE MESSAGE "); Serial.println(message);
/*
mySerial.print("n25.font=1");
NexEnd();
delay(100);
mySerial.print("n25.pco=63504");
NexEnd();
delay(100);
*/
}
e or paste code here

I do not see where you are printing/writing message to mySerial.

Why in the world are you using software serial on a Mega. There are 3 extra hardware serial ports on the Mega. Besides the fact that SoftwareSerial will not work on pins 16 and 17.

From the SoftwareSerial reference

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

Thank you for your responses.
David 2018
Thanks I cannot believe I missed that.
Added MySerial.print(message); to the code
and it works. Much Appreciated :grinning_face_with_smiling_eyes:

groundFungus. Pins 16 and 17 work just fine on the mega but I would prefer to use the built in
extra Serial ports on the Mega.
How do you set up these ports to use them.
Say I want to use Serial port 2. Do i just have to define the Tx & Rx pins as in SerialSoftware and then use "Serial2.print " instead of " mySerial.print"??

No, the pins for each serial port are fixed, you do not need to and cannot define them. To use serial port 2 you would put "Serial2.begin(115200);" (or a baud rate of your choice) in setup() and use Serial2.print() as you suggested.

Why don't you just print the command, without buffering?

 Serial.write('n');
 Serial.print(staT);
 Serial.print(F(".pco="));
 Serial.print(pco);

Here are two complete sketches for comparison

uint8_t staT = 4;
uint16_t pco = 255;
char message [20];

void setup() {
  Serial.begin(115200);
  sprintf(message, "n%d.pco=%u", staT, pco);
  Serial.print(message);
}
void loop() {}
Der Sketch verwendet 2978 Bytes (9%) des Programmspeicherplatzes. Das Maximum sind 30720 Bytes.
Globale Variablen verwenden 216 Bytes (10%) des dynamischen Speichers, 1832 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.
uint8_t staT = 4;
uint16_t pco = 255;

void setup() {
  Serial.begin(115200);
  Serial.write('n');
  Serial.print(staT);
  Serial.print(F(".pco="));
  Serial.print(pco);
}
void loop() {}
Der Sketch verwendet 1732 Bytes (5%) des Programmspeicherplatzes. Das Maximum sind 30720 Bytes.
Globale Variablen verwenden 184 Bytes (8%) des dynamischen Speichers, 1864 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.

Look at the massive size increase caused by using sprintf.

I just tried (Set Serial2.begin(9600);
Serial2.print("n31.font=1");
NexEnd();
Serial2.print("n31.pco=2016");
NexEnd();

that changes nothing on the Nextion
but
mySerial.print("n31.font=1");
NexEnd();
mySerial.print("n31.pco=2016");
NexEnd();
Works just fine

mySerial.write('n');
 mySerial.print(staT);
 mySerial.print(F(".pco="));
 mySerial.print(pco);

Thanks Wandall Tries this and it works just fine but I still cant get anything to work on
"Serial2.print"

Just exchange mySerial by Serial2, that should work.

You need a Serial2.begin() in setup too.

You are aware that Serial2 is the third serial port, not the second?

So the Nextion is connected to pins 16 and 17?

Looks like Serial2 is indeed pins 16, 17.


Please share your updated code. There is no "Set".

Got up this Morning and "redid" the whole Serial2 thing and it works like a charm now.

I thank you all for your help and patience.

:smiley:

1 Like