Using a variable to point to Nextion Box number

Using this Arduino code here to change the font size of n2.

"mySerial.print("n2.font=1");"

Works fine

Is there any way of using a variable in Arduino code to select the "n" number.

EG I tried;

int aaa = 2:
mySerial.print("n(aaa).font=1");

That wont work. Is there any other way of using a variable?

I have 12 " number boxes "on the Nextion and it would be nice to be able to select the boxes by using a variable.

eg

If (DogsFoot)== 4){
"mySerial.print("n(DogsFoot).font=1");"
}

int num = 2;
char message[20];
sprintf(message,"n%d.font=1",num);
mySerial.print(message);


Thanks Bob but it no work for me.
My Nextion box is n22.
Rx Tx connections are correct.
What am I Missing

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 9); /*Even though you can use the hardware serial port in this case I think it is better to
  leave the hardware serial open for debugging purposes*/
String sendThis = "";
//    RTC with Nextion
#include <Wire.h>
#include <ds3231.h>
int houR = 0;
int miN = 0;
int seC = 0;
int miN1 = 0;//   To change clock once a minute
struct ts t;
void setup() {
  Serial.begin(9600); //open the hardware serial port


  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
  //............................................................   SET UP RTC   ..........
  Wire.begin();
  DS3231_init;
  //..............................................................   SET TIME   ...........
  //t.hour = 13; t.min = 10; t.sec = 5; t.mday = 25; t.mon = 10; t.year = 2021;
  //DS3231_set(t);
}
//...........................................................................   VOID NEXTION END ROUTINE    ..............
void NexEnd() {
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);
}
//...........................................................................    VOID WRITE STRING TO NEXTION    ..........,
void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    mySerial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }
  NexEnd();
  miN1 = miN; //   Set Variable to last read Minute

}
void statioNtest() {
  if (seC < 30) {
    int num = 22;
    char message[20];
    sprintf(message, "n%d.font=1", num);
    mySerial.print(message);
     NexEnd();
    //char message[20];
    sprintf(message, "n%d.pco=2016", num);
    mySerial.print(message);
     NexEnd();
  }
  if (seC > 30) {
    int num = 22;
    char message[20];
    sprintf(message, "n%d.font=3", num);
    mySerial.print(message);
     NexEnd();
   // char message[20];
    sprintf(message, "n%d.pco=65535", num);
    mySerial.print(message);
     NexEnd();
  }

}


//.................................................................................   VOID STATION   .......................
void statioN() {
  if (seC < 30) {
    mySerial.print("n2.font=1");
    NexEnd();
    mySerial.print("n2.pco=2016");
    NexEnd();
  }
  if (seC > 30) {
    mySerial.print("n2.font=3");
    NexEnd();
    mySerial.print("n2.pco=65535");
    NexEnd();
  }
}
//................................................................................   VOID LOOP   ....................................
void loop() {

  DS3231_get(&t);
  houR = (t.hour);     // SET houR and miN and seC to current time
  miN = (t.min);
  seC = (t.sec);

  if ( miN1 != miN) {    //   Allows to print out only when Minute changes

    Serial.print(houR); Serial.print(" : "); Serial.println (miN);
  }
  sendThis = "n0.val="; //Build the part of the string that we know
  sendThis.concat(houR); //Add the variable we want to send//.....................Send"HOUR"
  writeString(sendThis); /*Use a function to write the message character by character to the Nextion becausemySerial.write(sendThis) gives you
                           an error due to a datatype mismatch*/

  sendThis = "";
  delay(1000);

  sendThis = "n1.val=";
  sendThis.concat(miN); //........................................................Send MINUTE
  writeString(sendThis);
  sendThis = "";
  delay(1000);

  //statioN();//        THIS WORKS
  statioNtest();//   No change to Nextion display
}

Sorry but just found the error.

Changed the number to 2 and it works just fine.

Tx for your help

Trying to learn a bit more about using sprintf for my project.
My aim is to send the Time from Arduino to Nextion using sprintf
Have tried this but I must not understand the concept just yet.

// Try use sprintf to send time
int num1 = 10;
int num2 = 34;

char message[20];
sprintf(message, " Time  %d  :  %d",num1,num2);                                             
mySerial.print("t3.txt=(message)");.................// not sure about this?
 NexEnd();
mySerial.print("t3.txt=(message)");

will print exactly what is in the quotation marks, not the contents of the message variable

Try

sprintf(message,"t3.txt= Time  %d  :  %d",num1,num2);
Serial.println(message);

Make sure that the message global is large enough to hold the full length of the text plus 1 character for the string terminator

I must be thick as 2 planks of wood.

Have set the nextion t0 to 25 characters

All rx and tx are connected correctly

Nothing changes on the display.

What am I missing please?

type or paste#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 9); 
 
String  sendThis = "";

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600); 

  Serial.println("Serial On"); 

}

void loop() {
int num1 = 12;
int num2 = 27;
 char message[25]; 
 sprintf(message,"t0.txt= Time  %d  :  %d",num1,num2);
mySerial.println(message);

mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);
} code here

I know nothing about Nextion displays, but what exactly is the Nextion expecting to receive ?

In the previous code you helped me with

if (seC > 30) {
    int num = 22;
    char message[20];
    sprintf(message, "n%d.font=3", num);
    mySerial.print(message);
     NexEnd();
   // char message[20];
    sprintf(message, "n%d.pco=65535", num);
    mySerial.print(message);
     NexEnd();

This code changed the Font size and the print colour on the Nextion display.

Thic code

tyvoid loop() {
int num1 = 12;
int num2 = 27;
 char message[25]; 
 sprintf(message,"t0.txt= Time  %d  :  %d",num1,num2);
mySerial.println(message);

mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);pe or paste code here

was expected to fill a Text field in the Nextion with:

'' Time 12:57 "

According to the Serial Monitor the arduino is sending the correct format to the Nextion
IE

tO.txt= Time 12:27

but nothing changes on the Nextion display the Text field remaions empty

In summary, the Arduino is sending the correct message but the Nextion does not display it. Is it, perhaps, expecting a Carriage Return or Newline at the end of the message, otherwise how would it know that the text was complete ?

You can put a Carriage Return in the sprintf() string by inserting \r or a Linefeed by inserting \n just before the closing quotation marks

 sprintf(message,"t0.txt= Time  %d  :  %d\r",num1,num2);

or

 sprintf(message,"t0.txt= Time  %d  :  %d\n",num1,num2);

t0.txt= Time 12:27
should be
t0.txt=" Time 12:27"

So something like this would be needed:
sprintf(message,"t0.txt=\" Time %d : %d\"",num1,num2);

You should test out commands by typing into the simulator, where you can immediately see the error if it is wrong.

Thank you for your replies.
Tx Arduarn I did not realise what that simulator was for.

The Simulator returns"1A FF FF FF " error " parse invalid variables" on all 3.

My Nextion connections
are working, as the Text colour and font change as per the command but the TEXT does not change
This is the current code I am trying;

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 9);

String  sendThis = "";

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);

  Serial.println("Serial On");

}

void loop() {


  mySerial.print("t0.font=2");//   This changes the TEXT size OK

  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);

  mySerial.print("t0.pco=2016");//   This changes the TEXT colour OK

  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);
  int num1 = 12;
  int num2 = 27;
  char message[25];
  //                None of these 3 lines effects any change to the text box
  //sprintf(message,"t0.txt= Time  %d  :  %d\r",num1,num2);
  //sprintf(message,"t0.txt= Time  %d  :  %d\n",num1,num2);
  sprintf(message, "t0.txt=\" Time %d : %d\"", num1, num2);
  mySerial.println(message);

  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);

}

That is because the text to be displayed has to be in quotes within the message, see reply #11, whereas the commands to change colour and text size do not have any text in them

Try what @arduarn suggested, ie

sprintf(message,"t0.txt=\" Time %d : %d\"",num1,num2);

The " will insert a quotation mark into the message

Hi Bob in the last code I sent I have already tried that as well as the 2 other variations

I have tried all 3 of these but none print out anything to the text box

 //sprintf(message,"t0.txt= Time  %d  :  %d\r",num1,num2);
  //sprintf(message,"t0.txt= Time  %d  :  %d\n",num1,num2);
  sprintf(message, "t0.txt=\" Time %d : %d\"", num1, num2);    *************************

On the Nextion Simulator all 3 come up as a
"parse invalid variables" error.

It may be that Nextion does not recognise the sprintf code.

Beats me

My apologies for missing that

The Nextion knows nothing of the sprintf(), only what it receives. What do you see if you Serial.print() message after

sprintf(message, "t0.txt=\" Time %d : %d\"", num1, num2);

Think I already tried that. Extract from code above

sprintf(message, "t0.txt=\" Time %d : %d\"", num1, num2);
  mySerial.println(message);

For the text box t0 in the Nextion Editor attribute pane, check that the length of the text box is at least as big as the longest text you will write to it.

I meant what I said

What do you see in the Serial monitor ?

From my Nextion tutorial Using Nextion displays with Arduino

struct CLOCK {
  int8_t hour;
  int8_t minute;
  int8_t second;
};
struct CLOCK clock;
//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d", clock.hour, clock.minute, clock.second);
  Serial1.print(F("t1.txt=\""));
  Serial1.print(timestring);
  Serial1.print(F("\""));
  Serial1.print(F("\xFF\xFF\xFF"));         // Sends 0xff 0xff 0xff, which tell the Nextion that it has a complete string of data
}

Modify as needed for your project.

Tx Arduarn I already had increased the Nextion t0 Max Char to 20 which should be plenty.

Bob, sorry I misread your instructions

Serial.print (message); displays " t0.txt=Time 12 : 27" on the Serial Monitor.

Now if I could only get it to print that on the Nextion.

Perry thanks I will work on your suggestion tomorrow and get back to you.

Thanks all for your help