B4R Tutorial Connecting two Arduinos with rSoftwareSerial library

SS-2016-04-13_15.50.49.png


With the rSoftwareSerial library you can turn regular pins to serial ports. This is useful as the hardware serial is used by the IDE during development.

There are some limitations to the supported pins: https://www.arduino.cc/en/Reference/softwareSerial

As an example, the following program should run on two boards. When you press on the connected button, the on-board led on the other board will turn on.

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private astream As AsyncStreams
   Private led13 As Pin
   Private btn As Pin
   Private softserial As SoftwareSerial
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   led13.Initialize(13, led13.MODE_OUTPUT)
   led13.DigitalWrite(False)
   softserial.Initialize(57600, 10, 11)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
   btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
   btn.AddListener("Btn_StateChanged")
End Sub

Sub Btn_StateChanged (State As Boolean)
   Dim b As Byte
   If State Then b = 1 Else b = 0
   astream.Write(Array As Byte(b))
End Sub

Sub astream_NewData (Buffer() As Byte)
   Dim state As Boolean = Buffer(0) = 1 'state will be true if buffer(0) is 1.
   led13.DigitalWrite(Not(state))
End Sub

The library is attached (it will be included in the next update).
 

Attachments

  • rSoftwareSerial.zip
    1.7 KB · Views: 1,514

derez

Expert
Licensed User
Longtime User
Erel can you make a version of this library for RPi (actually I need it for the OPi which does not want to communicate with arduino using the usb).
It will be great to send data using just simple pins.
 

Hypnos

Active Member
Licensed User
Longtime User
Hi Erel,

Just tried this example, when I connected the power into the main Arduino (the board which have the button), the other board also powered up, am I correct that I don't need the other power source for the 2nd board?
 

Laurent95

Active Member
Licensed User
Longtime User
Hi,
Just a question,
As i have memories of the Library Virtual Wire on Arduino it seems limited to 256 (bits-bytes ?) in the payload.
I saw it in case case of use with 433MHz RX/TX modules for the transmit datas.

Is it the same with this library ?
I ask because with the Arduino library we don't have an error in case the program sent more datas.

Thank you.
 
Last edited:

Laurent95

Active Member
Licensed User
Longtime User
There is no strict limit. However there could be errors with large packets. It will probably be better to use a timer so split the large payload.

What are you trying to send?

Thanks for the reply.
I have not yet implemented any Arduino code, i just start to discover B4R, who sounds very interesting.
Depends the case but i will have many things to sent.

The biggest project that i was planned to do on the Arduino is a weather station.
The program, at last version, could transmit, temp, humidity, % of light, wind speed, wind direction, quantity of rain, atmospheric pressure. And why not some other interesting parameters :).
In time where i was tried to code it on the Arduino i was needed to transmit two payloads otherwise i received no data in payload on the slave unit.
I didn't retrieve the exact code that i made at this time, but i have an example of the begining of the code attached for more understanding. In the example only DTH22 datas are sent.

For now i ask because i project to do a domotic complex, based on Arduino for the real time process and a computer or a maybe a Rasberry for the master management.
In fact i've many things to sent and receive, but i've also several Arduino and clones ;)
As i work in my new house that i just buy, it's the time to choose between the cable or RF communications.

Can you suggest me something ? In case that the problem in old house to cabling it's really more complicated. That's a reason why i thinks to use RF.

Thank you, and sorry to be totally out of subject :confused: if you want to move this post maybe it's preferable to not pollute this one.
 

Attachments

  • DHT11_TRANSMITTER-RECEIVER.zip
    2.6 KB · Views: 859

Laurent95

Active Member
Licensed User
Longtime User
It is better to start a new thread for this. I'll just say that there should be no problem sending 30+ parameters, assuming that you are sending them correctly. With correctly I mean sending them as binary data and not as strings.

Hi,
No worries i start a new thread when i'm ready with B4R.
Yes of course i remember that it's needed to sent in binary.
Thanks for the help.
 

devpialkt

Member
Licensed User
Longtime User
Hi Erel,

i've been tested that example with my arduino nano,
but I get this error:

error_arduino_nano.jpg


really need your advise :)

Thank You
 

derez

Expert
Licensed User
Longtime User
I need two Softwareserial ports (one for DFplayer, the other for HC-06 BT) on Arduino Uno at the same time (both transmit and receive async). I see in the limitations that it is not possible.
I have a solution with ESP but it is stationary (limited to the home network) and I want a portable device.
If I use a token to let only one port receive at a time, will the data be lost on the not-listening one ?
Any idea how to do it ?
 
Last edited:

Laurent95

Active Member
Licensed User
Longtime User
I need two Softwareserial ports (one for DFplayer, the other for HC-06 BT) on Arduino Uno at the same time (both transmit and receive async).
I see in the limitations that it is not possible. -> Depends how you manage them
I have a solution with ESP but it is stationary (limited to the home network) and I want a portable device.
If I use a token to let only one port receive at a time, will the data be lost on the not-listening one ? -> Yes
Any idea how to do it ? -> See code below if that respond to your demand, not sure

Hi,
Softwareserial is an emulator of serial ports (TX,RX) on Arduino, then, there are no reason that you can't use two different chanels for TX/RX, 4 digital ports, on the same card.
The proof is that the real RX/TX ports continue to work even you use Softwareserial.
Maybe the limitation that is discussed, where you see it, it's said because you can't use the both Softwareserial communication at the same time.
But if you can accept that only one communication works at a time, for that in the script you must use :
B4X:
My1stSerial->end;
My2ndSerial begin(9600);
Do what you need on Softwareserial2 communication
My2ndSerial->end;
My1stSerial->begin(1200);  // depends which speed you need
Do what you want with the Softwareserial1 communication
etc..
Of course you can't receive or transmit data on both serial chanels at the same time.
Then you must understand that one chanel only can listen a flow of data, but not the other. And of course they can't TX together at the same time.
The limitations is due that the library use the same 'timer', there are only 3 on an Arduino, for manage the communication.
Apart these limitation, you can use 2 Softwareserial communications or more.

But maybe an Arduino Mega can save you, i don 't know if they can work at the same time but these card have more than 1 physic TX/RX chanels.

Hope that helps you.

Regards.
Laurent
 

derez

Expert
Licensed User
Longtime User
Hi,
Softwareserial is an emulator of serial ports (TX,RX) on Arduino, then, there are no reason that you can't use two different chanels for TX/RX, 4 digital ports, on the same card.
The proof is that the real RX/TX ports continue to work even you use Softwareserial.
Maybe the limitation that is discussed, where you see it, it's said because you can't use the both Softwareserial communication at the same time.
But if you can accept that only one communication works at a time, for that in the script you must use :
B4X:
My1stSerial->end;
My2ndSerial begin(9600);
Do what you need on Softwareserial2 communication
My2ndSerial->end;
My1stSerial->begin(1200);  // depends which speed you need
Do what you want with the Softwareserial1 communication
etc..
Of course you can't receive or transmit data on both serial chanels at the same time.
Then you must understand that one chanel only can listen a flow of data, but not the other. And of course they can't TX together at the same time.
The limitations is due that the library use the same 'timer', there are only 3 on an Arduino, for manage the communication.
Apart these limitation, you can use 2 Softwareserial communications or more.

But maybe an Arduino Mega can save you, i don 't know if they can work at the same time but these card have more than 1 physic TX/RX chanels.

Hope that helps you.

Regards.
Laurent
Thank you, That is what I had in mind when I wrote "If I use a token to let only one port receive at a time".
 

Laurent95

Active Member
Licensed User
Longtime User
Will the Mega be able to communicate in both channels at the same time ?

Ok for precedent reply, you're welcome, then the code in my response can do it.
I haven't tested it for 2 SoftwareSerial communications but for send data with a RF433 MHz module on an Arduino Nano who get other data by SoftwareSerial way. Of course like the code in my precedent message i stop the SoftwareSerial com before sending data with the RF433.
That works, there are no reason for that's not work with 2 instances of SoftwareSerial.

For the Mega i think so, i use one, but it's stucked for Domoticz
And the time i finish the boxes in the garage, we moved recently, i can't put back my Nas on my Lan. In this case Domoticz will stay on the Rapsberry with the Arduino for a while :eek:.
But take a look at this, they said that's possible to use more than one TX/RX chanel and don't said it's not possible to TX at the same time on Mega :
http://forum.arduino.cc/index.php?topic=102769.0

Anyway on an Arduino Uno it's already possible between SoftWareSerial and the native TX/RX pins of Arduino to make it work.
See, there is also possibility to receive datas on 2 RX chanels (response of Crossroads) :
http://forum.arduino.cc/index.php?topic=206328.0
and believe me if Crossroads say it, that works, he is a guru with the Arduino.

Indeed if you use the Serial.print command when you use any sketch, even if a SoftWareSerial is used, that works. That means the native Serial works with the emulated TX/RX :)

Have fun, regards.
Laurent
 
Last edited:

derez

Expert
Licensed User
Longtime User
Ok for precedent reply, you're welcome, then the code in my response can do it.
I haven't tested it for 2 SoftwareSerial communications but for send data with a RF433 MHz module on an Arduino Nano who get other data by SoftwareSerial way. Of course like the code in my precedent message i stop the SoftwareSerial com before sending data with the RF433.
That works, there are no reason for that's not work with 2 instances of SoftwareSerial.

For the Mega i think so, i use one, but it's stucked for Domoticz
And the time i finish the boxes in the garage, we moved recently, i can't put back my Nas on my Lan. In this case Domoticz will stay on the Rapsberry with the Arduino for a while :eek:.
But take a look at this, they said that's possible to use more than one TX/RX chanel and don't said it's not possible to TX at the same time on Mega :
http://forum.arduino.cc/index.php?topic=102769.0

Anyway on an Arduino Uno it's already possible between SoftWareSerial and the native TX/RX pins of Arduino to make it work.
See, there is also possibility to receive datas on 2 RX chanels (response of Crossroads) :
http://forum.arduino.cc/index.php?topic=206328.0
and believe me if Crossroads say it, that works, he is a guru with the Arduino.

Indeed if you use the Serial.print command when you use any sketch, even if a SoftWareSerial is used, that works. That means the native Serial works with the emulated TX/RX :)

Have fun, regards.
Laurent

Thanks ! , many things to check... I'll report after checking.
 
Top