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

HMI
ino
(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");
   h1.getValue(myh1);
   Serial.println(myh1);
   x = myh1; 
   Serial.println(x);

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

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

 

 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;

  Serial.println(x);  

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");
  h1.getValue(&mynumber);
   Serial.println(mynumber);
  x = (int)mynumber;
  Serial.println(x);

  

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
0
h2 event
get h2.valÿÿÿ0
0
h3 event
get h3.valÿÿÿ0
0
"

 Indev2,

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

@lionel,

 

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.

@indev2

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


@lionel

 

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.


Steve.

 

#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,  
  NULL
};

void h1PopCallback(void *ptr)
{
    uint32_t mynumber = 0;    
    SoftSerial.print("\nh1PopCallback\n");
    h1.getValue(&mynumber);  
    SoftSerial.print(mynumber);  
    SoftSerial.print(" uint32_t\n");
    x = mynumber;    
}

void h2PopCallback(void *ptr)
{
    uint32_t mynumber = 0;    
    SoftSerial.print("\nh2PopCallback\n");
    h2.getValue(&mynumber);  
    SoftSerial.print(mynumber);  
    SoftSerial.print(" uint32_t\n");
    y = mynumber; 
}

void h3PopCallback(void *ptr)
{
    uint32_t mynumber = 0;    
    SoftSerial.print("\nh3PopCallback\n");
    h3.getValue(&mynumber);  
    SoftSerial.print(mynumber);  
    SoftSerial.print(" uint32_t\n");
    f = mynumber;  
}

void b5PushCallback(void *ptr){    
   SoftSerial.print("\nx = ");
   SoftSerial.print(x);
   SoftSerial.print("\ny = ");
   SoftSerial.print(y);
   SoftSerial.print("\nf = ");
   SoftSerial.print(f);
   SoftSerial.println();
} 


void setup() {
  // put your setup code here, to run once:
  SoftSerial.begin(115200); //Permet la communication en serial
  SoftSerial.println();
  SoftSerial.println("Slider Value Callback Test");
  nexInit();
  b5.attachPush(b5PushCallback, &b5);
  h1.attachPop(h1PopCallback);
  h2.attachPop(h2PopCallback);
  h3.attachPop(h3PopCallback);
}

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

image


@Patrick
@Indev2

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)

 

HMI
(1.11 MB)
ino
BIG THANK'SSS !!

 

@ Lionel,


Great work, pleased I could help.


HAPPY NEW YEAR to you and yours.


Steve.

Login or Signup to post a comment