TheThingsNetwork / arduino-device-lib

Arduino Library for TTN Devices
MIT License
208 stars 96 forks source link

Added SoftwareSerial listen function #82

Closed bengchet closed 7 years ago

bengchet commented 8 years ago

I tweaked the library a bit to enable listen function if users use this library for SoftwareSerial case. In original library, SoftwareSerial can be used but if the user use another SoftwareSerial class in the same sketch, the library might not work. In this tweaked library, I added some lines to check if user uses modemStream as HardwareSerial or SoftwareSerial, if SoftwareSerial, modemStream will intend to listen every time before sendCommand function is executed.

CLAassistant commented 8 years ago

CLA assistant check
All committers have signed the CLA.

johanstokking commented 8 years ago

The reason why we're using Stream is because you can use SoftwareSerial, HardwareSerial, AltSoftSerial, etc. What functionality do you specifically miss in the current implementation?

bengchet commented 8 years ago

In SoftwareSerial, the most important point is listen() function, which will make specific port to listen to incoming data at RX. This case is important when there are more than 1 SoftwareSerial is implemented.

For example, a sample sketch here

#include <TinyGPS.h>
#include <TheThingsNetwork.h>

SoftwareSerial loraSerial(2, 3);

TinyGPS gps;
SoftwareSerial ss(10, 11);

TheThingsNetwork ttn(loraSerial, debugSerial, /* TTN_FP_EU868 or TTN_FP_US915 */);
...
void setup(){
  debugSerial.begin(115200);
  loraSerial.begin(57600);
  ss.begin(9600);

 ....
}

Since there are 2 SoftwareSerial are being used, from code above ss RX port will be listened prior to loraSerial. So all the commands sent by loraSerial will not get response,resulting the error. Even if we switch ss and loraSerial during the setup, in later program we will need to put listen() function strategically to make sure all devices are happy and get the data from their respective RX port.

Pull request might not needed, but may be we can do something about it, like creating examples for 2 or more SoftwareSerial case? This was my test on Arduino Uno with LoRa shield + GPS shield, both shields using UART.

htdvisser commented 7 years ago

@johanstokking?