SoftwareSerial receiving wrong characters

Hi, I need to communicate 3 slaves (Bluno Nano) with a master (Arduino Nano). I tried with I2C but somewhy it’s fails (I can’t send either request messages), so I tried with Serial communication.

The problem is that I can’t read from master the correct character. I send just “-050” but the Master arduino just receive random characters. If I remove lines or delays, I can get the “-050”, but the problem is that only works with one arduino.
If I add the same code for the second arduino, I will get randoms characters again.

This is my Slave sketch:

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

void loop() {
  Serial.println("-050");
  delay(1000);
}

This is my working loop function on Master:

void loop() {
  Slave1.flush();
  Slave1.listen();
  char buffer1[4] = "";
  int pos1 = 0;
  while(Slave1.available() && pos1 < 4) {
    buffer1[pos1] = Slave1.read();
    Serial.print(buffer1[pos1]);
    pos1++;
  }
  StrToInt(buffer1, &RSSI1);
  Serial.println(RSSI1);
}

This is my unworking loop function on Master:

void loop() {
  Slave1.flush();
  Slave1.listen();
  char buffer1[4] = "";
  int pos1 = 0;
  while(Slave1.available() && pos1 < 4) {
    buffer1[pos1] = Slave1.read();
    Serial.print(buffer1[pos1]);
    pos1++;
  }
  StrToInt(buffer1, &RSSI1);
  Serial.println(RSSI1);

  Slave2.flush();
  Slave2.listen();
  char buffer2[4] = "";
  int pos2 = 0;
  while(Slave2.available() && pos2 < 4) {
    buffer2[pos2] = Slave2.read();
    Serial.print(buffer2[pos2]);
    pos2++;
  }
  StrToInt(buffer2, &RSSI2);
  Serial.println(RSSI2);
}

And this is my actual complete sketch for Master:

#include <SoftwareSerial.h>

SoftwareSerial Slave1(11, 12);
SoftwareSerial Slave2(9, 10);

void setup() {
  Serial.begin(9600);
  Slave1.begin(9600);
  Slave2.begin(9600);
}

int RSSI1 = 0;
int RSSI2 = 0;

void loop() {
  Slave1.flush();
  Slave1.listen();
  char buffer1[4] = "";
  int pos1 = 0;
  while(Slave1.available() && pos1 < 4) {
    buffer1[pos1] = Slave1.read();
    Serial.print(buffer1[pos1]);
    pos1++;
  }
  StrToInt(buffer1, &RSSI1);
  Serial.println(RSSI1);

  Slave2.flush();
  Slave2.listen();
  char buffer2[4] = "";
  int pos2 = 0;
  while(Slave2.available() && pos2 < 4) {
    buffer2[pos2] = Slave2.read();
    Serial.print(buffer2[pos2]);
    pos2++;
  }
  StrToInt(buffer2, &RSSI2);
  Serial.println(RSSI2);
}

void StrToInt(String input, int* output) {
  int tmp = 0;
  bool isNeg = false;
  for(int i = 0; i < input.length(); i++) {
    if(input [i] == '-') {
      isNeg = true;
    } else {
      int num = input[i] - '0';
      if(num >= 0 && num <= 10) {
        if(tmp == 0) {
          tmp = num;
        } else {
          tmp = (tmp * 10) + num;
        }
      }
    }
  }

  if(isNeg)
    tmp = tmp * -1;
  if(tmp != 0)
    *output = tmp;
}

The output from serial could be either random characters (either or not english/spanish characters) or just nothing.

Note: I did a research, but I think that I used the wrongs tags on google, because I didn’t find any information about this. Also, I tried with baud rate of 300 and 115200, both with the same result.

Note2: I will be very grateful if you can help me with fixing the SW or I2C (Bluno Nano is an Arduino Uno, and has its SDA and SCL in A4 and A5. The Arduino nano has the same pins in the same order)

It is not really practical to use more than one instance of SoftwareSerial. Only one of them can listen at any one time but for that to work the sender would need to wait until the master is listening to it. For what you are doing you should use a Mega which has 3 spare HardwareSerial ports.

I have not used I2C myself but I understand that it can work with multiple devices as long as the distance is short. How far apart are your Arduinos?

...R

Robin2:
It is not really practical to use more than one instance of SoftwareSerial. Only one of them can listen at any one time but for that to work the sender would need to wait until the master is listening to it. For what you are doing you should use a Mega which has 3 spare HardwareSerial ports.

I have not used I2C myself but I understand that it can work with multiple devices as long as the distance is short. How far apart are your Arduinos?

...R

Thanks for your reply.
I know that Software serial is not the best option, but for now it's one of my possibilities.
The arduino mega was another option, but the problem is that it is 10CM long, but I have only 12CM of usefull space in my project, and I have a protoboard already.

The Slaves and the master are just right behind of each other, and all are connected by 8 (4 SDA + 4 SCL) medium wide F-M jumpers (no more of 7in each). But when I use I2C, I doesn't do anything (either wrong characters nor TX/RX/L blink), and Right now I'm using a Raspberry Pi 3B and an Arduino Uno with I2C connection for another project and it's works fine (and those are connected with a 20In jumper). That is so weird that two "Nanos" cannot communicates each other

It may be possible to use AltSoftSerial and NeoSWSerial to get two extra serial ports. They are both available from the Arduino IDE Library Manager, under the menu Sketch → Include Library → Manage Libraries.

There are some limitations:

  • NeoSWSerial only supports 9600, 19200 and 38400. Stick with 9600 for the most reliability.

  • Transmitting on the NeoSWSerial port prevents AltSoftSerial from receiving.

  • AltSoftSerial only works on two specific pins (8 & 9 on an UNO)

It would be much more efficient and reliable to use Serial for one of the slaves.

If the messages you are sending between the master and slave has some kind of message delimiters (framing), you can still send debug messages to the Serial Monitor window. The slave should ignore it if there is no “start” character in your debug prints.

You would also see the messages sent to the slave in the Serial Monitor window. Be aware that you have to disconnect pin 0 to upload a new switch over USB. Some people put a switch in that wire so it’s easy to upload.

-dev:
It may be possible to use AltSoftSerial and NeoSWSerial to get two extra serial ports. They are both available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

There are some limitations:

  • NeoSWSerial only supports 9600, 19200 and 38400. Stick with 9600 for the most reliability.

  • Transmitting on the NeoSWSerial port prevents AltSoftSerial from receiving.

  • AltSoftSerial only works on two specific pins (8 & 9 on an UNO)

It would be much more efficient and reliable to use Serial for one of the slaves.

If the messages you are sending between the master and slave has some kind of message delimiters (framing), you can still send debug messages to the Serial Monitor window. The slave should ignore it if there is no "start" character in your debug prints.

You would also see the messages sent to the slave in the Serial Monitor window. Be aware that you have to disconnect pin 0 to upload a new switch over USB. Some people put a switch in that wire so it's easy to upload.

Thanks for the information, I didn't knew about that libraries. But, how much arduino in total I will have in the end? (counting the extras arduino and a normal/HW serial arduino)? 2 or 3?

As I said, "two extra serial ports". That's in addition to the "normal HW Serial", for a total of 3.

I tried with I2C but somewhy it's fails (I can't send either request messages),

You should start a new thread to figure out why your I2C isn't working. Talking to 3 slaves is a good application for I2C. That's really what I2C was designed for...

JPZV:
Hi, I need to communicate 3 slaves (Bluno Nano) with a master (Arduino Nano). I tried with I2C but somewhy it's fails (I can't send either request messages)

'somewhy it's fails'

So the I2C fails to work as it should, and you post a problem with the serial receiving the wrong characters ?

As -dev has pointed out, I2C is designed for your sort of application and should work, far better to fix the problem with that than invent a replacement using multiple softserial ports, which probably will not work anyway.

In any case, your array to hold the string you receive is too short AND you do not make a string out of it by keeping it NULL terminated. Passing a char array that is not NULL terminated to a function that expects a NULL terminated char array (aka a string) is NOT a good idea.

Thanks hoy for yours replies.
Finally I bought an Arduino mega (Although I think that is a clone... For the price and for the paint on the board...) and now I can get both arduinos working fine.
I have a Little problem but is a Bluno related issue so I will open a topic in another sub-forum to maintain clean this one.

Thanks you again