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;
}
}