Start a new topic

Here is a Quick Way to Send Float to Nextion Text Control from Arduino


float num = 36.2523;

String numTXT= String(num,2);//Will show 2 decimal places
char buf[10] = {0};


How about keeping it simple and embedding a new function into the library?

In NexText.h copy and paste this...  

     * Set float as text attribute of component.
     * @param floatTxt - float variable. 
     * @param dp - number of decimal places (maximum 6). 	 
     * @return nothing 
	void setFloatText(float floatTxt, uint8_t dp);	

  Then in NexText.cpp copy and paste this.... 

void NexText::setFloatText(float floatTxt, uint8_t dp)
	nexSerial.print(floatTxt, dp);	

  To use, just call like the other functions, example....  

float myFloat = 34.712;

t0.setFloatTxt(myFloat, 3); 
 //Where the 3 is the number of decimal places to print (Max 6)

outputs... t0.txt="34.172"ÿÿÿ

  Converting to String class then to 'C' char array, then back to String class then back to 'C' string can hardly be fast. Serial.print() can handle floats, simples:-)  

Good Idea... I saw some other routines, but look cumbersome.

Maybe the Nextion folks should go ahead and update the library, then it's simple. A lot of folks were/are confused as adding float values to the Text objects should be easy!   ;-)


and to whom can I redirect all upcoming support questions coming up only users dont really read the instructions? :-)

    - I send my float to n0.val, but my Nextion dosn't display any ... BUUUUUG ....
    - Why can't I calculate with my floats on Nextion side ... BUUUUUG ...
    - when I convert the float to a number variable, I only get the integer part ... HELP ...

such implementations should stay in users responsibility ...

The Nextion is an Integer device ...



Neutron may be an integer device, but we are talking about the libraries which do the heavy lifting.

Correction - Nextion... darn spell corrector...

There are I think six user contributed libs on forum.

If none meet needs, then another user will again contribute

Hi i've write the correction to the library but the arduino ide report me this error :

Arduino:1.8.5 (Windows 10), Scheda:"Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"


C:\Users\MrYoz\Desktop\Arduino\Forse ci siamo\ci_siamo_vicini\ci_siamo_vicini.ino: In function 'void loop()':


ci_siamo_vicini:191: error: 'class NexText' has no member named 'setFloatTxt'


       temp.setFloatTxt( myFloat, dp);




ci_siamo_vicini:210: error: expected ';' before 'nexLoop'






exit status 1

'class NexText' has no member named 'setFloatTxt'


How i can fix it.

Thanks for any answer.

Great Regards.

(1.62 MB)

When class NexText has no member named setFloatTxt

 - then create such setFloatTxt as a member if this is the desire

how i can do it ?

Your MCU programming language is your duty to learn - you chose it.

In Pascal


TMyClass = class(TObject)
      function setFloatText(r : Real) : String;

function TMyClass.setFloatText(r : Real) : String;
  setFloatText := Format('%.2f',[r]);


The above needs to be encapsulated into a t0.txt=""

so something like a

  print t0.txt="

  print setFloatText(floatval)

  print "ÿÿÿ

Thanks for your help if I can ask you a question. I have a temperature indicator on the second page, but when I change the page the loop for continuous and saturates the serial port. how can I stop the cycle when I change the page?  This is the cycle.

 void t4PopCallback(void *ptr){
  a: sensors.requestTemperatures();
int Temp_Now=(sensors.getTempCByIndex(0));
int temp1 = (Temp_Now) ;
    memset(buffer, 0, sizeof(buffer));
    itoa(temp1, buffer, 10);
    goto a;



@Fabrizio Pigliapoco

My instructions above (7 months ago) were pretty explicit.

Create the member "setFloatText" by copying the code above, exactly as provided, into exactly the library files quoted. NexText.h and NexText.cpp respectively.

The files should be in the directory....


Please note this is not an official addition, and as such is not supported by Itead.

Did you not just answer your own second question?

loop() is constantly running

 - regardless of what page changes on Nextion

 - regardless if temperature rises or falls.

One doesn't stop loop()

 - one defines proactive and purposefully when to do what

Then with such "purposefully defined definition"

 - one writes the code to exactly implement this purpose

Want something to occur only during specific pages

 - make a conditional definition based for such

Don't want something to run endless and saturate

 - make a conditional definition based for such so it doesn't saturate.

1 char/byte of output takes 10 bits of baudrate

  - therefore baudrate/10 is max bytes to send as a hard limit

Desire no saturation, don't push so close to or over such limit

Login or Signup to post a comment