How to use SoftwareSerial Library?



  • Hi,
    first of all thank you very much for the great forum and all the futures built in mySensors!
    Although I ve read up and down the forums and information, I am not really clear how to use the SoftwareSerial instead of AltSoftSerial lib. There are some comparison why the one might be better than the other but I didn't grab a sketch sample to go from there.
    The reason why I am asking is that I had established an rs485 network with custom designed nodes on arduino nano with rs485 shield. The system is a little bit like mySensors, it works with the ICSC lib, but unfortunately I came across mySensors too late. So all my PCBs are done and the parts are soldered and the rs485 module is connected like this:
    DI = Pin 2
    DE/RE = Pin 3
    RO = Pin 4
    The only option I have now to "upgrade" to my sensors is to get the SoftwareSerial lib running.
    Time is not an issue for me, so it would be totally sufficient to run at a speed of 19200 or 57600 Bauds.

    Is there anyone with a similar problem giving me a hint how to get along with it? That would be very nice.
    Best regards,
    City



  • I think there is couple things you need to tweak if you want to use SoftwareSerial instead of AltSoftSerial since its quite well baked in the default solution.

    See these:


  • Admin

    No problem,

    You should be able to use soft serial by defining:

    #define MY_RS485_HWSERIAL yoursoftserial
    

    It'll fallback to AltSoftSerial if undefined.



  • Hi,
    Thank you very much for the solution @hek: this sounds pretty straightforward 😉 and was what I was looking for.
    I will check it out this weekend.
    It would be fantastic to switch the hole system to mysensors! Since a lot of developing is already done.

    I will come back to post my results.

    Best regards
    Crty



  • Hi
    Before I was able to go testing I was wondering if the gateway would be much easier (but more dirty) to realize with two stacked arduinos. One is operating the rs485 to serial connection and sends it via serial to the second arduino which in turn is connected to udp or http.
    I know it would be neater to go with software but as far as I know this is realized within some hours.
    By the way in this case it turns into a good platform adapter since the second arduino can be virtually connected to anything.
    What do you think?

    Have a nice weekend. Kduino



  • Hi @kduino
    I think my problem is similar, what I want to do is to send the received sensor data on the serial gateway to a GSM modem for insertion into a Cloud server, but I have no idea of how to capture the data on the serial gateway, IE I can see the sensor values, but how would I trap the data for sending to the GSM modem? Any advice would be appreciated please



  • Hi,
    actualy I am still struggling getting the rs485 gateway and the motion sensor example running. for some reason they don't communicate. So I connected them directly over serial output without rs485 module - but still no connection. I will investigate further...
    As soon as they are connected I will extend the network....



  • Hi Hek,
    thank you again for the information. Unfortunately I wasn't able to get it to work like this. Is there a trick in terms of order of the definitions?

    Thank you so much for your help!

    kduino

    Below you will find my sketch:

    #include <SoftwareSerial.h>
    
    SoftwareSerial mySerial(4, 2);
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    #define MY_NODE_ID 80
    
    // Enable RS485 transport layer
    #define MY_RS485
    
    // Define this to enables DE-pin management on defined pin
    #define MY_RS485_DE_PIN 3
    
    // Set RS485 baud rate to use
    #define MY_RS485_BAUD_RATE 9600
    
    // Enable this if RS485 is connected to a hardware serial port
    #define MY_RS485_HWSERIAL mySerial
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()
    {
    	pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("Motion Sensor", "1.0");
    
    	// Register all sensors to gw (they will be created as child devices)
    	present(CHILD_ID, S_MOTION);
    }
    
    void loop()
    {
    	// Read digital motion value
    	bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    
    	Serial.println(tripped);
    	send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
    
    	// Sleep until interrupt comes in on motion sensor. Send update every two minute.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    


  • Hi,
    I think I got to the specific part of code:

    From Github MySensors/hal/transport/MyTransportRS485.cpp
    https://github.com/mysensors/MySensors/blob/development/hal/transport/MyTransportRS485.cpp#L88

    #if defined(__linux__)
    SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
    #elif defined(MY_RS485_HWSERIAL)
    HardwareSerial& _dev = MY_RS485_HWSERIAL;
    #else
    AltSoftSerial _dev;
    #endif
    

    But unfortunately I am not able to change it to the softwareSerial option. How would the code look like if I would make an additional if for the softwareSerial?
    Help is very appreciated.

    By, kduino



  • Just an idea:
    I would try adding:

    #elif defined(MY_RS485_SWSERIAL)
    SoftwareSerial& _dev = MY_RS485_SWSERIAL;
    

    so it looks like this:

    #if defined(__linux__)
    SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
    #elif defined(MY_RS485_SWSERIAL)
    SoftwareSerial& _dev = MY_RS485_SWSERIAL;
    #elif defined(MY_RS485_HWSERIAL)
    HardwareSerial& _dev = MY_RS485_HWSERIAL;
    #else
    AltSoftSerial _dev;
    #endif
    

    and in your code define:

    #define MY_RS485_HWSERIAL
    #define MY_RS485_SWSERIAL mySerial
    

    and see what happens 😆

    ps. the #define of MY_RS485_HWSERIAL is still there since I'm not sure what will be missing for serial communication if that is not defined.



  • HI PJR,
    thank you very much for your reply. I did as you suggested but were not able to test during the last days.
    Today I wired it up and it worked. Unfortunately updating some sensor code it is currently not working but as soon as possible I will post the whole sketch together with the adapted MyTransportRS485.ccp.

    By kduino



  • Hi PJR,
    would you mind explaining me the reason for defining MY_RS485_HWSERIAL?
    Thank you,
    by kduino



  • I did it like this:

    #if defined(__linux__)
    SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
    #elif defined(MY_RS485_SWSERIAL)
    SoftwareSerial& _dev = MY_RS485_SWSERIAL;
    #elif defined(MY_RS485_HWSERIAL)
    HardwareSerial& _dev = MY_RS485_HWSERIAL;
    #else
    AltSoftSerial _dev;
    #endif
    

    The Arduino code is extended by:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable RS485 transport layer
    #define MY_RS485
    
    // Define this to enables DE-pin management on defined pin
    #define MY_RS485_DE_PIN 3
    
    // Set RS485 baud rate to use
    #define MY_RS485_BAUD_RATE 9600
    
    // Enable this if RS485 is connected to a hardware serial port
    //#define MY_RS485_HWSERIAL Serial1
    
    // Enable this if RS485 is connected to Software Serial port:
    #include <SoftwareSerial.h>
    SoftwareSerial softSerial(4, 2); //rx, tx
    #define MY_RS485_SWSERIAL softSerial
    
    #include <MySensors.h>
    


  • @kduino said in How to use SoftwareSerial Library?:

    Hi PJR,
    would you mind explaining me the reason for defining MY_RS485_HWSERIAL?
    Thank you,
    by kduino

    Take a look at MySensors.h:

    #elif defined(MY_RS485)
      #if !defined(MY_RS485_HWSERIAL)
        #if defined(__linux__)
          #error You must specify MY_RS485_HWSERIAL for RS485 transport
        #endif
        #include "drivers/AltSoftSerial/AltSoftSerial.cpp"
      #endif
      #include "hal/transport/MyTransportRS485.cpp"
    ..
    

    If I read this correctly it loads also AltSoftSerial if you dont define MY_RS485_HWSERIAL
    Of course I can read this wrong..



  • Hi PJR,
    thanks a lot! This is obvious. Please apologize that I didn't take a look at this site. So the MyTransportRS485.cpp is loaded only in case of defining MY_HWSERIAL... This explains why it was not working after skipping the MY_HWSERIAL definition.
    Unfortunately I was not able to run another test now but I hope it will come up soon.
    The changes regarding softwareSerial would be a great help for those who are not able to use the altSoftSerial pins.
    Once it is running stable I will post a request @hek to ask for implementation.
    I will come back with more tests.
    By kduino

    PS: It seams that during compilation it loads also the AltSoftSerial.cpp. In case of use of the SoftwareSerial it would be unnecessary overhead, of course. Is there a reason why it is loaded at this stage? I would rather load it at MyTransportXYZ.cpp.



  • Hi PJR,
    now it is working with this code in the MyTransportRS485 without any changes on MySensors.h:

    #if defined(MY_RS485_SWSERIAL)
    SoftwareSerial& _dev = MY_RS485_SWSERIAL;
    #elif defined(__linux__)
    SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
    #elif defined(MY_RS485_HWSERIAL)
    HardwareSerial& _dev = MY_RS485_HWSERIAL;
    #else
    AltSoftSerial _dev;
    #endif
    

    As far as I can interpret the peace of code, the
    #include "hal/transport/MyTransportRS485.cpp"
    is active as soon as the MY_RS485 layer is defined.

    This is a great step, I can use all my nodes from now on!
    Thank you very much for your support!
    By Kduino



Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts