Hello all,
I have a project currently that i am sending and receiving data between a nextion display and an arduino i'm not using the library as i don't like it and find it messy so am communicating over serial.
Firstly the display communcates great with the arduino all button pushes work expectidly and the arduino responds fine the issue comes up when i try and send data to the nextion.
So i have some air pressures being sent to the arduino through soft serial and updating the nextion numbers and that all works fine but the issue is when this is happening i am getting serial back to the arduino that shouldn't be there as in no nextion buttons have been pushed etc so its only when the arduino sends serial to the nextion display that this occurs and then when a button is pushed the data takes a twist and goes random characters unexpectedly and no longer functions as intended.
Here is my code currently as you can see have commented out some of the bottom serial from arduino to nextion
#include <SoftwareSerial.h>
SoftwareSerial NextSerial(12,11); //RX,TX
//Button States from Nextion
int TYRBTNST=0;
int COMBTNST=0;
String endChar = String(char(0xfff)) + String(char(0xfff)) + String(char(0xfff));
//Pressure Sensor Pins
int SNSTank=A0;
int SNSTyreFront=A1;
int SNSTyreRear=A2;
int SNSBagL=A3;
int SNSBagR=A4;
// Outputs
int BagLUp=2;
int BagRUp=3;
int TyreFUp=4;
int TyreRUp=5;
int BagLDwn=6;
int BagRDwn=7;
int TyreFDwn=8;
int TyreRDwn=9;
int CompTrigger=10;
// Front and rear target pressures
int TargetFrontPress=0;
int TargetRearPress=0;
// Line and Bag Pressures
int LineFrontPress=0;
int LineRearPress=0;
int BagLPress=0;
int BagRPress=0;
int TankPress=0;
void setup() {
// Begin Serial
Serial.begin(9600);
delay(1000);
NextSerial.begin(9600);
//Set pins as outputs
pinMode(BagLUp, OUTPUT);
pinMode(BagRUp, OUTPUT);
pinMode(TyreFUp, OUTPUT);
pinMode(TyreRUp, OUTPUT);
pinMode(BagLDwn, OUTPUT);
pinMode(BagRDwn, OUTPUT);
pinMode(TyreFDwn, OUTPUT);
pinMode(TyreRDwn, OUTPUT);
pinMode(CompTrigger, OUTPUT);
// Write all output pins High
digitalWrite(BagLUp, 1);
digitalWrite(BagRUp, 1);
digitalWrite(TyreFUp, 1);
digitalWrite(TyreRUp, 1);
digitalWrite(BagLDwn, 1);
digitalWrite(BagRDwn, 1);
digitalWrite(TyreFDwn, 1);
digitalWrite(TyreRDwn, 1);
digitalWrite(CompTrigger, 0);
// Startup
Serial.println("Startup");
delay(500);
}
void loop() {
// Serial Read
if(NextSerial.available()){
String dfd="";
delay(100);
while(NextSerial.available()){
dfd+= char(NextSerial.read());
}
// Delete Unwanted Serial
//if(dfd.length()<5) dfd="";
//if(dfd.length()>10) dfd="";
Serial.println(dfd);
//Tyre Page
// Tyre Button Enable and Disable
if(dfd.substring(0,8) == "TYRBTNST"){
TYRBTNST = (dfd.substring(8,9).toInt());
Serial.print("Tyre State ");
Serial.println(dfd.substring(8,9).toInt());
} // End Tyre Button Enable & Disable
//Home Page
// Compressor Button Enable and Disable
if(dfd.substring(0,8) == "COMBTNST"){
// Normally Turns LED ON But not while using serial2 digitalWrite(LED_BUILTIN,dfd.substring(8,9).toInt());
COMBTNST = (dfd.substring(8,9).toInt());
Serial.print("Compressor State ");
Serial.println(dfd.substring(8,9).toInt());
digitalWrite(CompTrigger,dfd.substring(8,9).toInt());
} // End Compressor Enable & Disable
//Bag Control Page
// Left Bag Arrows
// Left Bag Up
if(dfd.substring(0,6) == "BagLUp"){
digitalWrite(BagLUp,dfd.substring(6,7).toInt());
Serial.print("LeftBagUp ");
Serial.println(dfd.substring(6,7).toInt());
} // End Right Up
// Left Bag Down
if(dfd.substring(0,7) == "BagLDwn"){
digitalWrite(BagLDwn,dfd.substring(7,8).toInt());
Serial.print("LeftBagDown ");
Serial.println(dfd.substring(7,8).toInt());
} //End Right Down
// Right Bag Arrows
// Right Bag Up
if(dfd.substring(0,6) == "BagRUp"){
digitalWrite(BagRUp,dfd.substring(6,7).toInt());
Serial.print("RightBagUp ");
Serial.println(dfd.substring(6,7).toInt());
} // End Right Up
// Right Bag Down
if(dfd.substring(0,7) == "BagRDwn"){
digitalWrite(BagRDwn,dfd.substring(7,8).toInt());
Serial.print("RightBagDown ");
Serial.println(dfd.substring(7,8).toInt());
} //End Right Down
// Both Bags
// Both Up
if(dfd.substring(0,6) == "BagsUp"){
digitalWrite(BagLUp,dfd.substring(6,7).toInt());
digitalWrite(BagRUp,dfd.substring(6,7).toInt());
Serial.print("BothBagsUp ");
Serial.println(dfd.substring(6,7).toInt());
} // End bags up
// Both Down
if(dfd.substring(0,7) == "BagsDwn"){
digitalWrite(BagLDwn,dfd.substring(7,8).toInt());
digitalWrite(BagRDwn,dfd.substring(7,8).toInt());
Serial.print("BothBagsDwn ");
Serial.println(dfd.substring(7,8).toInt());
} // End bags down
}// end serial read
//Read Sensor values
// Tank Pressure
TankPress = map(analogRead(SNSTank), 101, 922, 0, 200);
//Tyre Pressures
LineFrontPress = map(analogRead(SNSTyreFront), 101, 922, 0, 200);
LineRearPress = map(analogRead(SNSTyreRear), 101, 922, 0, 200);
// Bag Pressures
BagLPress = map(analogRead(SNSBagL), 101, 922, 0, 200);
BagRPress = map(analogRead(SNSBagR), 101, 922, 0, 200);
// Send Tank Info
//NextSerial.println(String("HomePage.TankPress.val=") + String(TankPress));
//NextSerial.print(endChar);
// Send Bag Info Home Page
NextSerial.print(String("HomePage.LeftBagPress.val=") + String(BagLPress));
NextSerial.write(0xff);
NextSerial.write(0xff);
NextSerial.write(0xff);
// NextSerial.print(String("HomePage.RightBagPress.val=") + String(BagRPress));
// NextSerial.write(0xff);
// NextSerial.write(0xff);
// NextSerial.write(0xff);
// Send Bag Info Airbag COntrol Page
// NextSerial.print(String("AirbagControl.LeftBagPress.val=") + String(BagLPress));
// NextSerial.write(0xff);
// NextSerial.write(0xff);
//NextSerial.write(0xff);
//NextSerial.print(String("AirbagControl.RightBagPress.val=") + String(BagRPress));
//NextSerial.write(0xff);
//NextSerial.write(0xff);
//NextSerial.write(0xff);
Here you can see the 2 different serial monitor outputs.
The first is when the arduino is sending the updated value to nextion
`// Send Bag Info Home Page
NextSerial.print(String("HomePage.LeftBagPress.val=") + String(BagLPress));
NextSerial.write(0xff);
NextSerial.write(0xff);
NextSerial.write(0xff);`
The second is with this commented out and a single button push both time to show the difference in serial data
// Send Bag Info Home Page
//NextSerial.print(String("HomePage.LeftBagPress.val=") + String(BagLPress));
//NextSerial.write(0xff);
//NextSerial.write(0xff);
//NextSerial.write(0xff);