Issue with uint_32 and nextion touchscreen

Hello,

I'm quite beginner with Arduino and C++ and I'm facing an issue that I don't understand at all.
In another project, I need the nextion returning some value after the release of a button.
The returned value is 4294951785 but I entered 500.25 in the nextion screen.

thus, i made a small code just to check. I'm using a number widget and a float widget in the nextion.
I understood the float widget is not a real float; it is just showing the . where we want.
Nextion is returning an uint_32. It shouldn't be an issue to return 500.25 but here is what the serial monitor shows:

15:36:09.979 -> [1577:0,3,b0]
15:36:12.244 -> recvRetNumber :500
15:36:12.244 -> recvRetNumber :4294951785
15:36:12.278 -> nombre: 500 (value coming from number widget)
15:36:12.311 -> float: 4294951785 (value coming from float widget)
15:36:12.311 -> float: 42949516.00

and here is the arduino code I wrote:

#include "Nextion.h"

NexNumber n0=NexNumber(0,1, "n0");
NexNumber x0=NexNumber(0,2,"x0");
NexButton start=NexButton(0,3,"b0");

NexTouch *nex_listen_list[] = {
  &start,
  NULL
};
#define ledPin 13

 void start_release(void *ptr)
  {
    uint32_t nombre = 0;  
 n0.getValue(&nombre);

 uint32_t floatt = 0; 
 x0.getValue(&floatt);  

 Serial.begin (9600);
  delay(250);
   digitalWrite(ledPin, HIGH);
   delay(2000);
   digitalWrite(ledPin, LOW);

   float x=floatt/100.00;
Serial.print("nombre: ");
Serial.println(nombre);
Serial.print("float: ");
Serial.println(floatt);
Serial.print("float: ");
Serial.println(x);

 
  }
  
void setup() {
 nexInit();
start.attachPop(start_release, &start);
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
}

void loop() {
   nexLoop(nex_listen_list);

}

If someone have an idea about what's going wrong, it will be very apreciated.
thanks for your help
Best regards

4294951785 translates to ‭FFFFC369‬ (hex). It turns out C369 (hex) is 50025. I believe C369 is being interpreted as a signed number and the sign bit is extended then it is stored into the uint32_t floatt variable. Not sure why as I am not familiar with this library.

It won't be easy I think. When the nextion is sending something to the arduino, it always finish by FFF.

I just try with a number: 952.87 it works. If I'm trying to enter 1234.25, I also get a strange value. Tried to drop a message on Nextion forum but it seems to be dead since a while.

We found why I have a strange return but how to solve it? I'm affraid it is far from my competencies and knowledge.

Anyway, thanks for the help.

Ravier17:
It won't be easy I think. When the nextion is sending something to the arduino, it always finish by FFF.

I just try with a number: 952.87 and it works.

We found why I have a strange return but how to solve it? I'm affraid it is far from my competencies and knowledge.

Anyway, thanks for the help.

Try entering 327.68 and let me know what happens.

Ok, will try that tomorrow afternoon. I don’t have both nextion and arduino under the hand right now.

thanks

Ravier17:
Ok, will try that tomorrow afternoon. I don't have both nextion and arduino under the hand right now.

thanks

Does it allow negative numbers at all? If not you could just mask the result.

Hello,

here is the serial monitor output when I'm entering 327.68:

14:38:01.211 -> recvRetNumber :4294934528
14:38:01.245 -> recvRetNumber :4294934528
14:38:01.245 -> nombre: 4294934528
14:38:01.278 -> float: 4294934528
14:38:01.312 -> float: 42949344.00

I did not try with negative numbers as, in the final project, the value will always be positive. In the final project, I'll enter the tare and the gross weight to get the net weight. It is the net weight who'll be sent to the arduino.
All the other calculation are based on the net weight.

Thanks

I believe this confirms my suspicion that there is a sign bit extension happening because the number you received is FFFF8000 hex. 8000 hex is actually 32767 and the MSbit is being extended. This is probably a bug or sloppy programming in the library.

If you don't need to enter a number greater than 655.35 (65535 or FFFF hex) then you could just mask the result. Here is an example:

 x0.getValue(&floatt); 
 floatt = floatt & 0xFFFF;

Try this and see what happens.

Hello,

I added the line as requested. i also added a conversion to HEX just to see. Here is the serial monitor result:

16:15:19.796 → ⸮:⸮⸮H⸮32767
16:15:23.594 → nombre: 32767
16:15:23.594 → nombre HEX: 7FFF
16:15:23.627 → float: 32767
16:15:23.627 → float: 327.67
16:15:23.661 → floatt HEX:7FFF

If I’m trying to enter something greater than 655.35 here is the result with 656.35:

16:16:27.562 → recvRetNumber :32767
16:16:27.562 → recvRetNumber :65635 (Arduino is receiving the right value but something is going
wrong. I should use this value and divide it by 100)
16:16:27.596 → nombre: 32767
16:16:27.629 → nombre HEX: 7FFF
16:16:27.629 → float: 99
16:16:27.663 → float: 0.99
16:16:27.663 → floatt HEX:63

And for info, here is the NexNumber.h

/**
 * @file NexNumber.h
 *
 * The definition of class NexNumber. 
 *
 * @author Wu Pengfei (email:<pengfei.wu@itead.cc>)
 * @date 2015/8/13
 *
 * @copyright 
 * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */
 
#ifndef __NEXNUMBER_H__
#define __NEXNUMBER_H__

#include "NexTouch.h"
#include "NexHardware.h"
/**
 * @addtogroup Component 
 * @{ 
 */

/**
 * NexNumber component.
 */
class NexNumber: public NexTouch
{
public: /* methods */
    /**
     * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name);
     */
    NexNumber(uint8_t pid, uint8_t cid, const char *name);
    
    /**
     * Get number attribute of component.
     *
     * @param number - buffer storing text returned. 
     * @return The real length of text returned. 
     */
    bool getValue(uint32_t *number);
    
    /**
     * Set number attribute of component.
     *
     * @param number - number buffer. 
     * @return true if success, false for failure. 
     */
    bool setValue(uint32_t number);
	
    /**
     * Get bco attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_background_color_bco(uint32_t *number);
	
    /**
     * Set bco attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_background_color_bco(uint32_t number);
	
    /**
     * Get pco attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_font_color_pco(uint32_t *number);	

    /**
     * Set pco attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_font_color_pco(uint32_t number);			
	
    /**
     * Get xcen attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_place_xcen(uint32_t *number);	

    /**
     * Set xcen attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_place_xcen(uint32_t number);			
	
    /**
     * Get ycen attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_place_ycen(uint32_t *number);	

    /**
     * Set ycen attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_place_ycen(uint32_t number);			
	
    /**
     * Get font attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t getFont(uint32_t *number);	

    /**
     * Set font attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool setFont(uint32_t number);			
	
    /**
     * Get lenth attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_number_lenth(uint32_t *number);

    /**
     * Set lenth attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_number_lenth(uint32_t number);	

    /**
     * Get picc attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_background_crop_picc(uint32_t *number);	

    /**
     * Set picc attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_background_crop_picc(uint32_t number);	

    /**
     * Get pic attribute of component
     *
     * @param number - buffer storing data retur
     * @return the length of the data 
     */
    uint32_t Get_background_image_pic(uint32_t *number);	

    /**
     * Set pic attribute of component
     *
     * @param number - To set up the data
     * @return true if success, false for failure
     */
    bool Set_background_image_pic(uint32_t number);			
};

/**
 * @}
 */

#endif /* #ifndef __NEXNUMBER_H__ */

thanks

Hi,

After searching a lot on internet, it appears the library is giving this issue. i did not find any solution to solve the issue thus, I tried another approach.

I changed my number box on the nextion to a text field where I write a number.
In the arduino side, I get the text via a string then I transform the string to float with atof.

Here is the code I wrote to do that:

#include "Nextion.h"

NexButton start=NexButton(0,2,"b0");
NexText t0=NexText(0,1,"t0");

char buffer[100] = {0};

NexTouch *nex_listen_list[] = {
  &start,
  NULL
};

void start_release(void *ptr){
  Serial.begin (9600);
  float number;
    memset(buffer, 0, sizeof(buffer));
    t0.getText(buffer, sizeof(buffer));
    number= atof(buffer);
    Serial.print("valeur string:");
    Serial.println(number);
}

void setup() {
  nexInit(115200);
  start.attachPop(start_release, &start);

}

void loop() {
    nexLoop(nex_listen_list);

}

It is for sure not the best way but at least, it is working.
This post may be flagged as solved (I don't know how to flag as solved).
Thanks for the help

Hi,

I faced the same problem when using original library from Nextion, its not stable all the time, cannot get number, or value from text on screen.

You can refer to this link to use library of this guy (jyberg):

I tried to use his library, and it works perfectly. I hope it can help your problem.

Hello,

@thiennd7, thanks for the info. I'll try with this library.
I tried so many thing that I can't remember if I tried the library or not.

Best regards