Nextion displey - problem with reading data from display

Hello, I need a help with reading the data from the display, getting data is done using the getText function - i pass the name of the object as a parameter( I checked the object name defined in the nextion editor). Each time the getText function is called, a command is written to the console instead of to display, command: get inputFFill.txt���. There may be a problem with the transmission itself over the serial link, which I use for debbuging and for sending commands at the same time. Onclick, onrelease event handlers work. Thanks for the advice, sorry for the spaghetti code:

metruinoOffline.cpp (10.2 KB)

Hello chossecko,
Welcome

Support for Nextion on these fora is pretty much as follows:
You can follow the methods I set out in using Nextion displays with Arduino. My methods do not use the Nextion libraries, so while I am happy to offer help on using my methods I cannot offer anything very helpful for any Nextion library.
The original Nextion libraries are full of bugs. There is a link from my tutorial to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.
There's also a separate Easy Nextion Library by Seithan, his methods are different to mine, choose which works best for you.
Beyond that the odd person occasionally offers a bit of help but not much.

I've not looked at your code because to do so means downloading the file, putting it into a suitable folder and then opening it with the IDE, in any case, by your own admission it is 'spaghetti code'. Please create a simple program that illustrates the problem and is not 'spaghetti' and post that here in code tags in line with the forum instructions. You can read the forum instructions at:
General guidance and
How to use this forum
Especially item #7 on posting code.

You are more likely to get help if you make it easy for us to help you.

Thank you.

PerryBebbington, thank you for the quick reply, as I mentioned i have a problem with getting,setting data from,to the display specifically with the functions getText, setText, every time I call them - command that should be sent to the display is sent to the console - command: get inputFFill.txt��� . touchReturn function works. I’m trying to build my own library, because the library from the manufacturer Nextion is unusable and also thank you for the tip on Easy Nextion Library, i’ll try it

#include <SPI.h>
//WIFI
#include <ESP8266WiFi.h>

//Server
#include <ESPAsyncWebServer.h>
#include <WiFiClient.h>

//Display
#define nextion Serial
// #define Serial Serial1


//TIME LIB
#include <millisDelay.h>
millisDelay firstFillDelayObject;


//Wifi ap-ssid, pw
const char *ssid = "ESP-AP";
const char *password = "123456789";



//eletricValve
boolean stateEletricValve = LOW;
#define outputPinEletricValve 14
boolean statePumpFilter = 0;
boolean stateHeating = 0;

AsyncWebServer server(80);

String nextionReceive(boolean read_data);


// DISPLAY functions
void nextionInit(int speedInit)
{
  nextion.begin(speedInit);
}

//------------------------------------------------------------------
void send_Command(const char *cmd)
{ // send data to display
  nextion.print(cmd);
  nextion.write(0xFF);
  nextion.write(0xFF);
  nextion.write(0xFF);
}

void sendText(String componentID, String text)
{ 
  String txt = componentID + ".txt=\"" + text + "\"";
  send_Command(txt.c_str());
}

void initFirstFillDelay(uint32_t startDelay)
{
  //firstFill values
  uint32_t firstFillDelayMs = startDelay * 60 * 1000;
  delay(100);
  firstFillDelayObject.start(firstFillDelayMs);
  if (!firstFillDelayObject.isRunning())
  {
    Serial.println("timer has not been set");
  }
  else
  {
    Serial.println("timer has been set succesfully");
    Serial.println(firstFillDelayMs);
    digitalWrite(outputPinEletricValve, HIGH);
  }
}

void stopFirstFill()
{
  char buffer[10] = {0};
  byte defaultState = 0;
  if (firstFillDelayObject.isRunning())
  {
    firstFillDelayObject.finish();
    digitalWrite(outputPinEletricValve, LOW);
    Serial.println("button stop was pressed");
    memset(buffer, 0, sizeof(buffer));
    itoa(defaultState, buffer, 10);
    sendText("fieldTimeout", "0");
  }
  else
  {
    Serial.println("timer didnt started yet");
  }
}

long convertNextTextToInt(String text)
{
  uint32_t number = text.toInt();
  return number;
}

// void sendText(String componentID, String text) { 
//   String txt = componentID + ".txt=\"" + text + "\"";
//   send_Command(txt.c_str());
// }

void pageReturn(String page_ID)
{
  if (page_ID == "15")
  {
  }
}

String getText(String componentID)
{ 
  // String txt = "get " + componentID + ".txt";
  String txt = "get inputFFill.txt";
  send_Command(txt.c_str());
  txt = nextionReceive(false);
  txt = txt.substring(1, txt.length() - 3);
  Serial.println("function getText");
  Serial.println(txt);
  return txt;
}



void touchReturn(String page_ID, String component_ID, String touch_event)
{
  if (page_ID == "5") //firstFill
  {
    if (component_ID == "3")
    {
      // digitalWrite(outputPinEletricValve, HIGH);
      // String test = getText("inputFFill");
      // Serial.println(test);
      unsigned long delay = convertNextTextToInt(getText("inputFFill"));
      Serial.println(delay);
      if (delay > 0)
        initFirstFillDelay(delay);
    }
    else if (component_ID == "4")
    {
      // digitalWrite(outputPinEletricValve, LOW);
      stopFirstFill();
    }
  }
  else if (page_ID == "15")
  { //Temperature
    if (component_ID == "3")
    {
      // sendText("inputActualTmp", String(temperature));
      // sendText("3", String(temperature));
    }
  }
}

String nextionReceive(boolean read_data)
{ //returns generic

  boolean answer = false; // sign
  char bite;              //var for save sign
  String cmd;             //var for save text
  byte countEnd = 0;      // counter
  unsigned long previous; // time start
  int timeout = 1000;     // time to wait for incoming data
  previous = millis();

  do
  { // waiting for correct answer
    if (nextion.available())
    { 
      bite = nextion.read();
      cmd += bite;
      if ((byte)bite == 0xff)
        countEnd++;
      if (countEnd == 3)
        answer = true;
    }
  } while (!answer && !((unsigned long)(millis() - previous) >= timeout)); //waiting for corrent answer or elapsed time

  if (read_data)
  { // read general data
    if (cmd[0] == 0x65)
    { // Touch event return data
      // 0X65 + Page ID + Component ID + TouchEvent + End
      touchReturn(String(cmd[1], DEC), String(cmd[2], DEC), String(cmd[3], DEC));
    }
    else if (cmd[0] == 0x66)
    { // Current page ID number returns
      // 0X66 + Page ID + End
      pageReturn(String(cmd[1], DEC));
    }
    else if (cmd[0] == 0x67)
    { // Touch coordinate data returns
      // 0X67++ Coordinate X High-order+Coordinate X Low-order+Coordinate Y High-order+Coordinate Y Low-order+TouchEvent State+End
    }
    else if (cmd[0] == 0x68)
    { // Touch Event in sleep mode
      // 0X68++Coordinate X High-order+Coordinate X Low-order+Coordinate Y High-order+Coordinate Y Low-order+TouchEvent State+End
    }
  } 
  else
  { //read get data
        Serial.println("reading valuesss");
    if (cmd[0] == 0x70)
    { // String variable data returns
      // X70+Variable Content in ASCII code+End
      // touchReturn(String(cmd[1], DEC), String(cmd[2], DEC), String(cmd[3], DEC));
      // getText(String(cmd[1], DEC));
      Serial.println("reading valuesss 0x70");
      return cmd;
    }
    else if (cmd[0] == 0x71)
    { // Numeric variable data returns
      // 0X71+variable binary data(4 bytes little endian mode, low in front)+End
      return cmd;
    }
  }
}



void checkDisplay()
{                              
  if (nextion.available() > 0) // checking the memmory
  {
    nextionReceive(true); //read values from serial port
  }
}

//------------------------------------------------------------------
void visible(String componentID, boolean visible)
{ // skryti/zobrazeni komponenty
  String vis = "vis " + componentID + "," + String(visible);
  send_Command(vis.c_str());
}




void setup()
{

  //Display
  nextionInit(9600);

  //Serial
  Serial.begin(9600);


  // Server
  WiFi.mode(WIFI_AP);
  //AP - mode
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print(F("Soft-AP IP address = "));
  Serial.println(myIP);
  Serial.println("WiFi connected");
  Serial.println("IP adress client: ");
  Serial.println(WiFi.localIP());
  Serial.println("Status: ");
  Serial.print(WiFi.status());
  server.begin();
}



void loop()
{
  //Display
  checkDisplay();

}

Hello chossecko,
I don't know the answer to your specific question, sorry. Some things I noticed that might be useful:

If you are trying to communicate with the Nextion and build a project around it I suggest you forget the main project and concentrate on the Nextion communications. Only when you are satisfied with the communications to the Nextion should you return to working on the main project, this avoids confusion arising from a problem being possibly with the Nextion code or with the main project.

String nextionReceive(boolean read_data);

Don't use Strings, they fragment the heap and cause memory problems. For more information search this site as it is a frequently discussed subject. Use char arrays, also known as C strings, for example:

char someData[20];
void send_Command(const char *cmd)

I can't see that working as you have defined a constant, if it does then someone needs to teach me something new.

Don't use delay, delay means nothing else is happening or can happen, and is my pet hate on here. See
Using millis for timing
Demonstration for several things at the same time

Your approach is so different to mine I cannot make any comment as to whether it's likely to work or lead to trouble, sorry.

Good luck.