Start a new topic
Not Taken

how can i Send the value of a number from display to arduino

Hello!

I need to send the value of a number from the display(i can change this number from the display) to the arduino. I read other post, and i see that i need to write in the nextion ide, get a.val (for example).
how can catch this value in the arduino sketch, at the button function?

For example, in the display i have a button with this code:

a.val=a.val+1

get a.val


and in arduino i have this:

#include <Nextion.h>

SoftwareSerial HMISerial(10,11);

NexButton b0 = NexButton(0, 1, "b0");

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

void b0PopCallback(void *ptr)

{

    uint32_t a;

    recvRetNumber(&a); // get numeric variable a

    dbSerialPrintln(a);

}

 

void setup(void)

{

    nexInit();

    b0.attachPop(b0PopCallback, &b0);

}

 

void loop() {

    nexLoop(nex_listen_list);

}


I want to print in serial monitor the value of the number, but nothing happens when i press the button.


I do the same with a text of the length 4.

To get the text just do this when press the button:

DisplayText.getText(text_char, 4);


declaration of the text:

NexText DisplayText= NexText(9, 14, "DisplayText");


and your variable to store the text "text_char":

char text_char[4];

Ok alex, i will test it, but i really need is to work with numbers, no texts, i got a lot of variables. In the display, you need to write something to send the variable to arduino?

However i will test that! Thanks!

I am not sure if "NexText" works also for Number Components.


You don't need to write something to send it to the Arduino.

The Arduino will ask for it:

DisplayText.getText(text_char, 4);


You just need to do the declaration of the Component in your arduino sketch:

NexText DisplayText= NexText(9, 14, "DisplayText");

Page ID, Component ID and Component Name


Then you need a variable where you want to store it at the Arduino:

Define it:

char text_char[4];


Then get the text (or maybe number) from the Display:

DisplayText.getText(text_char, 4);


And print it in serial:

Serial.print("The Text is: ");

Serial.println(text_char);



Nextext doesn't work with numbers.

I Thought that in the display i need to write for example "get text.val" to send it to arduino.

I will test this and then i tell you what happend! thank you!


Hi Alex, I did all that you told me but it doesn't work.

In the serial monitor only shows me this when I press the text.

The text value in the Nextion IDE is "test".

I tried only with a text, with the option "Send component ID" in touch release event.

Then, I tried with a button, and in the button function I write the code, but it doesn't show in the serial monitor the text "test".

I copy the two codes.

The first code is:

#include "Nextion.h"

NexText DisplayText= NexText(0, 1, "DisplayText");

char text_char[4];

char buffer[100] = {0};

NexTouch *nex_listen_list[] =

{

    &DisplayText,

    NULL

};

void DisplayTextPopCallback(void *ptr)

{

  DisplayText.getText(text_char, 4);

  Serial.print("The Text is: ");

  Serial.println(text_char);

}

void setup(void)

{

    Serial.begin(9600);

   

    nexInit();

    DisplayText.attachPop(DisplayTextPopCallback);

    dbSerialPrintln("setup done");

}

void loop(void)

{

    nexLoop(nex_listen_list);

}

And the code with de button is:

#include "Nextion.h"

NexText DisplayText= NexText(0, 1, "DisplayText");

NexButton b0 = NexButton(0, 2, "b0");

char text_char[4];

char buffer[100] = {0};

NexTouch *nex_listen_list[] =

{

    &DisplayText,

    &b0,

    NULL

};

void DisplayTextPopCallback(void *ptr)

{

  DisplayText.getText(text_char, 4);

  Serial.print("The Text is: ");

  Serial.println(text_char);

}

void b0PopCallback(void *ptr)

{

  DisplayText.getText(text_char, 4);

  dbSerialPrintln("The Text is: ");

  dbSerialPrintln(text_char);

}

void setup(void)

{

    Serial.begin(9600);

   

    nexInit();

    DisplayText.attachPop(DisplayTextPopCallback);

    b0.attachPop(b0PopCallback);

    dbSerialPrintln("setup done");

}

void loop(void)

{

    nexLoop(nex_listen_list);

}

The two codes shows me the same thing.

I am now reviewing all of the Feature Requests, this will take some time, patience please.

This is not a feature Request - print va0.val

This problem was solved. If anyone need the solution please contact me. Thanks

How do I contact you,  I would like to know the answer.

Thank you

Chris


There are two ways to get a number from the display to the mcu

All Nextion commands are data terminated with 0xFF 0xFF 0xFF in both directions


One method is if I want the value from va0.val, then I will send  "get va0.val" over serial and then read the response

This is exactly what the iteadlib Arduino Nextion library does


An easier method is to actually use the library

int32_t mynumber;

NexNumber numonNextion = NexNumber(0,1,"n0");

numonNextion.getValue(&mynumber);


The second method is to push the number via a button press

Inside the button Touch Press Event place

print n0.val


This method again sends the number but in a non formatted method,

4 bytes in little endian format

- but you'll have to do the coding to catch the return value.


Please refer to the IteadLib Arduino Nextion Library code

and the Nextion Instruction Set


(Edited to fix error in declaration - no your not mad, it was wrong before)


2 people like this

Thank you patrick, the method that I used, was:


"An easier method is to actually use the library

int32_t mynumber;

NexNumber numonNextion(0,1,"n0");

numonNextion.getValue(mynumber);

"

This works perfect. 

Chris, if you need to know more, please answer here, or email me, i.fontana@smart-bio.com.ar


Patrick

there is 2 or 3 different nextion library, what library are you have used?
could you give me a full arduino code exemple and nextion code exemple about command get value?


thank you


marcos




i m use this library, is so easy.... maybe you can help get value with this library

zip

With the Itead lib Arduino Nextion Library

 

#include <Arduino.h>
#include <Nextion.h>

// declare number variable Arduino side and initialize
int32_t myNumber = 345678 ;

// Nextion touch even listen list - nothing for this time
NexTouch *nex_Listen_List[] = {
  NULL
};

// function that will send myNumber to n0
void sendmynumber(void)
  NexNumber  myNextionNumber = NexNumber(0,1,"page0.n0");
  myNumber=2300*3;
  myNextionNumber.setValue(myNumber);
}

// minimal Nextion setup
void setup() {
  nexInit();
}

// main Arduino loop
void loop() {
   nexLoop(nex_Listen_List);
   sendmynumber();
   delay(5000);
}

 

So in the above, the number is updated every 5 seconds.

But seriously, I have other things I need to program momentarily


1 person likes this
ok Patrick, thank you so much, this simple example help a lot
I understand, sorry boring you



 

Login or Signup to post a comment