Send and receive data on serial between 2 arduinos

Hi all,

I have one arduino nano and one nodemcu, which are wired for software serial communication.

I am sending from nano a string like “s300,22e”, where:
s = start of the string
300 = value1
22 = value2
e = end of the string

The issue is that when I print the received string in nodemcu I get “300 | 22300” instead of “300 | 22”.

Here are the codes :

Nano:

//SENDER - nano

#include <SoftwareSerial.h>
SoftwareSerial COM(2, 3); //Serial to esp

void setup() {
  COM.begin(9600);
 }

void loop() {
      COM.print("s300,22e");
      delay(50);
}

Nodemcu:

//Receiver - esp

#include <SoftwareSerial.h>
SoftwareSerial COM(5, 4); // Serial to nano

byte incomingByte;
String readBuffer = "";
String DATA_RECEIVED = "";
int value1, value2 ;

void setup() {
  Serial.begin(9600);             // Serial port to computer
  COM.begin(9600);               // Serial port to nano
}

void loop() {
  read_serial();
  delay(50);
}

void read_serial(){
  readBuffer = "";
  boolean start = false;
  while (COM.available()) {
    incomingByte = COM.read();
    delay(5);
    // Reads the data between the start "s" and end marker "e"
    if (start == true) {
      if (incomingByte != 'e') {
        readBuffer += char(incomingByte);    // Add each byte to ReadBuffer string variable
      }
      else {
        start = false;
      }
    }
    // Checks whether the received message statrs with the start marker "s"
    else if ( incomingByte == 's') {
      start = true; // If true start reading the message
    }
  }
  // Converts the string into integer

  if (readBuffer.toInt() != 0) {

  DATA_RECEIVED = String(readBuffer);
  
  String input = "";

   for (int i = 0; i < DATA_RECEIVED.length(); i++) {
    if (DATA_RECEIVED.substring(i, i+1) == ",") {
      value1 = DATA_RECEIVED.substring(0, i).toInt();
      value2 = DATA_RECEIVED.substring(i+1).toInt();
    break;
    }
   }
  }

Serial.print(value1);
Serial.print(" | " );
Serial.println(value2);
}

Can you please help me ?
Thank you.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable - it is what I use for Arduino to Arduino communication.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

Thank you very much for pointing me to that topic.

I have been playing with mu UNO+NdeCPU(ESP8266MOD); but, I cant make your codes working. In my case, the receiver always shows 0s. Can you please, post the wiring connection of your setup. Have you used logic level shifter?

GolamMostafa:
I have been playing with mu UNO+NdeCPU(ESP8266MOD); but, I cant make your codes working.

Who do you mean when you say “your” ?

…R

Robin2:
Who do you mean when you say "your" ?

This kind of imperative post always, I think, is aimed at the OP.

GolamMostafa:
This kind of imperative post always, I think, is aimed at the OP.

In the absence of other evidence I assume that an ambiguous reference is always to the immediately preceding Post.

Much less confusing to mention the number of the Reply that you are referring to - or else to quote piece of the Reply.

…R

Robin2:
In the absence of other evidence I assume that an ambiguous reference is always to the immediately preceding Post.

Much less confusing to mention the number of the Reply that you are referring to - or else to quote piece of the Reply.

Exactly. Very important for us to remember.