Here is my code, I'm using mega for interacting both python and nextion. I am receiving data from nextion > arduino > python but it nextion does not receive codes from arduino. Wires connected to RX(Nex) > TX1 (Mega), TX(Nex) > RX1(Mega). I also tried this in Proteus and it works
String grainV = "t0.txt";
String lengthV = "t1.txt";
String widthV = "t2.txt";
String percV = "t3.txt";
int value = 30;
String data;
int old = value;
int newPot = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Nextion
Serial1.begin(9600); // Python
send(grainV, "10");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
// Sending data from python to nextion
data = Serial.readString();
split(data);
}
if(Serial1.available()>0){
// Sending data from nextion to python
data = Serial1.readString();
Serial.println(data);
}
delay(20);
}
void send(String comp, String val){
Serial1.print(comp);
Serial1.print("=");
Serial1.print('"');
Serial1.print(val);
Serial1.print('"');
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
}
void split(String str){
//
int i1 = str.indexOf('-');
int i2 = str.indexOf('-', i1+1);
int i3 = str.indexOf('-', i2+1);
String grain = str.substring(0, i1);
String length = str.substring(i1+1, i2);
String width = str.substring(i2+1, i3);
String perc = str.substring(i3+1, str.length());
//grain = grain.toInt();
//length = length.toInt();
//width = width.toInt();
//perc = perc.toInt();
send(grainV, grain);
delay(1000);
send(lengthV, length);
delay(1000);
send(widthV, width);
delay(1000);
send(percV, perc);
//Serial.println();
}