how to transfer a string by software serial?

i have a string that is
String a = "water".

i want to transfer it with softwareserial from nodemcu to arduino.

how can i complete this code for that?

#include <SoftwareSerial.h>
SoftwareSerial s(D6, D5);
String a = "water"
void setup ()
{
  Serial.begin(9600);
  s.begin(9600); 
}
void loop ()
{
//???? what should i write here?
s.write(a); // is this correct?
}

Have you tested your posted sketch having done a physical connection between NodeMCU and UNO? If yes, what is the result? Can you observe the string on the Serial Monitor of the UNO? Where is your receiver sketch?

Serial.print(a);

GolamMostafa:
Have you tested your posted sketch having done a physical connection between NodeMCU and UNO? If yes, what is the result? Can you observe the string on the Serial Monitor of the UNO? Where is your receiver sketch?

you mean this sender code is right hah?
but i get this error :

no matching function for call to SoftwareSerial::write(String&)

(deleted)

TheMemberFormerlyKnownAsAWOL:
Serial.print(a);

now we arrive to main problem. i used this s.print(a).
i replace a to "200water". but when i get it in arduino side, it show me = "00water". where is the problem?

my arduino code is here:

#include <SoftwareSerial.h>
String a = "200water"
void setup ()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}
void loop ()
{
 while (!Serial1.available()) {delay (500);}
   if (Serial1.read() > 0)
   {
     while (Serial1.available())
   {
     reading = Serial1.read();
     inchar += reading;
     delay (1);
   }
   Serial.println(inchar); // it prints "00water"!!!!}

spycatcher2k:
See reply #2

can you help me?

Which Arduino?

Search for Robin's "serial input basics - updated" thread to get ideas how to reliably receive data.

sterretje:
Which Arduino?

Search for Robin's "serial input basics - updated" thread to get ideas how to reliably receive data.

this is nodemcu that want to send "200water":

#include <SoftwareSerial.h>
SoftwareSerial s(D6, D5);
String a = "water"
void setup ()
{
 Serial.begin(9600);
 s.begin(9600);
}
void loop ()
{
//???? what should i write here?
s.write(a); // is this correct?
}

this is arduino that want to get "200water" but it get "00water":

#include <SoftwareSerial.h>
String a = "200water"
void setup ()
{
 Serial.begin(9600);
 Serial1.begin(9600);
}
void loop ()
{
while (!Serial1.available()) {delay (500);}
  if (Serial1.read() > 0)
  {
    while (Serial1.available())
  {
    reading = Serial1.read();
    inchar += reading;
    delay (1);
  }
  Serial.println(inchar); // it prints "00water"!!!!
}
if (Serial1.read() > 0)

This code is reading the first byte from the message and throwing it away.

Instead, use Serial.avaliable()

if (Serial1.available() > 0)

The best approach would be to use the more memory efficient char array instead of "S"tring coupled with a robust serial packet protocol. You can use SerialTransfer.h to automatically implement such a protocol for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

cattledog:

if (Serial1.read() > 0)

This code is reading the first byte from the message and throwing it away.

Instead, use Serial.avaliable()

if (Serial1.available() > 0)

so tnx guys. this solved.