Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. How to use SoftwareSerial Library?

How to use SoftwareSerial Library?

Scheduled Pinned Locked Moved Development
softwareserialrs485arduino nano
16 Posts 4 Posters 6.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kduinoK Offline
    kduinoK Offline
    kduino
    wrote on last edited by
    #7

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

    1 Reply Last reply
    0
    • kduinoK Offline
      kduinoK Offline
      kduino
      wrote on last edited by kduino
      #8

      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);
      }
      
      
      1 Reply Last reply
      0
      • kduinoK Offline
        kduinoK Offline
        kduino
        wrote on last edited by
        #9

        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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pjr
          wrote on last edited by pjr
          #10

          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 :laughing:

          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.

          1 Reply Last reply
          0
          • kduinoK Offline
            kduinoK Offline
            kduino
            wrote on last edited by
            #11

            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

            1 Reply Last reply
            0
            • kduinoK Offline
              kduinoK Offline
              kduino
              wrote on last edited by
              #12

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

              P 1 Reply Last reply
              0
              • kduinoK Offline
                kduinoK Offline
                kduino
                wrote on last edited by
                #13

                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>
                
                1 Reply Last reply
                0
                • kduinoK kduino

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

                  P Offline
                  P Offline
                  pjr
                  wrote on last edited by pjr
                  #14

                  @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..

                  1 Reply Last reply
                  0
                  • kduinoK Offline
                    kduinoK Offline
                    kduino
                    wrote on last edited by
                    #15

                    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.

                    1 Reply Last reply
                    0
                    • kduinoK Offline
                      kduinoK Offline
                      kduino
                      wrote on last edited by
                      #16

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      12

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular