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,

  • 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

  • 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!


    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;
    	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);

Log in to reply

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.