ESP32 to Nextion just won't work

Have a Nextion 3.5" with touch, making a thermostat. I can get messages from the Nextion to the ESP32 just fine. Using Serial2 on the ESP32, and it read and interprets the commands coming into the ESP32 with no problem, so my serial connect is OK.

When I try sending a simple number back into a text box - no luck. It can’t be this hard. :slight_smile: I’ve looked at a lot of posts and I think my format is correct. Using " in my strings, sending three 0xff at the end. Here’s a test program I wrote. No luck. Thanks for any suggestions.

Theron Wierenga

// Nextion_write 
// My test copy copy
// theron@wierengaconsulting.com

String sendThis;

void setup() {
 
  Serial.begin(115200); //open the hardware serial port
  Serial2.begin(9600);
}

void loop() {
  
  String sendThis = ""; //Declare and initialise the string we will send
  delay(1000); //Probably unneccessary, but I give the screen some time to respond
  sendThis = "t1.txt=\"Test\""; //Build the part of the string that we know
  writeString(sendThis); /*Use a function to write the message character by character to the Nextion because
  mySerial.write(sendThis) gives you an error due to a datatype mismatch*/
}

//NOTE: A great big thanks to: RamjetX for writing this function. You can find his/her post here: http://forum.arduino.cc/index.php?topic=89143.0. Please go give him/her some Karma!
void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    Serial2.print(stringData[i]);   // Push each char 1 by 1 on each loop pass  
    Serial.print(stringData[i]);
  }
  Serial2.print(0xff); //We need to write the 3 ending bits to the Nextion as well
  Serial2.print(0xff); //it will tell the Nextion that this is the end of what we want to send.
  Serial2.print(0xff);
  Serial.println(0xff);
}// end writeString function

Hello Theron,

The most obvious problem with your code is this:

Serial2.print(0xff); //We need to write the 3 ending bits to the Nextion as well

That doesn't do what you think it does, it doesn't send 0xff, it sends as text 255. You need to use:

Serial2.write(0xff);

There are plenty of warnings on the forums about using

String

(Capital S). I never use it so I don't know first hand what problems it causes, I just know it does.

The rest of your code I am struggling to follow. Have you read my tutorial 'using Nextion displays with Arduino'? The code in there definitely does work.

Perry,

Thank you, thank you. Serial2.write did the trick. At my age you do a lot of dumb things. The example I used had Serial2.write in it and I changed them to Serial2.print!

Regards, Theron

At my age you do a lot of dumb things.

So do I! Don't worry about it, everyone on here has made and will continue to make daft mistakes.

One more thing, in the code you posted originally you had

Serial.println(0xff);

I've no idea why you have that, you don't need 4 lots of 0xff and you don't need a line feed after them.