How to use SoftwareSerial Library?
-
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; #endifBut 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; #endifand in your code define:
#define MY_RS485_HWSERIAL #define MY_RS485_SWSERIAL mySerialand 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.
-
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
-
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; #endifThe 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> -
Hi PJR,
would you mind explaining me the reason for defining MY_RS485_HWSERIAL?
Thank you,
by kduino@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 kduinoTake 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 kduinoPS: 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; #endifAs 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