# Sending numeric value from Nextion display to a const long variable using EasyNextionLibrary

**URL:** <https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101>\
**Category:** Displays\
**Created:** [March 18, 2024, 5:59pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101 "2024-03-18T17:59:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 18, 2024, 5:59pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/1 "2024-03-18T17:59:17Z")

</div>

Hello people, I'm a newbie with arduino and trying to learn it.

I have a Nextion display and I am using the EasyNextionLibrary on Arduino IDE.

I have a sketch with a variable called

```arduino
const long interval = 10000

```

(It is located above the void setup(){ of the sketch)

I need to take the numeric value from a Number Box on the Nextion display with ID n61 (that use the Nextion numeric keypad) to replace 10000 value of the variable interval.

The EasyNextionLibrary has the function "readNumber();", so I think I have to use

```arduino
myNex.readNumber("n61.val"); 

```

to read whatever I have enter in the Number box n61.

So my question is how do I replace the codes

```arduino
const long interval = 10000

```

so that 10000 is replace with whatever was entered in the Nextion n61 Number box?

I have already managed the portion for my Arduino 2650 Mega to send to the Nextion display...

Thanks for you time..  
Cheers

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 18, 2024, 7:35pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/2 "2024-03-18T19:35:37Z")

</div>

`const long interval = 10000`

The 'const" qualifier means that the compiler won't let your code change the variable and should give an error if you try to do so.

If it's a time variable, it should be typed as unsigned long interval and the assignment is a simple

`unsigned long interval = myNex.readNumber("n61.val");`

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 18, 2024, 7:56pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/3 "2024-03-18T19:56:07Z")

</div>

Thanks for your response...

Replacing

```arduino
const long interval = 10000;  

```

with

```arduino
unsigned long interval = 10000; 

```

Project is working, however

```arduino
unsigned long interval = myNex.readNumber("n61.val");

```

is not working...

Is there a way that I can serial print the value entered in Nextion's n61number box to see if myNex.readNumber("n61.val"); is actually working?

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 18, 2024, 8:15pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/4 "2024-03-18T20:15:50Z")

</div>

> [@papagino](#):
>
> is not working...

Please explain "is not working".

If you print the value of interval after the assignment, what do you see?  
What do you expect?  
Is there a problem with using the interval in the Arduino code?  
Can you post the Arduino code section where you are reading the number and what you do with it?

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 19, 2024, 1:55pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/5 "2024-03-19T13:55:22Z")

</div>

Ok, if I use

```arduino
unsigned long interval = 10000; 

```

Then

```arduino
Serial.println(interval);

```

will be 10000

However

```arduino
unsigned long interval = myNex.readNumber("n61.val");

```

will not print anything.

I think the problem is that the Nextion is not sending the required information to the EasyNextion Library.

Do I have to write some code for the n61 number field in the Nextion Editor?

Thanks for your time...

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 19, 2024, 2:08pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/6 "2024-03-19T14:08:29Z")

</div>

> [@papagino](#):
>
> will not print anything.

Does it print 0?  
Does it print the "777777" error code?

> Do I have to write some code for the n61 number field in the Nextion Editor?

Certainly there has to be code to give it a value, but you seemed to say that there is a numerical value in the field. How is the number field getting a value?

What code is causing the Arduino to read the number? Is it in a trigger function?

> I think the problem is that the Nextion is not sending the required information to the EasyNextion Library.

What else is going on with the Nextion when the Arduino is trying to read the value.

I have seen issues when the Arduino is writing information very frequently with a background timed function (like updating a clock every second) it can effect the readings. Also if you are trying to detect a page after a page change to read a number on a new page, sometimes that can be an issue if there are frequent writes going on.

It may be best to write a simple piece of test code with a number box, a way to put a number in it, and a way to read it. If you can get to a simple Arduino code and HMI code I can test it.

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 19, 2024, 2:41pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/7 "2024-03-19T14:41:33Z")

</div>

**Does it print 0?**  
**Does it print the "777777" error code?**  
No, nothing is printed at all...

**Certainly there has to be code to give it a value, but you seemed to say that there is a numerical value in the field. How is the number field getting a value?**

I placed a default value of 10000 in the n61 number field. It get its value when you click on the number field, the Nextion numeric keypad open where you enter a value.

**What code is causing the Arduino to read the number? Is it in a trigger function?**  
No trigger function, this is the portion that I'm not sure... I though that the EasyNextion Library would parse the value using

```arduino
myNex.readNumber("n61.val");

```

but it doesn't. do I need to add some codes in the Nextion Editor for the n61 field???

**What else is going on with the Nextion when the Arduino is trying to read the value.**

A lot is going on, display values of TDS and Temperature sensor, timer, L293 motor controller...  
Complex machine I'm building, Top Secret LOL...

The sketch has over 600 lines and I am only half way in the project...

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 19, 2024, 2:52pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/8 "2024-03-19T14:52:53Z")

</div>

Also, the Nextion display has 7 pages... but n61 ID is only used once...  
Created a copy of my sketch and commenting lots of functions to see if something is causing the problem...

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 19, 2024, 3:05pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/9 "2024-03-19T15:05:20Z")

</div>

commented most of the sketch, I think the problem is that I need some codes on the Nextion side...

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 19, 2024, 3:15pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/10 "2024-03-19T15:15:02Z")

</div>

I may have to hire someone to help me with this project, I'm very good in electronics, but a novice in coding, getting old and my brain is swelling...LOL

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 19, 2024, 8:29pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/11 "2024-03-19T20:29:18Z")

</div>

No special code is needed on the Nextion. It's passively read by the Arduino. Are you certain you're on the page with the number field when you try to read it?

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 20, 2024, 2:19am UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/12 "2024-03-20T02:19:36Z")

</div>

No, not getting n61 parsing, even if on that page.  
The vscope is also set to global for that number field in Nextion editor.  
Arduino is sending data to text/number fields to Nextion ok, but it is the other way around that doesn't work...

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 20, 2024, 2:49am UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/13 "2024-03-20T02:49:46Z")

</div>

> Arduino is sending data to text/number fields to Nextion ok, but it is the other way around that doesn't work...

Is there anywhere else in your program where you successfully read a number field from the Nextion? Perhaps a reading in a trigger function?  
Is there any where in the program where you successfully read a text field?  
Can you read any trigger functions?

Do you have this line at the start of loop()  
`myNex.NextionListen();`

If you indeed have a systematic issue with reading anything from the Nextion, I would investigate the Nextion Tx to Arduino Rx connection.

Are there jumper cables or bread boards involved? Check the wiring carefully and perhaps redo it.

Does the Mega Serial port which is reading from the Nextion work with a different input source? Have you tested it with a feedback loop from another port on the Mega?

The Nextion serial is at 3.3v and its possible that the signal level is not high enough for the Mega. Do you have an oscilloscope or logic analyser to examine the Serial levels from the Nextion? Do you have a level shifter you can use between the Nextion and the Arduino?

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 20, 2024, 1:26pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/14 "2024-03-20T13:26:36Z")

</div>

**Is there anywhere else in your program where you successfully read a number field from the Nextion? Perhaps a reading in a trigger function?**  
**Is there any where in the program where you successfully read a text field?**  
**Can you read any trigger functions?**

All the buttons on Nextion are setup with trigger functions and Arduino is executing those functions accordingly.  
ex:

```arduino
void trigger1(){
  /* Create a button on Nextion
   * Write in the Touch Release Event of the button
   * this command: printh 23 02 54 01
   * Every time the button is pressed, the trigger1() function will run
   * and the code inside will be executed once
   */
   if(digitalRead(48) == LOW){
    digitalWrite(48, HIGH);
    Serial.println ("Power button ON");
    player.playSpecified(1);
        
  }else if(digitalRead(48) == HIGH){
    digitalWrite(48, LOW);
    Serial.println ("Power button OFF");
    player.playSpecified(2);
    digitalWrite(8, LOW); 
    digitalWrite(2, LOW); 
    myNex.writeNum("bt1.val", 0); 
    
  }

```

I have not been successful yet reading text or number from fields on the Nextion, however, I can send data to text and number field to the Nextion using the serial1.print method.  
Ex:

```arduino
Serial1.print("t1.txt=\""); // Changing the value of box t1
  Serial1.print(celsius); // celsius read the temperature from the sensor and output in Celcius
  Serial1.print("\"");
  Serial1.write(0xff); // We always have to send this three lines after each command sent to the nextion display.
  Serial1.write(0xff);
  Serial1.write(0xff);

```

**Do you have this line at the start of loop()**  
**`myNex.NextionListen();`**

Yes, first line after the void loop(){

**If you indeed have a systematic issue with reading anything from the Nextion, I would investigate the Nextion Tx to Arduino Rx connection.**

Nextion TX connected to Arduino RX (D19) and Nextion RX connected to Arduino TX (D18).  
Checked for continuity from Nextion to Arduino and all good.  
I am however using a Screw Terminal Block Shield Board for Arduino MEGA-2560 R3 and screw Terminal to D19 on Arduino test ok.

**Does the Mega Serial port which is reading from the Nextion work with a different input source? Have you tested it with a feedback loop from another port on the Mega?**

Serial1 is dedicated to Nextion display. I will need to try another serial port to see if there is a problem with Serial1...

**The Nextion serial is at 3.3v and its possible that the signal level is not high enough for the Mega. Do you have an oscilloscope or logic analyser to examine the Serial levels from the Nextion?**

I will check the Nextion TX with my oscilloscope to investigate potential problem with signal...

**Do you have a level shifter you can use between the Nextion and the Arduino?**  
Not sure what's this, is it something to step up signal from 3.3v to 5v?

Do I have to do anything on the Nextion side for it to send data to Arduino? Trigger commands or something?

Thanks again for your effort...

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 20, 2024, 2:43pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/15 "2024-03-20T14:43:21Z")

</div>

I think if the trigger functions are working correctly, then then Serial1 to Mega Rx is confirmed OK. You can send values OK so the Mega Tx is good as well. The screw block is a good interconnection method.

I have no idea why you can not read either text or a value.

Can you read a value from a field from within a trigger function?

Does each page have a page number in its preinitialization field like

```arduino
printh 23 02 50 0x // x = page number starting from 0

```

> I can send data to text and number field to the Nextion using the serial1.print method.

Can you send data using the Easy Nextion Library commands?

Can you run a Nextion Library example program like ReadAndWriteNumber.ino and it's associated HMI file? There is another basic one for a text read and write as well.

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 20, 2024, 3:29pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/16 "2024-03-20T15:29:06Z")

</div>

I've switch the Nextion to serial 2 and same behavior.  
Also, check the Nextion TX signal with scope, and I do get waveform whenever I hit a button on the screen.

I've noticed something however, if I comment the line:

```arduino
unsigned long interval = 10000; 

```

and add

```arduino
unsigned long interval = myNex.readNumber("n61.val");

```

then nothing is working anymore, no interaction between Nextion and arduino, buttons touch do nothing...

**Can you read a value from a field from within a trigger function?**  
Do I need to add a trigger function for text and number fields? Now I have trigger functions for buttons only!!!

All my trigger commands for the buttons have a Serial. print under it, so I can see on serial output:  
ex:

```arduino
Serial.println ("Power button ON");

```

**Does each page have a page number in its preinitialization field like**

**`**  **printh 23 02 50 0x // x = page number starting from 0**  **`**

All my buttons regardless of what pages they're on, have a different trigger that match the **`printh 23 02 50 xx`** and all buttons are working when triggered no matter the pages there on.

**Can you send data using the Easy Nextion Library commands?**  
Haven't tried yet, using

```arduino
Serial1.print("t1.txt=\""); ...

```

Will have to try it...

**Can you run a Nextion Library example program like ReadAndWriteNumber.ino and it's associated HMI file? There is another basic one for a text read and write as well.**

This also...

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [March 20, 2024, 3:45pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/17 "2024-03-20T15:45:42Z")

</div>

The Arduino code in a trigger function can read from any field on the page.

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 20, 2024, 3:54pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/18 "2024-03-20T15:54:32Z")

</div>

**The Arduino code in a trigger function can read from any field on the page.**

How do I do this, for example I have:

```arduino
void trigger23(){
  /* Create a button on Nextion
   * Write in the Touch Release Event of the button
   * this command: printh 23 02 54 17
   * Every time the button is pressed, the trigger23() function will run
   * and the code inside will be executed once
   */
  Serial.println ("SET button pressed");
  myNex.readNumber("t71.val");
  delay (100);
  Serial.println (???);
}  

```

How do I see and serial print the value I place in t71?

I haven't figure this yet...

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 20, 2024, 4:02pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/19 "2024-03-20T16:02:49Z")

</div>

Do I have the create a variable for the t71 value?  
Something like:

```arduino
number = myNex.readNumber("t71.val");  

```

Then print the variable like:

```arduino
Serial.println (number);

```

???

Maybe that's it???

---

<div class="post-metadata">

**Author:** ![papagino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papagino/32/1098795_2.png) [@papagino](https://forum.arduino.cc/u/papagino)\
**Post date:** [March 20, 2024, 4:21pm UTC](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101/20 "2024-03-20T16:21:19Z")

</div>

As soon as I use

```arduino
myNex.readNumber...

```

the program stop working???

[Next page](https://forum.arduino.cc/t/sending-numeric-value-from-nextion-display-to-a-const-long-variable-using-easynextionlibrary/1237101.md?page=2)
