Nextion Variable Trouble

Someone on this forum recommended a Nextion screen in an old thread, and since I wanted to try one with an editor (having manually created UI first), I thought it’d be great.

The library is a mess, support is nonexistent. I cannot get it to read global variables and return from getText and getValue.

I dunno what to do at this point, I feel like I’ve run through everything.

I’ll post my very basic attempt here and maybe someone will have a recommendation:

#include <NexVariable.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexButton.h>



NexVariable pickupName = NexVariable(0,7,"pickupName"); // txt 50
NexVariable blocksTotal = NexVariable(0,10,"blocksTotal"); // val

NexButton autoNewWind = NexButton(3, 6, "b3"); // autoNewWind


char buffer[100] = {0};
uint32_t number = 0;

NexTouch *nex_listen_list[] = 
{
  &autoNewWind,
  &pickupName,
  &blocksTotal,
  NULL
};


void autoNewWindPopCallback(void *ptr)
{
    dbSerialPrintln("Total Blocks:");
    
    blocksTotal.getValue(&number);
    dbSerialPrintln(number);

    dbSerialPrintln("Pickup Name:");

    memset(buffer, 0, sizeof(buffer));
    blocksTotal.getText(buffer, sizeof(buffer));
    dbSerialPrintln(buffer);


}



void setup(void) {

    nexInit();
    autoNewWind.attachPop(autoNewWindPopCallback);
    dbSerialPrintln("Ready"); 
}



void loop(void) {

nexLoop(nex_listen_list);

}

All of the page #s, component IDs, and names are correct. They are programed and uploaded via the Nextion Editor first, then the Nextion screen is attached to an Arduino Mega 2560 via Serial2. Everything works fine, I get callbacks from buttons when I click them, but I absolutely cannot get any values / text from variables.

The variables are global, and located on the initial page 0 (the button is being pressed on page 6, it shouldn’t matter though).

What shall I do here? I don’t get it, is there a way around their crappy library?

What shall I do here? I don't get it, is there a way around their crappy library?

Hello JTBennett,

I'm trying to say this without sarcasm but I'm struggling. Maybe you should try the methods I set out in 'Using Nextion displays with Arduino'. No crappy Nextion library.

Do I need to change the title to something like 'Using Nextion displays with Arduino and no crappy libraries'?

Come back if you need help.

I clicked on it before and the first page looked like it was just very basic information and referencing Arduino support, so I assumed it wouldn't be what I was looking for. You probably should change the title actually..

jtbennett:
I clicked on it before and the first page looked like it was just very basic information and referencing Arduino support, so I assumed it wouldn't be what I was looking for. You probably should change the title actually..

What title do you suggest?

I hope you find it useful, you will have to change your code to suit my methods.

Don't change the title...Its fine.

Using the forums "SEARCH" facility finds "nextion" and related issues just fine.
I just used it to do some quite specific number counts for Arduino themselves for issue reports.

Bob.

PerryBebbington:
What title do you suggest?

I hope you find it useful, you will have to change your code to suit my methods.

"Using Nextion Without Crappy Nextion Libraries" will be sufficient for anyone else as frustrated as I am. I used the search, I scanned their help docs, I trawled around Google. All the help I came across was related to working with their libraries, and most of it was a dead end. I found someone had made an "Enhanced" Nextion library to address the issue of the official one not even working with global variables to start with, but I'm still not getting a proper value reading from my global variables in spite of them being perfectly functional on the TFT.

Anyway...I'm going to read through your thread now, thank you. My main need is to just trigger the reading of variable values/text when certain buttons are pressed. That's all - thought it'd be straightforward but apparently Nextion is a sleazy company trying to sell you fonts for $200 and deleting any questions on their forums that make light of their useless library and lack of support.

My main need is to just trigger the reading of variable values/text when certain buttons are pressed.

Obviously I am saying this without seeing your code or knowing what you are trying to achieve; I think you are going to have to change the way your project works and move your variables to the Arduino, with the Nextion sending instructions that indicate that a button has been pressed, then the Arduino acting on that.

PerryBebbington:
Obviously I am saying this without seeing your code or knowing what you are trying to achieve; I think you are going to have to change the way your project works and move your variables to the Arduino, with the Nextion sending instructions that indicate that a button has been pressed, then the Arduino acting on that.

Yeah, you're correct about that - I actually just decided to calm down and rethink things. It's a pain because it's an 11 page project with a number of user-set parameters.

You've pointed out one method of settings the variables in Arduino but I had thought of going the other way with it and just sending all the variable values from the button's release script editor (through the Nextion editor). I see you can do something like get t0.txt, but I'm not sure if that would send it over serial.

It's a pain because it's an 11 page project with a number of user-set parameters.

My biggest project is my heating controller, which has 5 pages. The controller uses the methods I set out in the tutorial and works just fine. The only variables I use that are kept on the Nextion are ones for dimming the display if it has not been touched for more than some amount of time. I have no reason to read these variables.

I see you can do something like get t0.txt, but I'm not sure if that would send it over serial.

I've not tried it and my method of reading stuff from the Nextion would need some adaptation to support it. I think you could do it but I have yet to encounter a situation where it would be necessary.

I've been working on it and got some results but it's really going to lead to me overhauling everything and having a really bloated workaround on the arduino just to get some variables transferred over...I know I'm not the best coder, but this whole thing seems so stupid to me at this point. MCUFriend generic chinese TFTs had a great library, I dunno why Nextion has to be such a turd.

Maybe you can help me if I just state a basic sequence that'd solve most of my problems:

  1. User enters string into the tft keyboard, it's saved as a variable on the tft.
  2. That variable string is then sent over serial and placed into a variable on the Arduino.
  3. The corresponding Arduino variable can be sent back to text fields on the tft when particular pages are loaded.

Arduino Variable --> Nextion Variable
Nextion Variable --> Arduino Variable
Nextion Page --> Arduino Variable

That's pretty much all I wanna do here. It seems so simple on paper..

Maybe you can help me if I just state a basic sequence that'd solve most of my problems:

  1. User enters string into the tft keyboard, it's saved as a variable on the tft.
  2. That variable string is then sent over serial and placed into a variable on the Arduino.
  3. The corresponding Arduino variable can be sent back to text fields on the tft when particular pages are loaded.

Arduino Variable --> Nextion Variable
Nextion Variable --> Arduino Variable
Nextion Page --> Arduino Variable

That's pretty much all I wanna do here. It seems so simple on paper..

If you look at my tutorial it achieves that, but not the way you propose. The variables are not stored on the Nextion, they are stored on the Arduino. Each button press is sent to the Arduino which then deals with it and updates the variables. What you are trying to do is easy, they way you are trying to do it is hard.

I can help you if you use my methods, if you want to go your own way that's fine but I won't be able to help much.

Ok I'll go through your process then, thank you - I got thrown off because you said you hadn't used it to read variables, but I took it as you hadn't used it to work with variables :expressionless:

PerryBebbington:
use my methods

So I’ve made good progress and I’m sending page numbers to the Arduino using printh hex, but I’m just wondering how I would go about sending a string, like the contents of a text field back to the Arduino. It would be a unique string or set of numbers that the user types in via a keyboard on the touchscreen - I want to send it to the arduino to put into a variable there.

Would I do it the same way we send it in? printf “string here” ?

#include <SoftwareSerial.h>

SoftwareSerial nexSerial(50, 51);

void setup() {
  Serial.begin(9600);
  nexSerial.begin(9600);
  HMI_startup_message();
}

void HMI_read() {
  static uint8_t HMI_read_data[100]; // I turned this up to a hundred, everything else is the same though
  static uint8_t HMI_read_data_i;
  static uint8_t a5count;
  uint8_t readSerial;
  
  while (nexSerial.available() > 0) {
    readSerial = nexSerial.read();
    if (readSerial == 0xa5) {
      ++a5count;
      if (a5count > 2) {
        a5count = 0;
        HMI_read_data_i = 0;
      }
    }
    else {
      a5count = 0;
    }
    
    HMI_read_data[HMI_read_data_i] = readSerial;
    if (HMI_read_data_i == 5) {

      switch (HMI_read_data[1]) {
        case 0:
            Serial.println("Page 0");
            break;
        case 1:
            Serial.println("Page 1");
            break;
        case 2:
            Serial.println("Page 2");
            break;
        case 3:
            Serial.println("Page 3");
            break;
        case 4:
            Serial.println("Page 4");
            break;
        case 5:
            Serial.println("Page 5");
            break;
        case 6:
            Serial.println("Page 6");
            break;
        case 7:
            Serial.println("Page 7");
            break;
        case 8:
            Serial.println("Page 8");
            break;
        case 9:
            Serial.println("Page 9");
            break;
        case 10:
            Serial.println("Page 10");
            break;
        case 11:
            Serial.println("Page 11");
            break;
      }
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i > 9) {
      HMI_read_data_i = 9;
    }
  }
}

void loop() {
  HMI_read();
  }

I see prints in the nextion docs, it seems to be specifically for sending variables (prints variable,len i think). I am trying to get it to just send a string like prints “string stuff”, X - no go there…I’m not really a proper coder, I never had any education in it (much like William Hung and singing), so I’m probably asking some stupid questions. I just try things and see what works :expressionless:

I’m just wondering how I would go about sending a string, like the contents of a text field back to the Arduino. It would be a unique string or set of numbers that the user types in via a keyboard on the touchscreen - I want to send it to the arduino to put into a variable there.

While I’m sure there must be a way to do it like that, that’s not how my method works. My method is to send individual key presses to the Arduino and have it make sense of them, not enter a string on the Nextion and, at the end send the string, send the entire string. If you have a text field, and I am assuming an alpha-numeric keyboard on the Nextion, then send each key press to the Arduino, let the Arduino assemble the key presses into a string and send the resulting string back to the Nextion for display. In my demonstration you should see that each press of + or - against hours, minutes and seconds is sent to the Arduino, the Arduino updates the values and the result is sent back as a string by:

void HMI_display_clock();

I also included:

void HMI_startup_message();

To demonstrate sending a pure text string to the Nextion, and:

void HMI_display_page(uint8_t page);

Which sends a combined variable and text string. I would think that at least one of these does something close to what you need.

Perry

I'll try something out, I think I can build the variables with single keypresses by appending the values into a complete variable.

I just saw this thread, but the answer is simple. You're using a variable assignment but trying to retrieve the text value using a text method.

NexVariable blocksTotal = NexVariable(0,10,"blocksTotal"); // val

blocksTotal.getText(buffer, sizeof(buffer));

what you need to be using is a NexText assignment:

NexText blocksTotal = NexText(0, 10, "blocksTotal");