Code on the Arduino to Serial Monitor
int LED = 5; //Define the pin for the LED
void setup() {
Serial.begin(9600); //The default baud rate of the Nextion TFT is 9600.
pinMode(LED,OUTPUT); //Define pin as OUTPUT
}
void loop() {
if(Serial.available()>0) //If we receive something...
{
String Received = Serial.readString(); //Save the received String in the Received variable
if(int(Received[0]) == 234) //If the first character of "Received" is "1"
{
digitalWrite(LED,HIGH); //We turn on the LED
}
if(int(Received[0]) == 12) //if is a "0"
{
digitalWrite(LED,LOW); //We turn the LED off
}
}
}
Code on the Arduino to Nextion
const uint8_t bufferSize = 128;
char NextionDataRx[bufferSize];
uint8_t NextionDataIndex = 0;
bool dataReady = false;
bool overflow = false;
uint16_t baudNextion = 9600; // Change this to match whatever board rate you are using for the Nextion.
void setup() {
char fileName[] = __FILE__;
Serial.begin(9600); // ................Had to change this to see an output
delay(2000);
Serial.println("Serial monitor started");
Serial1.begin(baudNextion);
Serial.println("Serial 1 started");
Serial.println(" ");
Serial.println(sizeof(fileName));
Serial.println(fileName);
}
void loop() {
RxNextionData();
PrintNextionData();
}
void RxNextionData() {
char RxTemp;
static uint8_t charCount;
static uint8_t ffCount;
while (Serial1.available() > 0) {
RxTemp = Serial1.read();
if (RxTemp == 0xff) {
++ffCount;
if (ffCount >= 3) {
dataReady = true;
ffCount = 0;
NextionDataRx[NextionDataIndex] = 0;
NextionDataIndex = 0;
}
} else {
NextionDataRx[NextionDataIndex] = RxTemp;
++NextionDataIndex;
if (NextionDataIndex >= bufferSize) {
NextionDataRx[bufferSize - 1] = 0;
dataReady = true;
overflow = true;
}
}
}
}
void PrintNextionData() {
if (dataReady) {
dataReady = false;
if (overflow) {
overflow = false;
Serial.print("Buffer overflow: ");
}
Serial.println(NextionDataRx);
}
}
