Unofficial Nextion/TJC User Forum

Using Nextion Xfloat+Keyboard functions with Arduino (Update Nextion from Arduino)

Hey guys,

ive googled all day but am getting nowhere. The issue is showing a float number (1234.56) that is coming from an arduino to show on a Nextion Basic 2.4".
Most people have done workarounds by shooting them into a TEXTFIELD, and that will show them perfectly indeed. BUT that will just let me read the values coming from the arduino. I need to be able to use the Nextion Numeric Keyboard function to alter the value, send it to arduino, and save /calculate it there.

The problem that i am having is that if i send over the serial from arduino:
Serial.print(“x0.val=”);
Serial.print(bat); //BAT is the value that needs to come from the Nextion Keyboard
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

BUT this will not show the value in the x0 float field. it remains at 0.
If i now use the Keyboard to enter a value, then all is fine at first. It sends the correct input to arduino and will remain set in the x0 float, will accept changes and everything. But if i now leave the page and do something with the value (change the units) and come back, the page will have changed the units but the original value remains (eg i make a kilogram entry of 150, leave the page and change settings to pounds, and then go back to the entry page, then the display will show 150lbs instead of the converted 330lbs that arduino is also using (and sending in the above shown Serial.print. On the first update the arduino will notice the change to 150 and then i am totally off.
Same issue when powerdown/powerup and loading the latest values…it will load as 0 until i make the first manual entry, and then were back at where i started writing …

What i basically need is a way to WRITE a 1234.56 type float from Arduino to the x0.val field…can this be done? Is there a workaround to get the code there? I am not using the nextion library…

Thanks for your help

Store the value and its units in a variable. Store the global units setting in a global variable. In the pre-initialize routine check the global units and the value’s units. If they are different, convert the value, then display it.

Here’s the code I use on the Teensy (Arduino) side:

/// <summary>
/// PrintFloat
/// </summary>
/// <comment>
/// Gets decimal place of a float
/// </comment>
/// <param name="floatValue">Float</param>
/// <returns>Decimal places</returns>
int PrintFloat(float floatValue) {
	int decimalPlaces;
	float tempValue = floatValue;

	for (decimalPlaces = 0; decimalPlaces < 6; decimalPlaces++) 
	{

		if (tempValue == (long)tempValue)
		{ 
			break;
		}
		tempValue *= 10.0;  // Shift left one decimal digit
	}

	return decimalPlaces;
}

/// <summary>
/// Serial Print
/// </summary>
/// <comment>
/// Prints data to USB and Bluetooth serial ports.  Only one will be active.
/// </comment>
/// <param name="number">Float to send</param>
/// <returns></returns>
void SerialPrint(float number, int decimalPlaces = 0)
{
	int trim = PrintFloat(number);
	switch (serialId)
	{
	case 0:
	{
		Serial.print(number, trim);
		break;
	}
	case 1:
	{
		Serial1.print(number, trim);
		break;
	}
	case 2:
	{
		Serial2.print(number, trim);
		break;
	}
	case 3:
	{
		Serial3.print(number, trim);
		break;
	}
	case 4:
	{
		Serial4.print(number, trim);
		break;
	}
	}
}

I just display it in a textbox on the Nextion, so no conversion code for that end.

Hello.
Yes i can also get them to show in a textbox. But i cannot get a defined float value out of a keyboard in the textbox…so i need it in the x0 field not the t0 field…

Just create your own numberpad in the Nextion that uses a textbox for input. My rose engine project is here: GitHub - elfren/RoseEngine_SpindleAndAxis: Drive Rose Engine steppers for the spindle and up to 3 axes If I were doing this project now, I would make the numberpad a pop up instead of full screen.

I now have a rather complicated solution, but it works.
I need arduino to pre-initialize a global variable type “number”. I can now point the float field in defined format to show the number. All i need to do is add/subtract the missing decimal points. Quite a bit of IF/THEN in the conditioning of which field is feeding which at the moment, but it works.


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).