Start a new topic

retrieve data from nextion to uno

 Hi everyone,

i have a 3.2 inc nextion ( not enhanced) and a uno R3 and y try again and again to make them communicate. (usin library  itead nextion master)

My project use 3 sliders to set  integer on uno, in order to programm some fontcions (motor, timer..)

the problem is that i can't retrive those sliders's value, with the three way i've found  in the forum.

here i post my ino and tft file.

(sorry, frenchies are bad talented...)

(1.87 KB)

So you have declared Arduino side h1 as component 1 on page 1

what is wrong with using  h1.getValue to retrieve slider value?

Hi Patrick, thank's for your interest.
when i ask to retrieve the value of the slider, there is a mismatch between what is shown in the number field and what return the serial.
th H1 value is always -128 and the two other always return 0.


I am not the Arduino Expert - however

If you have your h1/h2/h3 declared global scope (slider nextion segment)

you should not redeclare it local scope in callback function.



void b5PushCallback(void *ptr) {
    uint32_t   myh1, myh2, myh3;

   Serial.println("h1 event");
   x = myh1; 

   Serial.println("h2 event");
   y = myh2;

  Serial.println("h3 event");
  f = myh3;


 Try something like the above for callback

 @ lionel,

You also need to keep your variable types consistent.

To the best of my knowledge, Nextion sends back a 4 byte signed long or int32_t.

So don't then expect that to just fit into a 2 byte int.

What purpose is the second print ?

 x = (int)mynumber;


int x ;  // poids  de repas par jour
int y ; // nombre  de repas par jour
int f ; // vitesse du moteur de filtrage/fontaine

uint32_t mynumber;
  Serial.println("h1 event");
  NexSlider h1 = NexSlider(1, 1, "h1");
  x = (int)mynumber;


Thank's Patrick, your code looks much better, not arduino expert but  not beginner like me ;)
but still retrieve 0..
in serial :
"".4ÿÿÿbkcmd=1ÿÿÿpage 0ÿÿÿh1 event
get h1.valÿÿÿ0
h2 event
get h2.valÿÿÿ0
h3 event
get h3.valÿÿÿ0


you're right and this is the point i failed, i'm trying to find a way to have the val the nextion send in 4 bytes little indian to be converted in a int wich i can use to set the motor (0-256).

i've seen that i can cast ti uint_32t in int but failed to...

but the most important !
Merry christmas (automaticly converted for  your own !!)


1 person likes this
no one have an idea?


Have you not tried indev2's code above?

Now he is your Arduino guru, and ESP, and ..



Sorry my friend, I thought we were done here.


I'll dig out my Uno and try to get things running for real.


What I was trying to say earlier, is don't bother with any conversion (although you could use some bit shifting techniques).


Using the 4 byte version somewhere else in your code shouldn't give you any problems.


I think I've figured that the second print was just to test if your cast to an int actually works.


Of course you found that it doesn't :(


Back soon.


your help means  you don't have to be  sorry ;)

if i anderstand, i don't need to convert for using this  variable for shield motors or else?

bit shifting techniques... oh well, no, ... "do not laugh" ....

yes, all serial printin are for debugging and testing.

well, i guess we 'll see those things after new years day :)



I hope google translate works for you.


First I must admit, I am no fan of the Arduino Nextion library.


However for the purpose of educational value, I worked through your issues and got the results you were looking for in your original questions.


To begin, a few points to note.


The Nextion library documentation does clearly state, that it is written and set up for devices with more than 1 Serial output. Arduino Mega and similar devices.


Other readers also take note !


You will need to set up SoftwareSerial in an Uno sketch while trying to use Arduino Nextion library. A 

working example is included in the sketch below. Take the output from pin 9 via an FTDI or similar USB to Serial device. View all your debug SoftSerial.print() in any Serial Terminal app.

The library files are also written in relatively advanced level syntax, for beginners any modifications can quickly lead to non working code.

Lionel, I have had to considerably modify your example to get this working.

Grouping several .getValue requests together in one function gives undesirable results because of the slow speed of the Serial connection and a lack of robust checks in the code.

In the picture, is a sample of the output you should get when everything is set up correctly.

The x y f print is sent when you touch b5, but not by doing more callbacks, I think you'll figure that out.


I stand corrected on the cast to int method which should be valid.

Hope this helps with your project.



#include <NexButton.h>
#include <NexDualStateButton.h>
#include <NexSlider.h>
#include <Nextion.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>

//#include <Servo.h>
//#include <OneWire.h>
//#include <Adafruit_MotorShield.h>
//#include <DallasTemperature.h>

SoftwareSerial SoftSerial(8, 9); TX data OUT pin 9

uint32_t x ;  // poids  de repas par jour
uint32_t y ; // nombre  de repas par jour
uint32_t f ; // vitesse du moteur de filtrage/fontaine
int t ; // température de commutation
int m ; // vitesse du moteur de nutrition
int z ; //remplacer par le temps en S qu'il faut  pour livrer 5 gr de croquettes
int d ; //Durée LED
char buffer[100] = {0};
//---------------------------------page 0------------------------------------------
NexButton b4 = NexButton(1, 4, "b4");
NexButton b5 = NexButton(1, 7, "b5");
//-------------------------------slider nextion------------------------------------
NexSlider h1 = NexSlider(1, 1, "h1");
NexSlider h2 = NexSlider(1, 2, "h2");
NexSlider h3 = NexSlider(1, 3, "h3");

NexTouch *nex_listen_list[] =
 &h1, &h2, &h3, &b5,  

void h1PopCallback(void *ptr)
    uint32_t mynumber = 0;    
    SoftSerial.print(" uint32_t\n");
    x = mynumber;    

void h2PopCallback(void *ptr)
    uint32_t mynumber = 0;    
    SoftSerial.print(" uint32_t\n");
    y = mynumber; 

void h3PopCallback(void *ptr)
    uint32_t mynumber = 0;    
    SoftSerial.print(" uint32_t\n");
    f = mynumber;  

void b5PushCallback(void *ptr){    
   SoftSerial.print("\nx = ");
   SoftSerial.print("\ny = ");
   SoftSerial.print("\nf = ");

void setup() {
  // put your setup code here, to run once:
  SoftSerial.begin(115200); //Permet la communication en serial
  SoftSerial.println("Slider Value Callback Test");
  b5.attachPush(b5PushCallback, &b5);

void loop() {
  // put your main code here, to run repeatedly:



At firs , i had to say thank you google translate ^^ you're right.

Well, making  one void per slider wasn't my direction at first (memory space..)

but, it seems the only solution which is working then i must  take five to both of you ^^.

in fact, i'm pretty late for thanking you,  hard working this few days..

so, you'll see my results :

therre some minor modifications (graphics..) but well working.

now, i'll try to save memory and take a look to enhanced to add  mor user friendly (time, eeprom saving-uploading setting)


(1.11 MB)


@ Lionel,

Great work, pleased I could help.

HAPPY NEW YEAR to you and yours.


Login or Signup to post a comment