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

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

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

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

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

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");

Thanks for your response...

Replacing

const long interval = 10000;  

with

unsigned long  interval = 10000; 

Project is working, however

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?

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?

Ok, if I use

unsigned long  interval = 10000; 

Then

Serial.println(interval);

will be 10000

However

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

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.

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

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

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

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

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

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?

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

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?

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:

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:

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

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

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.

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:

unsigned long  interval = 10000; 

and add

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:

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

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

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

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

How do I do this, for example I have:

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

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

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

Then print the variable like:

Serial.println (number);

???

Maybe that's it???

As soon as I use

myNex.readNumber...

the program stop working???