Hi guys,
I need your help. I try to do serial communication between an arduini Mega and an ESP8266 (or in my case a NodeMCU)
All in all the code works but I some lines in the serial are missing always.
for example I try to send:
<toESP|pH_7.02|*014>
<toESP|tE_25.63|*015>
<toESP|tV_0|*011>
<toESP|cI_0|*011>
<toESP|mO_0|*011>
<toESP|mM_0|*011>
<toESP|cP_224.00|*016>
<toESP|cR_0.00|*014>
<toESP|cG_0.00|*014>
<toESP|cB_0.00|*014>
<toESP|p1_0|*011>
<toESP|p2_0|*011>
<toESP|lV_0|*011>
<toESP|l1_0|*011>
<toESP|l2_0|*011>
<toESP|cO_1|*011>
<toESP|hV_0|*011>
<toESP|d1_1|*011>
<toESP|d2_1|*011>
<toESP|d3_1|*011>
<toESP|cV_0|*011>
<toESP|nO_1458672841|*020>
but I receive
toESP|pH_7.02|*014
toESP|tV_0|*011*015
toESP|tV_0|*011*015
cI_0|*011
toESP|mO_0|*011
toESP|mM_0|*011
toESP|cP_224.00|*016
toESP|cR_0.00|*014
toESP|cB_0.00|*014
oESP|p1_0|*011
toESP|p2_0|*011
toESP|lV_0|*011
toESP|l1_0|*011
toESP|l2_0|*011
toESP|cO_1|*011
toESP|hV_0|*011
toESP|d1_1|*011
toESP|d2_1|*011
toESP|d3_1|*011
toESP|cV_0|*011
toESP|nO_1458672953|*020
The wierd thing is, that most of the time the second line is missing.
my code to send
void sendSerial(String sendCom)
{String sendThis = "<"+ sendCom + "|" ;
int CheckSum = sendThis.length()-1;
sendThis+='*';
if(CheckSum<10) {sendThis+="00";}
else if (CheckSum<100) {sendThis+=0;}
sendThis+=CheckSum;
sendThis+=">";
char charBuf[200];
sendThis.toCharArray(charBuf, 200);
Serial2.write(charBuf);
# if debug
{Serial.print(F("sendSerial DebugLine: "));
Serial.print(charBuf);
}
#endif
}
my code to receive
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available()) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx=0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void useNewData() {
if (newData == true) {
StringFromSerial=receivedChars;
webSocket.broadcastTXT("dbg:"+String(StringFromSerial));
parseCommand(StringFromSerial);
// sendSerial("dbgES: " + StringFromSerial);
StringFromSerial="";
ndx = 0;
newData = false;
}
}
as you already know - in the loop the useNewData() and recvWithStartEndMarkers() is constantly called
Any ideas?