How to have Nextion display send keypad value + button to Arduino Mega using EasyNextionLibrary

Hello people, I am working on a project that have a Nextion Display and a TDS sensor.
I am using the EasyNextionLibrary for the Nextion display and the GravityTDS library for the TDS sensor.
The Library for the TDS sensor allow it to be calibrated via serial using these commands:

  1. enter -> enter the calibration mode
    cal:tds value -> calibrate with the known tds value(25^c). e.g.cal:707
    exit -> save the parameters and exit the calibration mode.

So, I can calibrate the TDS sensor using those commands when the Arduino is connected to my PC, however, I'd like to be able to do it via the Nextion display.

This is the page I have created for the TDS calibration on the Nextion display:

image

When I click on the t71 text field, a numeric keypad open when I can enter a numerical value.
How to I setup the b71 "SET" button so that it send the commands above to simulate like I would do it via a PC?

Basically, it write "enter" in the Arduino serial (this put the library in calibration mode), then write cal:(value entered from the Nextion t71 text field), then write "exit" to save and exit calibration.

I am using the "trigger" feature of the EasyNextionLibrary to recognize each buttons press and release, so the commands should reside on the Arduino side and not on the Nextion side, if possible.

The touch Release Event for the "SET" button is printh 23 02 54 17 on the Nextion side.

On the Arduino Sketch, I now have:

 // TDS Calibration PPM Page 9
  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");

What do I have to add to this code so it does what's is mentioned above?

Any help would be greatly appreciated..

Cheers.

How to I setup the b71 "SET" button so that it send the commands above to simulate like I would do it via a PC?

I don't think you can do that directly from the serial output of the Nextion, and you are on the right track with putting the commands in the execution of the Trigger function. Reading the text field from the Nextion is the easy part.

I think your main issue is that the GravityTDS library is written for Serial input which is from the USB port.

boolean cmdSerialDataAvailable();

Are you connected to the USB port during normal operation in order to read or write anything over Serial? If there is no monitor normally connected, you might be able to use a ttl/usb converter to loop from one of the Mega hardware serial ports back into the usb port and the library will respond as normal. Then the trigger function would write the standard commands over the port which loops back to usb.

If the usb port is in use, then I think you might be able modify the library so that the input functions can read from one of the other serial ports instead of Serial and the trigger function would write to that Serial instance.

boolean cmdSerial3DataAvailable();

However, I think it may be better to replicate the library functions in the trigger function. You will need to study the library quite carefully to figure out what to do. I think this is the best way to go and that it leaves the library alone. I think these are the key library functions.

cmdParse()  //sets a mode index value
ecCalibration(byte mode)

It should be easy for the trigger function so set a mode index and call the ecCalibration function with the cal mode 2 (or 3 for eeprom storage). I think the ecCalibration function would need to be modified to take the Nextion data instead of the serial buffer value following "CAL:".

void GravityTDS::ecCalibration(byte mode)
{
    //char *cmdReceivedBufferPtr;
    static boolean ecCalibrationFinish = 0;
    static boolean enterCalibrationFlag = 0;
    float KValueTemp,rawECsolution;
    switch(mode)
    {
      case 0:
     if(enterCalibrationFlag)
        Serial.println(F("Command Error"));
     break;
      
      case 1:
      enterCalibrationFlag = 1;
      ecCalibrationFinish = 0;
      Serial.println();
      Serial.println(F(">>>Enter Calibration Mode<<<"));
      Serial.println(F(">>>Please put the probe into the standard buffer solution<<<"));
      Serial.println();
      break;
     
      case 2: //modify this to take Nextion value read by Mega
      //cmdReceivedBufferPtr=strstr(cmdReceivedBuffer, "CAL:");
      //cmdReceivedBufferPtr+=strlen("CAL:");
     // rawECsolution = strtod(cmdReceivedBufferPtr,NULL)/(float)(TdsFactor);      
    //You will need to take the rawECsolution value from reading a Nextion value divided by the TdsFactor
      rawECsolution = rawECsolution*(1.0+0.02*(temperature-25.0));
      if(enterCalibrationFlag)
      {
         // Serial.print("rawECsolution:");
         // Serial.print(rawECsolution);
         // Serial.print("  ecvalue:");
         // Serial.println(ecValue);
          KValueTemp = rawECsolution/(133.42*voltage*voltage*voltage - 255.86*voltage*voltage + 857.39*voltage);  //calibrate in the  buffer solution, such as 707ppm(1413us/cm)@25^c
          if((rawECsolution>0) && (rawECsolution<2000) && (KValueTemp>0.25) && (KValueTemp<4.0))
          {
              Serial.println();
              Serial.print(F(">>>Confrim Successful,K:"));
              Serial.print(KValueTemp);
              Serial.println(F(", Send EXIT to Save and Exit<<<"));
              kValue =  KValueTemp;
              ecCalibrationFinish = 1;
          }
          else{
            Serial.println();
            Serial.println(F(">>>Confirm Failed,Try Again<<<"));
            Serial.println();
            ecCalibrationFinish = 0;
          }        
      }
      break;

        case 3:
        if(enterCalibrationFlag)
        {
            Serial.println();
            if(ecCalibrationFinish)
            {
               EEPROM_write(kValueAddress, kValue);
               Serial.print(F(">>>Calibration Successful,K Value Saved"));
            }
            else Serial.print(F(">>>Calibration Failed"));       
            Serial.println(F(",Exit Calibration Mode<<<"));
            Serial.println();
            ecCalibrationFinish = 0;
            enterCalibrationFlag = 0;
        }
        break;
    }
}

Hi cattledog, thanks for your response.

Arduino is new to me and I am not a coder, yet ;o).

The Nextion is using Serial1 (D18, D19) on the Mega 2560 R3 board.
(Serial2 is the only one available now as all the other serials are being use by other devices).

If I modify the GravityTDS.h file of the library to use

boolean cmdSerial1DataAvailable();

instead of the default

boolean cmdSerialDataAvailable();

what else would I need to do on the Arduino sketch trigger command and/or the Nextion display for this to work ?

OR

What do I need to modify on the GravityTDS.cpp library file for the other option you mentioned?

This is probably easy do to for someone proficient in Arduino, but I am a newbie at this...

Any help on this would be very appreciated...
Cheers

That's good news. For a test, I would ignore the Nextion and the trigger function for now, and just modify the library to work with Serial2.

boolean cmdSerial2DataAvailable();

Arduino is new to me and I am not a coder, yet ;o).

Your heading down a difficult path trying to modify a library for your particular use( or to copy its functions for use elsewhere), and you should expect slow going. The more you can work with simplified test environments will make things easier.

Do you have a usb/ttl converter? If so, you can connect from your computer to Serial2 of the Mega, and see if you can calibrate the probe using a terminal program to send the commands over Serial2 to the modified library which will check for input on Serial2 and not Serial.

Then, if the library is working with input from Serial2 see if you can write the calibration commands from Serial1 to Serial2 with jumper wires cross connected tx>rx and rx>tx.

Good Luck.

I do have a usb/ttl converter and will give it a try...

Cheers