B4R Tutorial Connecting two Arduinos with rSoftwareSerial library

Discussion in 'B4R Tutorials' started by Erel, Apr 13, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    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.

    Code:
    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(
    576001011)
       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).
     

    Attached Files:

  2. derez

    derez Expert Licensed 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.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible to implement a reliable software serial on the RPi as it is not a real time OS.
     
  4. Hypnos

    Hypnos Active Member Licensed 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?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    That's correct. You might need it if you connect a shield that requires more power.
     
  6. Laurent95

    Laurent95 Active Member Licensed 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: May 19, 2016
  7. Erel

    Erel Administrator Staff Member Licensed 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?
     
  8. Laurent95

    Laurent95 Active Member Licensed User

    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.
     

    Attached Files:

  9. Erel

    Erel Administrator Staff Member Licensed 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.
     
  10. Laurent95

    Laurent95 Active Member Licensed User

    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.
     
  11. devpialkt

    devpialkt Member Licensed User

    Hi Erel,

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

    [​IMG]

    really need your advise :)

    Thank You
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    I'm able to compile this example with the Nano board selected (though I didn't run it as I don't have this board here). Try to update to the latest version of Arduino IDE and set the path under Tools - Configure Paths.
     
  13. derez

    derez Expert Licensed 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: Dec 28, 2016
  14. Erel

    Erel Administrator Staff Member Licensed User

    Note that you can use a board with multiple serial ports instead like Arduino Mega or Arduino Due.

    You can lose data with multiple SoftwareSerials.
     
    derez likes this.
  15. Laurent95

    Laurent95 Active Member Licensed 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 :
    Code:
    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 likes this.
  16. derez

    derez Expert Licensed User

    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".
     
  17. derez

    derez Expert Licensed User

    Will the Mega be able to communicate in both channels at the same time ?
     
  18. Laurent95

    Laurent95 Active Member Licensed 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
     
    Last edited: Dec 28, 2016
    derez likes this.
  19. derez

    derez Expert Licensed User

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

    Erel Administrator Staff Member Licensed User

    Yes.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice