Send String from one Arduino to another?

Hello,

I'm currently trying to send a string of data from one Arduino to the other. In the sender Arduino's void loop, I have the code:

Serial.print(average2, 4);
Serial.print(" ");
Serial.print(tempC);
Serial.print(" ");
Serial.print(tempF);
Serial.print(" ");  
Serial.println(average3,1); //note: average2, tempC, tempF, and average3 are all ints.

And in the receiver Arduino's void loop, I have:

if(Serial.available()) {
  String str = Serial.readString();
  Serial.println(str);
}

I've connected TX to RX, RX to TX, and GND to GND like all the Internet tutorials said to; however, the strings do not print to the receiver's serial monitor until I pull out the receiver's RX pin. At that point all the Strings that were sent to the receiver are printed at once. Obviously this isn't really a solution. I've read places that there is a serial buffer, before received serial information is printed/used. Are my Strings getting stuck in that buffer? How can I print the received Strings? Thanks in advance for everyone's help!

  • ApprovedCargo91

Arduino #1 printing to the Serial Monitor doesn't mean that the Arduino #2 will be receiving it.

On my laptop, Arduino #1 is on COM4. Arduino #2 is on COM7.

COM4 is not connected to COM7.

Yes, but the RX of Arduino #1 is connected to the TX of Arduino #2 and vice versa, which should allow communication between the two Arduinos. As I stated in my previous post, the data is being sent - when I pulled out the RX wire, the data printed - but for some reason is not printing with the code I have written.

approvedcargo91:
Yes, but the RX of Arduino #1 is connected to the TX of Arduino #2 and vice versa, which should allow communication between the two Arduinos. As I stated in my previous post, the data is being sent - when I pulled out the RX wire, the data printed - but for some reason is not printing with the code I have written.

Nothing is printed because when you have RX of Arduino #1 connected to RX of Arduino #2, the Serial Monitor is not connected to Arduino #1.

ieee488:
Nothing is printed because when you have RX of Arduino #1 connected to RX of Arduino #2, the Serial Monitor is not connected to Arduino #1.

I connected the TX to the RX, not the RX to the RX. And you can't tell me nothing is printed, because stuff is printed when I pull Receiver Arduino's RX wire.

approvedcargo91:
Yes, but the RX of Arduino #1 is connected to the TX of Arduino #2 and vice versa, which should allow communication between the two Arduinos. As I stated in my previous post, the data is being sent - when I pulled out the RX wire, the data printed - but for some reason is not printing with the code I have written.

they may be talking....

Use Software Serial on the one you are monitoring and print the message to Serial:

#include <SoftwareSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

SoftwareSerial mySerial (rxPin, txPin); 

void setup() 
{
  mySerial.begin(9600);
  Serial.begin(9600);
  Serial.print("Started");
}

void loop() 
{
  if(mySerial.available())
  {
    String incomingMessage = mySerial.readString();
    Serial.println(incomingMessage);
  }
}

You would need to post ALL of your code. That snippet shows that you are sending 4 values separated by spaces and ended with a carriage return and a line feed.

On the receiving end, you do not bother with stopping reading when the carriage return or line feed arrives. Why not?

Also, probably not a good idea to be using the C++ String type. Serial I/O is inherently character-centric. Better to work with ‘char’s and arrays of ‘char’s (aka C strings).

gfvalvo:
Also, probably not a good idea to be using the C++ String type. Serial I/O is inherently character-centric. Better to work with ‘char’s and arrays of ‘char’s (aka C strings).

but for this entry-level test of setting up basic communication, String class is fine

Use Software Serial on the one you are monitoring and print the message to Serial:

So the receiver Arduino has an HC-06 Bluetooth Module wired to it (being used for other purposes), and I'm using SoftwareSerial for that. When I tried to make a second SoftwareSerial for the sender Arduino, the HC-06 didn't perform its actions and the String didn't print. I guess you can't use the library for more than one thing? Is there a similar library I can use for the sender Arduino?

have you tried just toggling the on-board led to verify receipt of a message?

I haven't, how would you go about that? Something like this?

if(mySerial.available()) {
  String incomingMessage = mySerial.readString();
  if(incomingMessage != 0) {
    digitalWrite(13,HIGH);
  }
  else {
    digitalWrite(13,LOW);
  }
}

approvedcargo91:
I haven't, how would you go about that? Something like this?

or even

if(incomingMessage == "expectedMessage")
{
  ledOn();
  delay(250);
  ledOff();
} {

Just tested my code, the LED turns on only after I pull out the RX pin. I commented out all my Bluetooth stuff to make sure it wasn't that, but I get the same result.

but I get the same result.

You STILL are not using readStringUntil().

1 Like

PaulS:
You STILL are not using readStringUntil().

Why would I need to? I don't need the values in the string for any calculation, I just need to be able to read them from the receiver Arduino. No need to split the string up.

approvedcargo91:
Why would I need to? I don't need the values in the string for any calculation, I just need to be able to read them from the receiver Arduino. No need to split the string up.

Well, then, your code, which does that, is working perfectly.

If you want to deal with each packet that arrives, rather than ALL the data, you either need to slow down the sender, so that readString() will eventually time out, OR you need to stop reading when some amount of data has arrived. How much? Well, one packet, of course. That is what readStringUntil() determines and handles.

Of course, it is your program, and you are free to make it work (or not) however you want.

According to my knowledge, the serial port of the Arduino used in the receiver side is busy as data is coming from the transmitter Arduino so it is unable to print the data on serial monitor but as soon it gets free from receiving data it prints all the values it has received which was stored in it memory.
Hope I was of some help.

1 Like